Oren
Oren

Reputation: 5359

histtype='stepfilled' option using bar function

I want to create this kind of histogram: enter image description here

Using bar function, I know I can do it with hist using histtype='stepfilled', but how can I do it with the bar function?

Upvotes: 0

Views: 1456

Answers (1)

tmdavison
tmdavison

Reputation: 69173

I don't think its possible with plt.bar alone, but you can do this with a combination of plt.bar to get the shaded region, and plt.step to get the outline.

For example:

import numpy as np
import matplotlib.pyplot as plt

fig,ax = plt.subplots(1)

# Generate some random data
hist1,bins1 = np.histogram(np.random.normal(2,1,1000),bins=np.arange(-2,8,0.5))
hist2,bins2 = np.histogram(np.random.normal(4,1,1000),bins=np.arange(0,10,0.5))

# 'step' draws the lines, 'bar' fills them
ax.step(bins1[:-1],hist1,'k',linestyle='-',linewidth=2,where='post')
ax.bar(bins1[:-1],hist1,width=0.5,linewidth=0,facecolor='k',alpha=0.3)
ax.step(bins2[:-1],hist2,'k',linestyle='--',linewidth=2,where='post')
ax.bar(bins2[:-1],hist2,width=0.5,linewidth=0,facecolor='k',alpha=0.1)

plt.show()

enter image description here

Upvotes: 1

Related Questions