首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >绘制CDF的平滑曲线

绘制CDF的平滑曲线
EN

Stack Overflow用户
提问于 2018-11-13 21:58:57
回答 1查看 854关注 0票数 0

我正在尝试绘制两个df的CDF。我正在使用下面的代码

代码语言:javascript
复制
plt.hist([df['A'], df['B']], 1000, density=True, histtype='step', cumulative=True, label='Empirical')

plt.show()

我得到了这个图表:

这张图看起来是正确的,但是,我想在没有垂直90度线的情况下得到它,还有,我如何在1处切割y轴?

谢谢

EN

回答 1

Stack Overflow用户

发布于 2018-11-13 22:05:54

摆脱路径在结束时落到零的做法就像不使用pyplot.hist一样简单。相反,我将使用numpy.histogram并生成自己的CDF点,以便使用pyplot.plot将其输入到简单的线状图中

代码语言:javascript
复制
#generate bins and bin edges
A, Aedges = np.histogram(df['A'], bins = 1000)
B, Bedges = np.histogram(df['B'], bins = 1000)
#cumsum and normalize to get cdf rather than pdf
A = np.cumsum(A)
A /= A[-1]
B = np.cumsum(B)
B /= B[-1]
#convert bin edges to bin centers
Acenters = (Aedges[:-1]+Aedges[1:])/2
Bcenters = (Bedges[:-1]+Bedges[1:])/2
#plot a simple line plot instead of a histogram
plt.plot(A, Acenters)
plt.plot(B, Bcenters)

如果你想自定义图周围的脊椎/方框:这是一个从我保留的matplotlib cheat sheet复制的示例:

隐藏顶部和右侧脊椎

代码语言:javascript
复制
x = np.linspace(-np.pi, np.pi, 800)
y = np.sin(x)
fig, ax = plt.subplots(figsize=(8, 4))
ax.plot(x, y, label='Sine', color='red')
ax.set_axis_bgcolor('#e5e5e5')
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.spines['left'].set_position(
('outward',10))
ax.spines['bottom'].set_position(
('outward',10))
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
# do the ax.grid() after setting ticks
ax.grid(b=True, which='both',
color='white', linestyle='-',
linewidth=1.5)
ax.set_axisbelow(True)
ax.legend(loc='best', frameon=False)
fig.savefig('filename.png', dpi=125)

如果您使用的是pyplot而不是专用的图形/轴组合,则可以使用plt.gca()plt获取ax

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53282681

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档