python:绘制柱状图,顶部有一条函数线

python:绘制柱状图,顶部有一条函数线,python,matplotlib,scipy,Python,Matplotlib,Scipy,我尝试在Python中使用SciPy for stats和matplotlib进行一些分布绘制和拟合。我在创建柱状图方面运气不错: seed(2) alpha=5 loc=100 beta=22 data=ss.gamma.rvs(alpha,loc=loc,scale=beta,size=5000) myHist = hist(data, 100, normed=True) 太棒了 我甚至可以使用相同的伽马参数,绘制概率分布函数的线函数(在谷歌搜索之后): 我将如何绘制直方图myHist

我尝试在Python中使用SciPy for stats和matplotlib进行一些分布绘制和拟合。我在创建柱状图方面运气不错:

seed(2)
alpha=5
loc=100
beta=22
data=ss.gamma.rvs(alpha,loc=loc,scale=beta,size=5000)
myHist = hist(data, 100, normed=True)

太棒了

我甚至可以使用相同的伽马参数,绘制概率分布函数的线函数(在谷歌搜索之后):


我将如何绘制直方图
myHist
,并将PDF行
h
叠加在直方图顶部?我希望这是微不足道的,但我一直无法理解

把这两部分放在一起就行了

import scipy.stats as ss
import numpy as np
import matplotlib.pyplot as plt
alpha, loc, beta=5, 100, 22
data=ss.gamma.rvs(alpha,loc=loc,scale=beta,size=5000)
myHist = plt.hist(data, 100, normed=True)
rv = ss.gamma(alpha,loc,beta)
x = np.linspace(0,600) 
h = plt.plot(x, rv.pdf(x), lw=2)
plt.show()

为了确保在任何特定的绘图实例中都能得到所需的结果,请首先尝试创建一个
figure
对象

import scipy.stats as ss
import numpy as np
import matplotlib.pyplot as plt
# setting up the axes
fig = plt.figure(figsize=(8,8))
ax  = fig.add_subplot(111)
# now plot
alpha, loc, beta=5, 100, 22
data=ss.gamma.rvs(alpha,loc=loc,scale=beta,size=5000)
myHist = ax.hist(data, 100, normed=True)
rv = ss.gamma(alpha,loc,beta)
x = np.linspace(0,600)
h = ax.plot(x, rv.pdf(x), lw=2)
# show
plt.show()

人们可能对绘制任何直方图的分布函数感兴趣。 这可以使用
seaborn kde
功能完成

import numpy as np # for random data
import pandas as pd  # for convinience
import matplotlib.pyplot as plt  # for graphics
import seaborn as sns  # for nicer graphics

v1 = pd.Series(np.random.normal(0,10,1000), name='v1')
v2 = pd.Series(2*v1 + np.random.normal(60,15,1000), name='v2')

# plot a kernel density estimation over a stacked barchart
plt.figure()
plt.hist([v1, v2], histtype='barstacked', normed=True);
v3 = np.concatenate((v1,v2))
sns.kdeplot(v3);
plt.show()

从一门关于python数据可视化的课程中

扩展了Malik的答案,并试图坚持使用vanilla NumPy、SciPy和Matplotlib。我使用了Seaborn,但它只用于提供更好的默认设置和小的视觉调整:

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

import seaborn as sns
sns.set(style='ticks')

# parameterise our distributions
d1 = sps.norm(0, 10)
d2 = sps.norm(60, 15)

# sample values from above distributions
y1 = d1.rvs(300)
y2 = d2.rvs(200)
# combine mixture
ys = np.concatenate([y1, y2])

# create new figure with size given explicitly
plt.figure(figsize=(10, 6))

# add histogram showing individual components
plt.hist([y1, y2], 31, histtype='barstacked', density=True, alpha=0.4, edgecolor='none')

# get X limits and fix them
mn, mx = plt.xlim()
plt.xlim(mn, mx)

# add our distributions to figure
x = np.linspace(mn, mx, 301)
plt.plot(x, d1.pdf(x) * (len(y1) / len(ys)), color='C0', ls='--', label='d1')
plt.plot(x, d2.pdf(x) * (len(y2) / len(ys)), color='C1', ls='--', label='d2')

# estimate Kernel Density and plot
kde = sps.gaussian_kde(ys)
plt.plot(x, kde.pdf(x), label='KDE')

# finish up
plt.legend()
plt.ylabel('Probability density')
sns.despine()
给了我们以下的情节:


我一直尝试使用最小的功能集,同时生成相对较好的输出,特别是使用SciPy来估计KDE非常容易。

您可能正在用不同的图形绘制直方图和曲线图。如果您只是在同一个图形上调用hist和plot函数,那么2应该是superimposed@Dhara就是这样。我使用的是ipython笔记本电脑,我遇到的问题是我使用的是ipython笔记本电脑,所以我会运行一个绘图,它会以交互方式绘图,然后我会做一些事情,然后再绘图另一个,最后会出现一个新的绘图。谢谢你帮我解决这个问题!
import numpy as np
import scipy.stats as sps
import matplotlib.pyplot as plt

import seaborn as sns
sns.set(style='ticks')

# parameterise our distributions
d1 = sps.norm(0, 10)
d2 = sps.norm(60, 15)

# sample values from above distributions
y1 = d1.rvs(300)
y2 = d2.rvs(200)
# combine mixture
ys = np.concatenate([y1, y2])

# create new figure with size given explicitly
plt.figure(figsize=(10, 6))

# add histogram showing individual components
plt.hist([y1, y2], 31, histtype='barstacked', density=True, alpha=0.4, edgecolor='none')

# get X limits and fix them
mn, mx = plt.xlim()
plt.xlim(mn, mx)

# add our distributions to figure
x = np.linspace(mn, mx, 301)
plt.plot(x, d1.pdf(x) * (len(y1) / len(ys)), color='C0', ls='--', label='d1')
plt.plot(x, d2.pdf(x) * (len(y2) / len(ys)), color='C1', ls='--', label='d2')

# estimate Kernel Density and plot
kde = sps.gaussian_kde(ys)
plt.plot(x, kde.pdf(x), label='KDE')

# finish up
plt.legend()
plt.ylabel('Probability density')
sns.despine()