我正在尝试绘制两个df的CDF。我正在使用下面的代码
plt.hist([df['A'], df['B']], 1000, density=True, histtype='step', cumulative=True, label='Empirical')plt.show()
我得到了这个图表:

这张图看起来是正确的,但是,我想在没有垂直90度线的情况下得到它,还有,我如何在1处切割y轴?
谢谢
发布于 2018-11-13 22:05:54
摆脱路径在结束时落到零的做法就像不使用pyplot.hist一样简单。相反,我将使用numpy.histogram并生成自己的CDF点,以便使用pyplot.plot将其输入到简单的线状图中
#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复制的示例:
隐藏顶部和右侧脊椎
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。
https://stackoverflow.com/questions/53282681
复制相似问题