y轴范围显示使用绝对值而不是偏移值

32

我有以下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",有时不是(取决于数据范围?)。
“奇怪地”绘制: Plotted Badly “正确地”绘制: Plotted Correctly
3个回答

48

将useOffset设置为False:

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

9
在 MPL 1.3.0 版本中,这对我不再起作用。它说现在只能使用 ScalarFormatter。因此,这个网址中的简短答案解决了我的问题:https://dev59.com/zXA65IYBdhLWcg3wsw4X - K.-Michael Aye
1
ax.get_yaxis().get_major_formatter().set_useOffset(False) - Klimaat

11

这个答案建议将偏移量作为全局设置禁用,这对我起了作用:

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

我喜欢这个选项。我很少遇到需要使用偏移设置的情况。 - Bernard Esterhuyse

9
您可以使用以下方法:

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

尽管我更喜欢@HYRY的回答。

3
此答案可能会轻易导致完全错误的结果。永远不要在没有固定位置的情况下设置标签!最好使用其他答案中的任何一个。 - ImportanceOfBeingErnest

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接