gpt4 book ai didi

python - PyQtGraph 堆积图方法

转载 作者:太空宇宙 更新时间:2023-11-04 05:42:58 25 4
gpt4 key购买 nike

我想创建一个小部件,在彼此之上显示多个时间信号。这些信号具有相同的时间轴但不同的值轴。时间轴应该是自定义类型。时间轴应该是链接在一起的。

整个小部件将显示在 QHBox 布局中,紧挨着 QListWidget,带有一个项目,每个信号都有一个复选框,使用户能够启用或禁用各个绘图。

我目前有一个半工作的解决方案,但我想我在概念层面上做错了。我无法让值轴的 autoSIprefix 和标签正常工作,并且出现了不应该出现的奇怪的 float 轴。

我当前的小部件在左侧有 float 轴并且缺少值轴标签:

My current widget with floating axes on to left and missing value axis labels

class MultiPlotWidget(QWidget):
def __init__(self, parent, data_frame):
super().__init__(parent)

# Data
self._df = data_frame
self._grouped_data = self._grouped_data()
# everything is enabled by default
self._enabled_parameters = set(self._grouped_data.keys())
self._parameter_name_map = lambda name: name

self._plot_items = self._make_plot_items(self._grouped_data)

# Plot widget
self._fig_sa = QScrollArea(self)
self._fig = pg.GraphicsLayoutWidget(self._fig_sa)
self._fig.setMinimumSize(QSize(400, len(self._enabled_parameters) * 200))
self._fig_sa.setWidgetResizable(True)
self._fig_sa.setWidget(self._fig)

# Side widget
self._sw = SideWidget(self, sorted(self._enabled_parameters))
self._sw.parameter_state_change.connect(self._parameter_state_change_action)

# Layout
h_layout = QHBoxLayout()
h_layout.addWidget(self._fig_sa)
h_layout.addWidget(self._sw)

self.setLayout(h_layout)

self.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum)

def data(self):
return self._df

def set_parameter_name_map(self, pnm):
self._parameter_name_map = pnm
self._plot_items = self._make_plot_items(self._grouped_data)
self._sw.set_parameter_name_map(pnm)

def _grouped_data(self):
"""Regroup columns of self._df into a dict with groups with common
name but different suffix e.g. self._df has columns ["foo/a",
"foo/b", "foo/c"] then this function returns a dict {"foo": {"a": column
foo/a , "b": column foo/b, "c": column foo/c}}

"""
grouped = {}
for col in self._df.columns:
parts = col
name = col
suffix = DES
if len(parts) == 2:
name, suffix = parts
if name not in grouped: grouped[name] = {}
grouped[name].update({suffix: self._df[col].as_matrix()})

return grouped

def _make_plot_items(self, grouped_data):
plot_items = {}

last_vb = None
for group_name in self._enabled_parameters:
group_cols = grouped_data[group_name]
ts_name = self._parameter_name_map(group_name)

date_axis = DateAxisItem(self._df.index.to_series(), "bottom")

pi = pg.PlotItem(axisItems={"bottom": date_axis# , "left": value_axis
})
pi.setLabel("left", ts_name, unit="m")

vb = pi.getViewBox()

pi.setTitle(ts_name)
pi.showGrid(x=True, y=True, alpha=0.5)
value_axis = pi.getAxis("left")
value_axis.enableAutoSIPrefix()
value_axis.setLabel(text=ts_name, unit="m")
value_axis.showLabel(show=True)

colormap = {DES: pg.hsvColor(0, 0, 1),
ADV: pg.hsvColor(0, 1, 1),
FAV: pg.hsvColor(0.33, 1, 1)}
for suffix, col in group_cols.items():
vb.setLimits(xMin=0, xMax=len(col)-1)
pi.plot(col, name=suffix, antialias=True,
pen={"color": colormap.get(suffix, colormap[DES])})

# # Only show legend if more than one curve per plot is
# # present
# if len(group_cols.items()) > 1:
# pi.addLegend()

if last_vb is not None: # Link all time axes together
last_vb.setXLink(vb)
vb.setXLink(last_vb)
last_vb = vb

plot_items[group_name] = pi

return plot_items

def draw(self):
self._fig.setMinimumSize(QSize(400, len(self._enabled_parameters) * 220))

# Remove all items
row = 0
while True:
try:
item = self._fig.getItem(row, 0)
# for child_item in item.allChildItems():
# item.removeItem(child_item)
self._fig.removeItem(item)
row += 1
except Exception: # Thats exactly what pyqtgraph throws
break

# Insert the enabled plot items
self._fig.clear()
for group_name in sorted(self._enabled_parameters):
self._fig.addItem(self._plot_items[group_name])
self._fig.nextRow()

def _parameter_state_change_action(self, tpl):
"""Handles enabling and disabling the display of time series when they
are ticked or unticked in the side widget.

"""
p_name, p_enabled = tpl
if p_enabled:
self._enabled_parameters.add(p_name)
else:
self._enabled_parameters.remove(p_name)

self.draw()

我仔细查看了文档和示例,但没有找到合适的内容。你能给我一个提示,告诉我如何以“正确”的方式实现我想要的功能吗?非常感谢!

最佳答案

1) float 轴 -

这是 PlotItem.py (PyQtGraph 0.9.10) 中的已知错误,将在以后的版本中修复。固定代码已经在 GitHub 上可用 https://github.com/pyqtgraph/pyqtgraph/blob/develop/pyqtgraph/graphicsItems/PlotItem/PlotItem.py .拉取存储库并重新安装为我修复了它。

2) SI 单位 -

您可以使用 setLabels() 方法将 AutoUpdating SI 单位添加到 PlotItem。

yourPlotItem.setLabels(left = ('Amplitude','V'))

这会将“V”设置为左轴的单位,并且会根据缩放比例更改为 mV、kV 等。

关于python - PyQtGraph 堆积图方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33240897/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com