如何在matplotlib python中设置x轴值?

问题描述 投票:9回答:1

我想用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()

如何使用matplotlib绘制给定图形的蓝线?

python matplotlib
1个回答
14
投票

您的示例图上的缩放有点奇怪,但您可以通过绘制每个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 = [i for i in range(0, 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()
© www.soinside.com 2019 - 2024. All rights reserved.