I'm using pyplot to display a line graph of up to 30 lines. I would like to add a way to quickly show and hide individual lines on the graph. Pyplot does have a menu where you can edit line properties to change the color or style, but its rather clunky when you want to hide lines to isolate the one you're interested in. Ideally, I'd like to use checkboxes on the legend to show and hide lines. (Similar to showing and hiding layers in image editors like Paint.Net) I'm not sure if this is possible with pyplot, so I am open to other modules as long as they're somewhat easy to distribute.
Python – Hiding lines after showing a pyplot figure
matplotlibpython
Related Topic
- Python – List changes unexpectedly after assignment. Why is this and how can I prevent it
- Python – Why is reading lines from stdin much slower in C++ than Python
- Python – How to set the figure title and axes labels font size in Matplotlib
- Python – How to change the figure size with subplots
- Matplotlib – pyplot scatter plot marker size
- Python – Adding a legend to PyPlot in Matplotlib in the simplest manner possible
- Python – “UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.” when plotting figure with pyplot on Pycharm
Best Answer
If you'd like, you can hook up a callback to the legend that will show/hide lines when they're clicked. There's a simple example here: http://matplotlib.org/examples/event_handling/legend_picking.html
Here's a "fancier" example that should work without needing to manually specify the relationship of the lines and legend markers (Also has a few more features).
(Updated version in August 2019, as a response to repeated reports about this not working correctly; now it should! For the old version see version history)
This allows you to click on legend items to toggle their corresponding artists on/off. For example, you can go from this:
To this: