Is there a clean way to generate a line histogram chart in Python?

牧云@^-^@ 提交于 2019-12-04 08:29:57

问题


I need to create a histogram that plots a line and not a step or bar chart. I am using python 2.7 The plt.hist function below plots a stepped line and the bins don't line up in the plt.plot function.

import matplotlib.pyplot as plt
import numpy as np
noise = np.random.normal(0,1,(1000,1))
(n,x,_) = plt.hist(noise, bins = np.linspace(-3,3,7), histtype=u'step' )  
plt.plot(x[:-1],n)

I need the line to correlate with each bin's count at the bin centers as if there was a histtype=u'line' flag to go with the align=u'mid' flag


回答1:


Using scipy, you could use stats.gaussian_kde to estimate the probability density function:

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

noise = np.random.normal(0, 1, (1000, ))
density = stats.gaussian_kde(noise)
n, x, _ = plt.hist(noise, bins=np.linspace(-3, 3, 50), 
                   histtype=u'step', density=True)  
plt.plot(x, density(x))
plt.show()




回答2:


The line plot you are producing does not line up as the x values being used are the bin edges. You can calculate the bin centers as follows: bin_centers = 0.5*(x[1:]+x[:-1]) Then the complete code would be:

noise = np.random.normal(0,1,(1000,1))
n,x,_ = plt.hist(noise, bins = np.linspace(-3,3,7), histtype=u'step' )
bin_centers = 0.5*(x[1:]+x[:-1])
plt.plot(bin_centers,n) ## using bin_centers rather than edges
plt.show()

If you want the plot filled to y=0 then use plt.fill_between(bin_centers,n)




回答3:


Matplotlib's thumbnail gallery is usually quite helpful in situations like yours. A combination of this and this one from the gallery with some customizations is probably very close to what you have in mind:

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

mu = 0
sigma = 1
noise = np.random.normal(mu, sigma, size=1000)
num_bins = 7
n, bins, _ = plt.hist(noise, num_bins, normed=1, histtype='step')
y = mlab.normpdf(bins, mu, sigma)
plt.plot(bins, y, 'r--')
plt.show()

Also, increasing the number of bins helps...



来源:https://stackoverflow.com/questions/27872723/is-there-a-clean-way-to-generate-a-line-histogram-chart-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!