在 Matplotlib 中定位 y 值 - Matplotlib 值不遵循刻度定位

Positioning of y values in Matplotlib - Matplotlib values don't follow tick positioning

我试图找到解决方案,但不幸的是,直到现在都没有奏效。

我有这个代码示例,其中值非常接近两侧的 y 轴,我想将它们稍微移动到中心。两个报价应该是相互比较的。

from matplotlib import pyplot as plt

y = {}
x = ["a170", "b300"]
y["OlC"] = [1,2]
y["CSD"] = [3,4]
col=["b", "r"]
mark=[".","*"]
Fig, axs = plt.subplots(figsize=(3.25,3.25))
for i, meth in enumerate(y):
    axs.plot(x, y[meth], color = col[i], marker = mark[i], linestyle='None', label=meth) 

我尝试使用以下代码移动 y 刻度,但它只移动了刻度。

axs.set_xticks([0.25,0.75])

有没有办法移动值,例如一个从左起 25%,另一个也是 75%,与刻度相同?

谢谢!

enter image description here

您可以先使用 x 轴上的数字进行绘图,然后设置限制、设置所需的刻度,最后设置刻度标签。您可以在 axs.set_xlim(-1, 2) 中选择自己喜欢的值,具体取决于您希望刻度的距离有多近。

for i, meth in enumerate(y):
    axs.plot(range(len(x)), y[meth], color = col[i], marker = mark[i], linestyle='None', label=meth) 

axs.set_xlim(-1, 2)
axs.set_xticks([0, 1])
axs.set_xticklabels(x)
axs.legend()