小能豆

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

py

fmEQH.png

我想使用 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 绘制给定图形的蓝线?


阅读 12

收藏
2024-11-10

共1个答案

小能豆

示例图上的缩放比例有点奇怪,但您可以通过绘制每个 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()

YSof0.png

2024-11-10