首页 > 解决方案 > 用 qllayout 中的另一个 pyqtplot 替换 pyqtplot

问题描述

我正在构建一个根据用户选择基于 2 个不同参数动态填充的绘图布局。

第一个参数是要绘制的变量。此参数会将绘图添加到我的绘图布局中。

第二个参数是我希望在所有参数图中绘制的属性。

在我的代码中,我正在检查是否绘制(或不绘制)所选参数,并在必要时添加新图。

之后,我检查属性是否绘制在这些图形中。

如果未绘制这些属性,我将尝试用所有选定参数的每个变量图形用一个新图替换原始图。

我正在尝试这种方法,因为当我在我的变量图中添加一个新属性时,我正在再次绘制它,因此,我出现了同一属性的多个图例。

如何使用新参数将所选图替换为相同布局位置上的另一个图?

for i in range(1,len(variables)): 
    if variables[i] not in plotted_variables:
        graph = pg.PlotWidget(scroll_Area)
        graph.setMinimumSize(QtCore.QSize(0, 300))
        graph.setTitle(variables[i])

        plotted_variables.append(variables[i])
        vertical_layout.addWidget(graph)
        graphics.append(graph)

        for j in range(1,len(properties)):
            X_dados = np.array([0, 1,2,3,4,5,6,7,8,9,10])
            Y_dados = np.array([0, 1,2,3,4,5,6,7,8,9,10])*j
            graphics[i].plot(X_dados,Y_dados, name=properties[j])
            graphics[i].addLegend()



    else:
        if properties != plotted_prperties:
            new_graph = pg.PlotWidget()
            new_graph.setMinimumSize(QtCore.QSize(0, 300))
            new_graph.setTitle(variables[i])

            # self.plotted_variables[i] = variables[i]
            item =vertical_layout.takeAt(i)
            widget = item.widget()
            vertical_layout.replaceWidget(widget,new_graph)

            # self.graphics.append(new_graph)

            for j in range(1,len(properties)):
                X_dados = np.array([0, 1,2,3,4,5,6,7,8,9,10])
                Y_dados = np.array([0, 1,2,3,4,5,6,7,8,9,10])*j
                graphics[i].plot(X_dados,Y_dados, name=properties[j])
                graphics[i].addLegend()            

标签: pythonpyqt5pyqtgraph

解决方案


我找到了解决我的问题的方法。

这可能不是最好的(我注意到当我们访问大数据库并一次重绘超过 3 个图时它有点慢),但对我有用。如果有人有更好的方法来解决这个问题,我也想看看。

对于我的解决方案,我清除了所有绘制的数据并在给定选定变量和属性的情况下再次创建它。这种方法解决了标签和绘图区域的问题。

colors = ['', 'b', 'g', 'r', 'c', 'm', "y", 'k', 'w']
for i in range(len(graphics)-1,0,-1):
    vertical_layout.itemAt(i).widget().setParent(None)
    graphics.pop(i)

for i in range(len(plotted_variables)-1,0,-1):
    plotted_variables.pop(i)

for i in range(len(plotted_properties)-1,0,-1):
    plotted_properties.pop(i)

# plotar gráficos
for i in range(1,len(variables)): 
    graph = pg.PlotWidget(scroll_Area)
    graph.setMinimumSize(QtCore.QSize(0, 300))
    graph.setTitle(variables[i])

    plotted_variables.append(variables[i])
    vertical_layout.addWidget(graph)
    graphics.append(graph)

    graphics[i].addLegend()
    for j in range(1,len(properties)):
        X_dados = np.array([0, 1,2,3,4,5,6,7,8,9,10])
        Y_dados = np.array([0, 1,2,3,4,5,6,7,8,9,10])*j
        pen = pg.mkPen(color=colors[j])

        graphics[i].plot(X_dados,Y_dados, name=properties[j],pen=pen)

推荐阅读