【问题标题】:Positioning of y values in Matplotlib - Matplotlib values don't follow tick positioningMatplotlib 中 y 值的定位 - Matplotlib 值不遵循刻度定位
【发布时间】:2020-06-09 18:38:31
【问题描述】:

我试图找到一个解决方案,但不幸的是直到现在都没有任何效果。

我有这个代码示例,其中值非常接近两侧的 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

【问题讨论】:

    标签: python matplotlib position xvalue xticks


    【解决方案1】:

    您可以首先使用 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()
    

    【讨论】:

    • 非常感谢!它适用于 axs.set_xlim(-1, 2) 的一些变体!!!!
    猜你喜欢
    • 1970-01-01
    • 2019-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-20
    • 2011-09-23
    • 2021-10-26
    相关资源
    最近更新 更多