Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 叠加KDE和直方图时正确渲染y轴_Python_Pandas_Dataframe_Matplotlib - Fatal编程技术网

Python 叠加KDE和直方图时正确渲染y轴

Python 叠加KDE和直方图时正确渲染y轴,python,pandas,dataframe,matplotlib,Python,Pandas,Dataframe,Matplotlib,以前也曾提出过类似的问题,但没有同时使用这两个绘图函数,因此我们: 我有一个熊猫数据框中的列,我正在绘制直方图和KDE。但是,当我绘制它们时,y轴使用的是原始数据值范围,而不是离散的样本数/箱数(我想要的)。我怎样才能解决这个问题?实际绘图是完美的,但y轴是错误的 数据: 代码: 现在的样子: 如果你想要真实的计数,你需要将KDE放大到箱子的宽度乘以观察的数量。最棘手的部分是访问熊猫用来绘制KDE的数据。(为了简化手头的问题,我删除了与图例相关的部分) seaborn是一种选择吗?使用kde=T

以前也曾提出过类似的问题,但没有同时使用这两个绘图函数,因此我们:

我有一个熊猫数据框中的列,我正在绘制直方图和KDE。但是,当我绘制它们时,y轴使用的是原始数据值范围,而不是离散的样本数/箱数(我想要的)。我怎样才能解决这个问题?实际绘图是完美的,但y轴是错误的

数据:

代码:

现在的样子:

如果你想要真实的计数,你需要将KDE放大到箱子的宽度乘以观察的数量。最棘手的部分是访问熊猫用来绘制KDE的数据。(为了简化手头的问题,我删除了与图例相关的部分)


seaborn是一种选择吗?使用
kde=True
可能会使这更简单。也许在下一次绘图中,我将使用seaborn。我尽量不向环境中添加更多的模块,因为熊猫。数据框有自己的绘图功能,我想使用它们。谢谢!快速提问,如果要覆盖,为什么要使用子图?我以为你使用子图来并排放置绘图。@ninthpower-
fig,ax=plt.subplot()
是正常的方法。@Alolz所以我一直得到一个错误:
AttributeError:“矩形”对象没有属性“\ux”。
调试器指向
xdata=axis.get\u children()[0]。\ux
奇怪的是,当我绘图到pdf时,它仍然可以很好地绘制这个(以及其他20多个绘图)。我认为这可能与我正在使用的列的值非常接近(例如[1.003,1.0029,1.001…])有关。ninthpower-hmm,Line2D对象可能位于
轴的不同位置。可能有更好的方法来选择它,而不仅仅是基于位置,但您可以在打印之后打印(轴),并查看哪个索引包含Line2D对象以获取值
t2 = [140547476703.0, 113395471484.0, 158360225172.0, 105497674121.0, 186457736557.0, 153705359063.0, 36826568371.0, 200653068740.0, 190761317478.0, 126529980843.0, 98776029557.0, 132773701862.0, 14780432449.0, 167507656251.0, 121353262386.0, 136377019007.0, 134190768743.0, 218619462126.0, 07912778721.0, 215628911255.0, 147024833865.0, 94136343562.0, 135685803096.0, 165901502129.0, 45476074790.0, 125195690010.0, 113910844263.0, 123134290987.0, 112028565305.0, 93448218430.0, 07341012378.0, 93146854494.0, 132958913610.0, 102326700019.0, 196826471714.0, 122045354980.0, 76591131961.0, 134694468251.0, 120212625727.0, 108456858852.0, 106363042112.0, 193367024628.0, 39578667378.0, 178075400604.0, 155513974664.0, 132834624567.0, 137336282646.0, 125379267464.0]
fig = plt.figure()
# plot hist + kde
t2[t2.columns[0]].plot.kde(color = "maroon", label = "_nolegend_")
t2[t2.columns[0]].plot.hist(density = True, edgecolor = "grey", color = "tomato", title = t2.columns[0])

# plot mean/stdev
m = t2[t2.columns[0]].mean()
stdev = t2[t2.columns[0]].std()
plt.axvline(m, color = "black", ymax = 0.05, label = "mean")
plt.axvline(m-2*stdev, color = "black", ymax = 0.05, linestyle = ":", label = "+/- 2*Stdev")
plt.axvline(m+2*stdev, color = "black", ymax = 0.05, linestyle = ":")
plt.legend()
import matplotlib.pyplot as plt
import numpy as np

# Calculate KDE, get data
axis = t2[t2.columns[0]].plot.kde(color = "maroon", label = "_nolegend_")
xdata = axis.get_children()[0]._x
ydata = axis.get_children()[0]._y
plt.clf()


# Real figure
fig, ax = plt.subplots(figsize=(7,5))
# Plot Histogram, no density.
x = ax.hist(t2[t2.columns[0]], edgecolor = "grey", color = "tomato")

# size of the bins * N obs
scale = np.diff(x[1])[0]*len(t2)

# Plot scaled KDE
ax.plot(xdata, ydata*scale, color='blue')
ax.set_ylabel('N observations')

plt.show()