'Add/Remove numerous plots on a PlotWidget in Pyqtgraph

I have a plotWidget (self.widget0) from pyqtgraph on the pyqt GUI. I want to add 200 plots at this widget. What I can do is add each plot one by one:

self.plot0 = self.widget0.plot(xx0, yy0) 
self.plot1 = self.widget0.plot(xx1, yy1) 
...
self.plot199 = self.widget0.plot(xx199, yy199) 

Where xx0, xx1,... and yyo, yy1... are all 1D numpy arrays of the plot.

For this case, I can update the specified plots later but keep all others, for example if I want to update the 100th plot:

self.widget0.removeItem(self.plot99)
self.plot99 = self.widget0.plot(xx99_new, yy99_new) 

My question is adding those 200 lines in to self.plot0, self.plot1, self.plot2, self.plot3, ... are so inefficient and difficult. Can anyone advise a way to code this using loop or dictionary?



Solution 1:[1]

A dict of plots would suffice, you want a dict so that when an element is removed, the order isn't lost:

self.plots = {}
for plot_num in range(200):
    self.plots[plot_num] = self.widget0.plot(xx[plot_num], yy[plot_num])

self.widget0.removeItem(self.plots[99])
self.plots[99] = self.widget0.plot(xx99_new, yy99_new)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 misantroop