【问题标题】:Matplotlib histogram, frequency as thousandsMatplotlib 直方图,频率为千
【发布时间】:2011-08-14 21:07:52
【问题描述】:

我在 matplotlib 中绘制了一个直方图,其中包含大约 260,000 个值。

问题在于直方图上的频率轴(y 轴)达到了很高的数字,例如 100,000...我真正想要的是将 y 标签设为数千,而不是例如:

100000

75000

50000

25000

0

拥有这个:

100

75

50

25

0

然后我可以简单地将 y 轴更改为“频率 (000s)”——这样更容易阅读。任何人都知道如何实现这一目标?

【问题讨论】:

    标签: python graph matplotlib histogram


    【解决方案1】:

    使用matplotlib.ticker.FuncFormatter:

    import matplotlib.pyplot as plt
    import matplotlib.ticker as ticker
    import numpy as np
    
    mu, sigma = 100, 15
    x = mu + sigma * np.random.randn(1000000)
    
    fig, ax = plt.subplots()
    n, bins, patches = ax.hist(x, 50, facecolor='green', alpha=0.75)
    
    ax.yaxis.set_major_formatter(ticker.FuncFormatter(
        lambda y, pos: '%.0f' % (y * 1e-3)))
    ax.set_ylabel('Frequency (000s)')
    
    plt.show()
    

    产量

    【讨论】:

    • 是否可以改为 10K、20K、...80K?
    • @weefwefwqg3:将'%.0f'更改为'%.0fK'
    【解决方案2】:

    只需在输入之前自行转换值。在 numpy 中,您可以只使用 array/1000 而不是 array

    【讨论】:

    • 这只是将每个值除以 1000 - 它对频率值没有任何作用(频率将相同,只是 x 轴将除以 1000)
    • @Jonathan 你说得对,我说得太早了。无论如何,unutbu 有一个更好的答案。
    猜你喜欢
    • 1970-01-01
    • 2015-04-26
    • 2012-04-03
    • 2015-10-11
    • 2020-01-27
    • 2013-06-04
    • 2015-04-01
    • 2019-08-21
    • 2013-10-09
    相关资源
    最近更新 更多