繁体   English   中英

plt.yticks 无法正常工作 - matplotlib

[英]plt.yticks not working properly - matplotlib

I'm working on a project in unity and from a C# script I'm calling a Python script to plot some values on a graph using matplotlib , everything works fine except for the yticks on the graph. 将要绘制的值是 0.5 和 5.5 之间的浮点数,这是供参考的图表: 图形

我为设置 yticks 所做的工作如下:

plt.plot(x,y)
plt.yticks([0, 2, 4, 5])
plt.xlabel("frame")
plt.ylabel("distance")
plt.show()

如您所见,它没有在指定位置[0, 2, 4, 5]显示刻度,而是在 0.5 附近显示 4 个刻度。 看起来无论我将什么数组传递给yticks() ,它只需要它的len()并放置确切的刻度数,但是在错误的地方,我猜 y 轴的前 n 个值

Python 版本:3.8.8,Matplotlib 版本:3.4.1

非常感谢任何愿意提供帮助的人。

编辑:最小的可重现示例。 上图是使用 600 个值生成的,在下面的示例中仅使用了 50 个值,因此图形有所不同,但 ytick 问题仍然存在。

import matplotlib.pyplot as plt 

y = ['0.5356', '0.5356', '0.5615', '0.5509', '0.4958', '0.4944', '0.5056', '0.5512', '0.6314', 
     '0.7394', '0.8761', '1.0320', '1.2066', '1.3692', '1.5456', '1.7262', '1.9124', '2.0955',
     '2.2262', '2.2424', '2.2363', '2.2285', '2.1811', '2.1159', '2.0400', '1.9590', '1.8685',
     '1.7614', '1.6517', '1.5322', '1.3897', '1.2642', '1.1080', '0.9256', '0.8161', '0.6624',
     '0.5240', '0.3763', '0.2361', '0.1649', '0.2151', '0.3441', '0.5065', '0.6785', '0.8355',
     '1.0118', '1.1708', '1.3253', '1.4888', '1.6247']
x = [i for i in range(1, 51)]

# plotting
plt.plot(x,y)
plt.yticks([0, 2, 4, 5])
plt.xlabel("frame")
plt.ylabel("distance")
plt.show()

图应该是这样的:

50帧

好吧,我是个白痴。 像往常一样,这是我做过的最愚蠢的事情。 值不是浮点数,而是字符串。

您可以像这样显式设置滴答频率:

import matplotlib.pyplot as plt 

y = ['0.5356', '0.5356', '0.5615', '0.5509', '0.4958', '0.4944', '0.5056', '0.5512', '0.6314', 
     '0.7394', '0.8761', '1.0320', '1.2066', '1.3692', '1.5456', '1.7262', '1.9124', '2.0955',
     '2.2262', '2.2424', '2.2363', '2.2285', '2.1811', '2.1159', '2.0400', '1.9590', '1.8685',
     '1.7614', '1.6517', '1.5322', '1.3897', '1.2642', '1.1080', '0.9256', '0.8161', '0.6624',
     '0.5240', '0.3763', '0.2361', '0.1649', '0.2151', '0.3441', '0.5065', '0.6785', '0.8355',
     '1.0118', '1.1708', '1.3253', '1.4888', '1.6247']
y = [float(i) for i in y]
x = [i for i in range(1, 51)]

# plotting
plt.plot(x,y)
plt.yticks(np.arange(min(y), max(y)+1, 1.0))
plt.xlabel("frame")
plt.ylabel("distance")
plt.show()

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM