【问题标题】:Matplotlib cumulative frequency graph with extra line in PythonMatplotlib 累积频率图与 Python 中的额外线
【发布时间】:2016-08-06 08:37:40
【问题描述】:

当我运行我的代码来生成数据集的相对累积频率的图表时,我的图表会在图表与右侧的线 y=1 like this one 相交处出现一条直线。

y 轴限制在y=0y=1 的范围内,代表累积频率的0% 到100%,一旦图表达到y=1 或100%,它应该 em> 在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)

谢谢,马克斯

【问题讨论】:

    标签: python matplotlib graph


    【解决方案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()
    

    【讨论】:

    • 当我运行你放的代码时,我在这一行得到一个错误:>>> plt.hist(x,normed=1,bins=bins,histtype='step',cumulative=1),导致这条消息:UnboundLocalError: local variable 'ymin' referenced before assignment
    • @MaxSamuels 我没有看到任何局部变量 ymin。也许你忘记了 global 某处?什么是 ymin?
    • 哦,对不起,忘记给我的error message添加截图了。
    猜你喜欢
    • 2018-05-05
    • 1970-01-01
    • 1970-01-01
    • 2020-03-28
    • 1970-01-01
    • 2012-10-16
    • 2021-05-04
    • 2021-06-02
    • 1970-01-01
    相关资源
    最近更新 更多