【问题标题】:How to set "step" on axis X in my figure in matplotlib python 2.6.6?如何在 matplotlib python 2.6.6 中的我的图中在 X 轴上设置“步长”?
【发布时间】:2012-06-06 01:42:55
【问题描述】:

我有一些代码:

#!/usr/bin/env python

import matplotlib
matplotlib.use("Agg")       
import matplotlib.pyplot as plt

x = [1,2,3,4,5]
y = [1.2,1.9,3.1,4.2,4.8]

plt.plot(x,y)
plt.xlabel('OX')
plt.ylabel('OY')
plt.savefig('figure1.png')
plt.close()

它给了我这个数字:my figure

如您所见,X 轴上的“步长”为 0.5,但我想将其设置为 1。如何制作?

当我使用 plt.xticks(1) 时,它给了我错误:

Traceback(最近一次调用最后一次):文件“overflow.py”,第 13 行,在 plt.xticks(1) 文件“/usr/lib/pymodules/python2.6/matplotlib/pyplot.py”,第 998 行,在 xticks locs = ax.set_xticks(args[0]) 文件“/usr/lib/pymodules/python2.6/matplotlib/axes.py”,第 2064 行,在 set_xticks 返回 self.xaxis.set_ticks(ticks, minor=minor) 文件“/usr/lib/pymodules/python2.6/matplotlib/axis.py”,第 1150 行,在 set_ticks if len(ticks) > 1: TypeError: object of type 'int' has no len()

我在 Ubuntu 10.10 上使用 Python 2.6.6 ....

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:
    plt.xticks([1, 2, 3, 4, 5])
    

    xticks documentation.

    【讨论】:

    • 哇,谢谢,它工作得很好:)我不知道我怎么会错过这个;)再次感谢!
    • @mazix :如果这个答案解决了你的问题,你应该随时接受它(meta.stackexchange.com/questions/5234/…
    • @mgilson:非常感谢你,我刚刚接受了史蒂夫的回答:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-23
    • 2016-03-19
    • 1970-01-01
    • 2018-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多