Interactive selection of series in a matplotlib plot

前端 未结 3 588
梦谈多话
梦谈多话 2020-12-31 13:43

I have been looking for a way to be able to select which series are visible on a plot, after a plot is created.

I need this as i often have plots with many series. t

3条回答
  •  南方客
    南方客 (楼主)
    2020-12-31 14:06

    A list with checkboxes will be fine if you have a few plots or less, but for more plots a popup menu would probably be better. I am not sure whether either of these is possible with matplotlib though.

    The way I implemented this once was to use a slider to select the plot from a list - basically you use the slider to set the index of the series that should be shown. I had a few hundred series per dataset, so it was a good way to quickly glance through them.

    My code for setting this up was roughly like this:

    fig = pyplot.figure()
    slax = self.fig.add_axes((0.1,0.05,0.35,0.05))
    sl = matplotlib.widgets.Slider(slax, "Trace #", 0, len(plotlist), valinit=0.0)
    def update_trace():
        ax.clear()
        tracenum = int(np.floor(sl.val))
        ax.plot(plotlist[tracenum])
        fig.canvas.draw()
    sl.on_changed(update_trace)
    ax = self.fig.add_axes((0.6, 0.2, 0.35, 0.7))
    fig.add_subplot(axes=self.traceax)
    update_trace()
    

    Here's an example:

    Plot image

提交回复
热议问题