seulberg1
seulberg1

Reputation: 1013

Plot line-chart with vertical histogram for each timestep

I am trying to plot a line chart that shows the mean value for each point in time in a dataset (say you observe 100 datapoints for each day and plot the mean for each day) - this part is easy.

What I cannot figure out to do is how to add the distribution of observed values as a vertical histogram for each timestep. Because it may be a bit hard to describe, I made a pro-level paint drawing of the desired output:

enter image description here

#Data is in x, rows are observations, columns are timesteps
import numpy as np
import matplotlib.pyplot as plt
x = np.random.rand(100,10) + np.arange(0,10)/100
plt.plot(np.mean(x,axis=0))

plt.figure()
for i in range(x.shape[1]):
    plt.hist(x[:,i], label="i")

So I can easily plot the mean and I can also easily plot the histograms, but I am struggling to combine them both into the same graph. I considering using violin plots, but didn't make any headway there either. Any help is much appreciated.

Upvotes: 0

Views: 596

Answers (1)

K. Shores
K. Shores

Reputation: 1005

I'm not really sure what you're going for, but is something like this what you're after? Of course something isn't correct about the placement of the curves, but perhaps it's close-ish to what you want.

import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import gaussian_kde

x = np.random.rand(100,10) + np.arange(0,10)/100

fig, ax = plt.subplots()

ax.plot(np.mean(x,axis=0))

for i in range(x.shape[1]):
    kde = gaussian_kde(x[:,i])
    y = np.linspace(0, 1, 20)
    ax.plot(-kde(y)+i, y)

enter image description here

Upvotes: 1

Related Questions