Python Matplotlib CDF返回到零

Python Matplotlib CDF返回到零,python,pandas,matplotlib,Python,Pandas,Matplotlib,当尝试使用matplotlib的hist函数绘制累积分布函数(CDF)时,最后一点返回到零。我读了一些帖子,解释这是因为类似直方图的格式,但无法为我的案例找到解决方案 这是我的密码: import matplotlib.pyplot as plt x = [7.845419,7.593756,7.706831,7.256211,7.147965] fig, ax=plt.subplots() ax.hist(x, cumulative=True, normed=1, histtype='st

当尝试使用matplotlib的
hist
函数绘制累积分布函数(CDF)时,最后一点返回到零。我读了一些帖子,解释这是因为类似直方图的格式,但无法为我的案例找到解决方案

这是我的密码:

import matplotlib.pyplot as plt

x = [7.845419,7.593756,7.706831,7.256211,7.147965]

fig, ax=plt.subplots()
ax.hist(x, cumulative=True, normed=1, histtype='step', bins=100, label=('Label-1'), lw=2)
ax.grid(True)
ax.legend(loc='upper left')

plt.show()
这将生成以下图像

如您所见,step函数在直方图的最后一个仓位之后变为零,这是不需要的。如何更改代码以使CDF不返回零


谢谢你

你总是有一个选择,那就是先计算直方图,然后按照你喜欢的方式绘制结果,而不是依赖于
plt.hist

这里您将使用
numpy.histogram
计算直方图。然后,您可以创建一个数组,其中每个点都在其中重复,以获得阶梯状的行为

import numpy as np
import matplotlib.pyplot as plt

x = [7.845419,7.593756,7.706831,7.256211,7.147965]

h, edges = np.histogram(x, density=True, bins=100, )
h = np.cumsum(h)/np.cumsum(h).max()

X = edges.repeat(2)[:-1]
y = np.zeros_like(X)
y[1:] = h.repeat(2)

fig, ax=plt.subplots()

ax.plot(X,y,label='Label-1', lw=2)

ax.grid(True)
ax.legend(loc='upper left')
plt.show()

Hi@ImportanceOfBeingErnest。谢谢你的回答。我在问题中添加了完整的代码。我明白了。我对问题进行了编辑,使问题变得清晰,并对问题进行了详细说明。(我想请你们下次提问时以这种形式提供一个问题,因为这可以节省大家很多时间。)看看OP给出的技巧,这个问题看起来确实更容易理解。非常感谢。