【问题标题】:python - matplotlib - How to specify the scale for x axis [duplicate]python - matplotlib - 如何指定x轴的比例[重复]
【发布时间】:2012-10-26 18:32:12
【问题描述】:
可能重复:
Python, Matplotlib, subplot: How to set the axis range?
我想为我的散点图指定 x 轴的比例,类似于 excel。
例如,我输入 x 轴值如下:
x_values = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
但是,只出现第一个偶数的数字。
有没有办法让所有数字都出现在 x 轴上?
谢谢,
部分
【问题讨论】:
标签:
python
matplotlib
scale
scatter-plot
【解决方案1】:
您需要matplotlib.pyplot.xticks。在您的示例中:
xticks(x_values)
根据您的导入,在函数调用前加上模块名称。
例如,在ipython --pylab 我输入:
In [1]: x_values = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
In [2]: scatter(x_values, x_values)
Out[2]: <matplotlib.collections.PathCollection at 0x39686d0>
In [3]: xticks(x_values)
得到
请注意,这会在右侧留下一些空间,因为最初每 20 次刻度最多为 120。要在不知道最大值的情况下每 10 次获得一次刻度,您可以两次调用 xticks:
xticks(range(0, int(xticks()[0][-1])+1, 10))