matplotlib.pyplot 在相等范围内绘制 x 轴刻度 [英] matplotlib.pyplot plot x-axis ticks in equal range
问题描述
我需要相对于x值序列绘制y值序列.x值在很大的范围内变化.pyplot似乎使用线性x轴.所以下面的代码给了我一个不好的数字.
I need to plot a sequence of y values against a sequence of x values. The x values varies in a very large range. pyplot seems using a linear x-axis. So the following code gives me a bad figure.
def bad_plot():
x=[1,2,10,100,1000]
y=[5,10,6,7,9]
plt.plot(x,y,'rs')
plt.show()
前三个点(1,5),(2,10),(10,6)非常接近.我希望x轴只有5个刻度线[1,2,10,100,100],并且它们均匀地分散在x轴上.我怎样才能做到这一点?非常感谢.
The first three points (1,5), (2,10), (10,6) are so close. I want the x-axis only have 5 ticks [1,2,10,100,100] and they are uniformly scattered in the x-axis. How can I achieve that? Thanks a lot.
推荐答案
为了记录,我认为这是一个糟糕的图形设计选择.以固定间隔出现非规则(线性或对数)刻度会让读者感到困惑.
好的,您只需要做的是针对 0, 1, 2, 3, ...
绘图,然后将刻度设置为 x
处的值相同的位置使用 plt.xticks()
Okay what you simply have to do is plot against 0, 1, 2, 3, ...
but then set the ticks to be your values of x
at the same position using plt.xticks()
import numpy as np
import matplotlib.pyplot as plt
x=[1,2,10,100,1000]
y=[5,10,6,7,9]
N = len(x)
x2 = np.arange(N)
plt.plot(x2, y)
plt.xticks(x2, x)
plt.show()
这篇关于matplotlib.pyplot 在相等范围内绘制 x 轴刻度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!