python - Python 中带有额外线条的 Matplotlib 累积频率图

标签 python matplotlib graph

当我运行我的代码来生成数据集的相对累积频率的图表时,我的图表在图表与 y=1 线相交的点处直线向下右侧,like this one .

y 轴限制在 y=0y=1 的范围内,代表累积频率的 0% 到 100%,一旦图形达到 y=1,或 100%,它应该继续 y=1 直到 x 轴的上限,从 x=0x=2,类似于 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)

谢谢,麦克斯

最佳答案

您可以通过创建自己的垃圾箱和 setting the last bin to np.Inf 来做到这一点:

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

关于python - Python 中带有额外线条的 Matplotlib 累积频率图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36629069/

相关文章:

python - 使用 cdo 计算选定区域经纬度的平均值

java - 使用 DFS 检测无向图中的循环

graph - 需要图形分区技术

python - 改变 networkx/matplotlib 力图中的边长和簇间距

python - 如何使用 matplotlib 在绘图的角落插入小图像?

python - 如何使用 x 轴为 "Date"的 seaborn 实现 Lineplot

python - python中连续小波变换图(比例图)中的频率轴

python - 数据帧错误 - ValueError : Can only compare identically-labeled Series objects

python - 无法使按钮(使用 tkinter 创建)自动退出

python - Matplotlib 将轴显示为二进制