【问题标题】:Matplotlib yaxis range display using absolute values rather than offset values?Matplotlib yaxis范围显示使用绝对值而不是偏移值?
【发布时间】:2012-02-16 00:17:22
【问题描述】:

我有以下范围的 numpy 数据(usec 时间戳的增量):

array([ 4.312,  4.317,  4.316,  4.32 ,  4.316,  4.316,  4.319,  4.317,
    4.317,  4.316,  4.318,  4.316,  4.318,  4.316,  4.318,  4.317,
    4.317,  4.317,  4.316,  4.317,  4.318,  4.316,  4.318,  4.316,
    4.318,  4.316,  4.317,  4.317,  4.318,  4.316,  4.317,  4.317,
    4.317,  4.317,  4.317,  4.316,  4.319,  4.315,  4.319,  4.315,
    4.319,  4.315,  4.316,  4.319,  4.317,  4.317,  4.317,  4.318,
    4.315,  4.317,  4.317,  4.317,  4.319,  4.314,  4.32 ,  4.315,
    4.317,  4.318,  4.315,  4.318,  4.317,  4.317,  4.317,  4.316,
    4.317,  4.318,  4.317,  4.317,  4.317,  4.315,  4.319,  4.317,
    4.315,  4.319,  4.316,  4.318,  4.318,  4.315,  4.318,  4.317,
    4.317,  4.321])

当我使用 matplotlib.pyplot 绘图时:

 import matplotlib.pyplot as plt
 plt.plot( deltas ) 
 plt.show()

我得到以下情节。为什么 Y 轴会这样缩放?如何将 Y 轴标记为数据,而不是数据的偏移量?有时情节是“+4.nnn”,有时不是(取决于数据范围?)。

“奇怪地”绘制:

“正确”绘制:

【问题讨论】:

    标签: python numpy matplotlib


    【解决方案1】:

    将 useOffset 设置为 False:

    ax = plt.gca()
    ax.ticklabel_format(useOffset=False)
    

    【讨论】:

    • 这在 MPL 1.3.0 中不再适用于我。它说这现在只适用于 ScalarFormatter。所以这里的简短回答为我解决了这个问题:stackoverflow.com/questions/3677368/…
    • ax.get_yaxis().get_major_formatter().set_useOffset(False)
    【解决方案2】:

    This answer 建议禁用偏移作为全局设置,这对我有用:

    matplotlib.rcParams['axes.formatter.useoffset'] = False
    

    【讨论】:

    • 我喜欢这个选项。我很少遇到我想使用偏移设置的情况。
    【解决方案3】:

    你也可以使用

    ax = plt.gca()
    ax.set_yticklabels(ax.get_yticks())
    

    虽然我更喜欢@HYRY 的回答。

    【讨论】:

    • 这个答案很容易导致完全错误的结果。切勿在未固定位置的情况下设置标签!最好使用任何其他答案。
    猜你喜欢
    • 2012-03-07
    • 2019-10-08
    • 2011-08-15
    • 2013-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多