Matplotlib显示错误的y轴值

时间:2019-10-17 16:05:22

标签: python python-3.x matplotlib

我的代码很简单:

values = [-2071238, -2071241, -2071240, -2071242, -2071244, -2071239, -2071221, -2071194, -2071224, -2071240, -2071244, -2071241, -2071240, -2071241, -2071237, -2071223, -2071205, -2071225, -2071238]
indx = [0.0, 20.0, 40.0, 60.0, 80.0, 100.0, 120.0, 140.0, 160.0, 180.0, 200.0, 220.0, 240.0, 260.0, 280.0, 300.0, 320.0, 340.0, 360.0]

plt.scatter(indx, values)

#rendering
plt.xlabel("Axis 1")
plt.ylabel("Axis 2")
title = "All"
plt.title(title)
plt.savefig(title + ".png")
plt.show()

但是,结果如下:

picture

显然每个点的y值都不好。

我做错了什么还是忘记了什么?

1 个答案:

答案 0 :(得分:1)

您的图形在y轴上确实有很好的值,但是它们有偏移。可以禁用偏移量:

import matplotlib.pyplot as plt

values = [-2071238, -2071241, -2071240, -2071242, -2071244, -2071239, -2071221, -2071194, -2071224, -2071240, -2071244, -2071241, -2071240, -2071241, -2071237, -2071223, -2071205, -2071225, -2071238]
indx = [0.0, 20.0, 40.0, 60.0, 80.0, 100.0, 120.0, 140.0, 160.0, 180.0, 200.0, 220.0, 240.0, 260.0, 280.0, 300.0, 320.0, 340.0, 360.0]

plt.scatter(indx, values)

# disabling the offset on y axis
ax = plt.gca()
ax.ticklabel_format(useOffset=False)

#rendering
plt.xlabel("Axis 1")
plt.ylabel("Axis 2")
title = "All"
plt.title(title)
plt.savefig(title + ".png")

plt.show()

enter image description here