有没有办法重现 matplotlib 的drawstyle='mid' pyqtgraph 中的功能?

发布于 2025-01-15 03:17:38 字数 1046 浏览 3 评论 0原文

我试图重现 此处 找到的 matplotlib 示例。 我知道这个问题,但它没有不包括 drawstyle='mid' 的情况 我认为 stepMode='center' 可以解决问题,但我似乎无法获得与 drawstyle='mid' 相同的行为。 有什么帮助吗?有可能吗?

import pyqtgraph as pg
import numpy as np

x = np.arange(14)
y = np.sin(x / 2)


plt = pg.plot(x, y+2, pen='blue', stepMode='left')
plt.addItem(pg.PlotDataItem(x, y+2, pen='grey', symbol='o'))

plt.addItem(pg.PlotDataItem(x, y[:-1]+1, pen='orange', stepMode='center'))
plt.addItem(pg.PlotDataItem(x, y+1, pen='grey', symbol='o'))

plt.addItem(pg.PlotDataItem(x, y, pen='green', stepMode='right'))
plt.addItem(pg.PlotDataItem(x, y, pen='grey', symbol='o'))

输入图片此处描述

I'm trying to reproduce the example of matplotlib found here.
I'm aware of this question but it doesn't cover the case for drawstyle='mid'
I assumed stepMode='center' would do the trick but I can't seem to get the same behavior as drawstyle='mid'.
Any help? Is it even possible?

import pyqtgraph as pg
import numpy as np

x = np.arange(14)
y = np.sin(x / 2)


plt = pg.plot(x, y+2, pen='blue', stepMode='left')
plt.addItem(pg.PlotDataItem(x, y+2, pen='grey', symbol='o'))

plt.addItem(pg.PlotDataItem(x, y[:-1]+1, pen='orange', stepMode='center'))
plt.addItem(pg.PlotDataItem(x, y+1, pen='grey', symbol='o'))

plt.addItem(pg.PlotDataItem(x, y, pen='green', stepMode='right'))
plt.addItem(pg.PlotDataItem(x, y, pen='grey', symbol='o'))

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嗳卜坏 2025-01-22 03:17:38

我没有找到像 matplotlib 中那样的隐式方法,但如果有人感兴趣,这里有一个修复方法。

import pyqtgraph as pg
import numpy as np

x = np.arange(14)
y = np.sin(x / 2)

dx = x[1] - x[0]
# Define a new x slightly offset by dx/2
# you have to go to 2*dx to get the right side edge of the final bin
# because len(x2) needs to be len(y)+1 with stepMode='center'
x2 = np.arange(0, x[-1]+2*dx, dx) - dx/2

plt = pg.plot(x, y+2, pen='blue', stepMode='left')
plt.addItem(pg.PlotDataItem(x, y+2, pen='grey', symbol='o'))

plt.addItem(pg.PlotDataItem(x2, y+1, pen='orange', stepMode='center'))
plt.addItem(pg.PlotDataItem(x, y+1, pen='grey', symbol='o'))

plt.addItem(pg.PlotDataItem(x, y, pen='green', stepMode='right'))
plt.addItem(pg.PlotDataItem(x, y, pen='grey', symbol='o'))

这会产生这样的结果:
img_ Correct

I didn't find an implicit way of doing this like in matplotlib but here is a fix if anyone is interested.

import pyqtgraph as pg
import numpy as np

x = np.arange(14)
y = np.sin(x / 2)

dx = x[1] - x[0]
# Define a new x slightly offset by dx/2
# you have to go to 2*dx to get the right side edge of the final bin
# because len(x2) needs to be len(y)+1 with stepMode='center'
x2 = np.arange(0, x[-1]+2*dx, dx) - dx/2

plt = pg.plot(x, y+2, pen='blue', stepMode='left')
plt.addItem(pg.PlotDataItem(x, y+2, pen='grey', symbol='o'))

plt.addItem(pg.PlotDataItem(x2, y+1, pen='orange', stepMode='center'))
plt.addItem(pg.PlotDataItem(x, y+1, pen='grey', symbol='o'))

plt.addItem(pg.PlotDataItem(x, y, pen='green', stepMode='right'))
plt.addItem(pg.PlotDataItem(x, y, pen='grey', symbol='o'))

Which yields this:
img_correct

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文