如何在matplotlib python中设置x轴值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44813601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set x axis values in matplotlib python?
提问by neha
I want to draw this graph using matplotlib. I wrote the code but it's not changing the x axis values.
我想使用 matplotlib 绘制此图。我写了代码,但它没有改变 x 轴的值。
import matplotlib.pyplot as plt
x = [0.00001,0.001,0.01,0.1,0.5,1,5]
y = [0.945,0.885,0.893,0.9,0.996,1.25,1.19]
plt.xlim(0.00001,5)
plt.ylim(0.8,1.4)
plt.plot(x, y, marker='o', linestyle='--', color='r',
label='Square')
plt.xlabel('x')
plt.ylabel('y')
plt.title('compare')
plt.legend()
plt.show()
How I can draw the blue line of the given graph using matplotlib?
如何使用 matplotlib 绘制给定图形的蓝线?
回答by GWW
The scaling on your example figure is a bit strange but you can force it by plotting the index of each x-value and then setting the ticks to the data points:
示例图上的缩放有点奇怪,但您可以通过绘制每个 x 值的索引,然后将刻度设置为数据点来强制它:
import matplotlib.pyplot as plt
x = [0.00001,0.001,0.01,0.1,0.5,1,5]
# create an index for each tick position
xi = list(range(len(x)))
y = [0.945,0.885,0.893,0.9,0.996,1.25,1.19]
plt.ylim(0.8,1.4)
# plot the index for the x-values
plt.plot(xi, y, marker='o', linestyle='--', color='r', label='Square')
plt.xlabel('x')
plt.ylabel('y')
plt.xticks(xi, x)
plt.title('compare')
plt.legend()
plt.show()