用qllayout中的另一个pyqtplot替换pyqtplot

问题描述 投票:0回答:1

我正在建立一个基于两个不同参数根据用户选择动态填充的图版面。

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

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

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

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

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

我正在尝试这种方法,因为当我在变量图中添加新属性时,我再次对其进行了绘制,因此,对于同一属性,它会显示多个图例。

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

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()            
python pyqt5 pyqtgraph
1个回答
0
投票

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

这可能不是最好的,但是对我有用。如果有人有更好的方法来解决此问题,我也希望看到它。

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

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)
© www.soinside.com 2019 - 2024. All rights reserved.