如何在pyplot中缩放xtick值轴

时间:2016-05-18 15:53:58

标签: python pandas matplotlib

给出以下代码:

import matplotlib.pyplot as plt
import numpy as np

y=np.linspace(0,135000, 135001)
t=range(-len(y),0)

plt.plot(t,y)

enter image description here 我如何随后缩放格式或替换xtick值,使它们读取-140,-130,-120,110 ... 0?

1 个答案:

答案 0 :(得分:2)

使用matplotlib.ticker.FuncFormattersee docs):

plt.gca().get_xaxis().set_major_formatter(FuncFormatter(lambda x, p: format(int(x/1000), ',')))
plt.show()

enter image description here