Python中的Matplotlib带外线的累积频率图

2024-09-24 02:19:21 发布

您现在位置:Python中文网/ 问答频道 /正文

当我运行我的代码来生成一个数据集的相对累积频率的图时,我的图会在图与右边的y=1交叉的点处显示一条直线,like this one。在

y轴被限制在y=0到{}的范围内,代表0%到100%的累积频率,一旦图形达到y=1,或100%,它应该继续在y=1直到x轴的上限,从x=0到{},类似于this graph。在

有什么方法可以确保到达y=1之后的y=1处的直方图是连续的吗?我需要x轴保持在范围[0,2]上,y轴保持在范围[0,1]上。在

下面是我用来生成图形的Python代码:

import matplotlib.pyplot as plt 
# ...
plt.ylabel('Relative Cumulative Frequency')
plt.xlabel('Normalized Eigenvalues')
plt.hist(e.real, bins = 50, normed=1, histtype='step', cumulative=True)
# Limit X and Y ranges   
plt.xlim(0, 2)
plt.ylim(0, 1)

谢谢,麦克斯


Tags: 数据方法代码图形plt代表直方图this
1条回答
网友
1楼 · 发布于 2024-09-24 02:19:21

您可以通过创建自己的垃圾箱和setting the last bin to ^{}来完成此操作:

import matplotlib.pyplot as plt
import numpy as np
...
x = np.random.rand(100,1)

plt.ylabel('Relative Cumulative Frequency')
plt.xlabel('Normalized Eigenvalues')

binsCnt = 50
bins = np.append(np.linspace(x.min(), x.max(), binsCnt), [np.inf])
plt.hist(x, bins = bins, normed=1, histtype='step', cumulative=True)
# Limit X and Y ranges
plt.xlim(0, 2)
plt.ylim(0, 1)
plt.show()

plot

相关问题 更多 >