Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to generate a series of histograms on matplotlib?

I would like to generate a series of histogram shown below:

enter image description here

The above visualization was done in tensorflow but I'd like to reproduce the same visualization on matplotlib.

EDIT: Using plt.fill_between suggested by @SpghttCd, I have the following code:

colors=cm.OrRd_r(np.linspace(.2, .6, 10))
plt.figure()
x = np.arange(100)
for i in range(10):
    y = np.random.rand(100)
    plt.fill_between(x, y + 10-i, 10-i, 
                     facecolor=colors[i]
                     edgecolor='w')
plt.show()

This works great, but is it possible to use histogram instead of a continuous curve?

like image 527
MoneyBall Avatar asked Dec 14 '22 14:12

MoneyBall


1 Answers

EDIT:
joypy based approach, like mentioned in the comment of october:

import pandas as pd
import joypy
import numpy as np

df = pd.DataFrame()
for i in range(0, 400, 20):
    df[i] = np.random.normal(i/410*5, size=30)
joypy.joyplot(df, overlap=2, colormap=cm.OrRd_r, linecolor='w', linewidth=.5)

enter image description here

for finer control of colors, you can define a color gradient function which accepts a fractional index and start and stop color tuples:

def color_gradient(x=0.0, start=(0, 0, 0), stop=(1, 1, 1)):
    r = np.interp(x, [0, 1], [start[0], stop[0]])
    g = np.interp(x, [0, 1], [start[1], stop[1]])
    b = np.interp(x, [0, 1], [start[2], stop[2]])
    return (r, g, b)

Usage:

joypy.joyplot(df, overlap=2, colormap=lambda x: color_gradient(x, start=(.78, .25, .09), stop=(1.0, .64, .44)), linecolor='w', linewidth=.5)

Examples with different start and stop tuples:

enter image description here


original answer:

You could iterate over your dataarrays you'd like to plot with plt.fill_between, setting colors to some gradient and the line color to white:

creating some sample data:

import numpy as np
t = np.linspace(-1.6, 1.6, 11)
y = np.cos(t)**2
y2 = lambda : y + np.random.random(len(y))/5-.1

plot the series:

import matplotlib.pyplot as plt

import matplotlib.cm as cm
colors = cm.OrRd_r(np.linspace(.2, .6, 10))

plt.figure()
for i in range(10):
    plt.fill_between(t+i, y2()+10-i/10, 10-i/10, facecolor = colors[i], edgecolor='w')

enter image description here

If you want it to have more optimized towards your example you should perhaps consider providing some sample data.

EDIT:
As I commented below, I'm not quite sure if I understand what you want - or if you want the best for your task. Therefore here a code which plots besides your approach in your edit two smples of how to present a bunch of histograms in a way that they are better comparable:

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.cm as cm

N = 10
np.random.seed(42)

colors=cm.OrRd_r(np.linspace(.2, .6, N))

fig1 = plt.figure()
x = np.arange(100)
for i in range(10):
    y = np.random.rand(100)
    plt.fill_between(x, y + 10-i, 10-i, 
                     facecolor=colors[i],
                     edgecolor='w')

data = np.random.binomial(20, .3, (N, 100))

fig2, axs = plt.subplots(N, figsize=(10, 6))
for i, d in enumerate(data):
    axs[i].hist(d, range(20), color=colors[i], label=str(i))
fig2.legend(loc='upper center', ncol=5)


fig3, ax = plt.subplots(figsize=(10, 6))
ax.hist(data.T, range(20), color=colors, label=[str(i) for i in range(N)])
fig3.legend(loc='upper center', ncol=5)

This leads to the following plots:

  1. your plot from your edit: enter image description here

  2. N histograms in N subplots: enter image description here

  3. N histograms side by side in one plot: enter image description here

like image 175
SpghttCd Avatar answered Jan 16 '23 00:01

SpghttCd