Pyqtgraph颜色图中的特定区域

时间:2020-04-24 12:03:23

标签: python python-3.x pyqt5 pyqtgraph

到目前为止,我一直在使用matplotlib.collections,用不同的颜色标记一个集合非常简单。

我当前的项目要求我使用pyqtgraph来执行相同的操作。

self.plot = pg.PlotWidget()
layout.addWidget(self.plot)

如果bool数组的索引i为true,则必须对相应的float索引i进行着色(垂直或水平横断条)。

示例:

y = [50, 100, 50, 250, 150]
x = [1, 5, 87, 92, 106]
b = [False, False, True, False, True]

在绘制87和106之后,应通过x轴上的垂直条用一些颜色或标记突出显示。有提示吗?

1 个答案:

答案 0 :(得分:1)

对于您所想到的可视化,我还不太清楚,但是这里有一些标记您选择的点的选项:

  1. 使用pg.VTickGroup的单个实例沿x轴绘制刻度线
  2. 使用pg.InfiniteLine的许多实例来绘制线条和其他标记形状

示例:

import pyqtgraph as pg
import numpy as np

plt = pg.plot()

y = [50, 100, 50, 250, 150]
x = [1, 5, 87, 92, 106]
b = [False, False, True, False, True]

# draw a scatter plot with selected points in yellow
cmap = {False: (0, 0, 200), True: (255, 255, 0)}
brushes = [pg.mkBrush(cmap[x]) for x in b]
plt.plot(x, y, pen=None, symbol='o', symbolBrush=brushes)

# draw vertical ticks marking the position of selected points
tick_x = np.array(x)[b]
ticks = pg.VTickGroup(tick_x, yrange=[0, 0.1], pen={'color': 'w', 'width': 5})
plt.addItem(ticks)

# add a vertical line with marker at the bottom for each selected point
for tx in tick_x:
    l = plt.addLine(x=tx, pen=(50, 150, 50), markers=[('^', 0, 10)])