Matplotlib中有太多数据,虚线似乎是直线

2024-10-02 06:36:12 发布

您现在位置:Python中文网/ 问答频道 /正文

如下图所示 在子图(2,1,1)中,线型为“-”,但看起来像一条直线。 在子图(2,1,2)中,线型是“:”,看起来更好,但也有同样的问题。 我怎样才能得到一条简洁的虚线? enter image description here

plt.subplot(2, 1, 1)
plt.plot(df['START_DATETIME'],df['RESULT_DESC'],'o')
plt.plot(df['START_DATETIME'],(xbar,) * num)
plt.plot(df['START_DATETIME'],((UCL,) * num),'--')
plt.plot(df['START_DATETIME'],((LCL,) * num),'--')

plt.subplot(2, 1, 2)
plt.plot(df['START_DATETIME'],df['range'],'o')
plt.plot(df['START_DATETIME'],(rbar,) * num) 
plt.plot(df['START_DATETIME'],((UCLR,) * num),':')
plt.axhline(y=UCLR, xmin=starttime, xmax=stoptime)
plt.plot(df['START_DATETIME'],((0,)*num),':')

Tags: dfdatetimeplotpltresultstartnumdesc
1条回答
网友
1楼 · 发布于 2024-10-02 06:36:12

我认为问题是你有太多未排序的点。你的代码在绘制很多直线,它们各自都是虚线/虚线,但一条在另一条上看起来更像一条直线(我不知道为什么不是一条直线,但我猜是因为matplotlib内部并不总是从起始点开始,以节省一些时间-否则绘制像这种情况下成千上万的线将永远)。你知道吗

因为你只是画一条直线,所以解决方法是把输入限制在两点,第一点和最后一点。没有你的数据,我用随机数据复制了它,见下:

从matplotlib

import pyplot as plt
import numpy as np
x = np.random.rand(10000)
plt.plot(x,[1]*len(x), ' ')
plt.text(0, 0.9, 'Wrong - too many points')
plt.plot([np.min(x), np.max(x)], [0]*2, ' ')
plt.text(0, 0.1, 'Correct - only two points')

输出:

enter image description here

相关问题 更多 >

    热门问题