Home > Net >  pyqtgraph: completely clear PlotWidget
pyqtgraph: completely clear PlotWidget

Time:02-21

I am basically using the code from this example: https://github.com/pyqtgraph/pyqtgraph/blob/develop/examples/MultiplePlotAxes.py but I am adding and removing the additional axes during runtime. Before adding multiple axes to my code I used pw.clear to clear the PlotWidget, but that only clears Items on axis 1. How can I completely reset the PlotWidget (including the legend)? (Without closing it and creating a new one - if possible)

CodePudding user response:

I was answering Your previous question, so I will just add code snippet to do what You want. Again, pw.clear() would work just fine, but You have this special case with directly using scene(). So again, You have to do things manually.

To clear all plots and legend, You can use:

for plot_item in [legend, p1, p2, p3]:
    plot_item.clear()

You mentioned, that You are adding and removing plots dynamically. In that case, I would create a list of active plots with reset function. Then You can just add or remove curves from this list and always have consistent method to clear them all.

For example:

active_plot_items = [legend, p1, p2, p3]

def clear_plot_items():
    for plot_item in active_plot_items:
        plot_item.clear()

clear_plot_items()
  • Related