- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create a draggable legend in Matplotlib?
To create a draggable legend in matplotlib, we can take the following steps −
Create two lines, line1 and line2, using plot() method.
Place the legend for plot line1 and line2 with ordered lables at location 1, using legend() method.
To create a draggable legend, use set_draggable() method, where state=True. If state=False, then we can't drag the legend.
To display the figure, use show() method.
Example
from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True line1, = plt.plot([1, 2, 3]) line2, = plt.plot([3, 2, 1]) leg = plt.legend([line2, line1], ["line 2", "line 1"], loc=1) leg.set_draggable(state=True) plt.show()
Output
On the output window, you can drag the legend around with the mouse.
- Related Articles
- How to create a legend for a 3D bar in Matplotlib?
- How do you create a legend for a contour plot in Matplotlib?
- Create a draggable paragraph in HTML5
- How to add legend to imshow() in Matplotlib?
- How to create a draggable HTML element with JavaScript and CSS?
- How to add a legend to a Matplotlib pie chart?
- How to show legend elements horizontally in Matplotlib?
- How to change the legend fontname in Matplotlib?
- How to position and align a Matplotlib figure legend?
- Text alignment in a Matplotlib legend
- How to align rows in a Matplotlib legend with 2 columns?
- How to adjust the size of a Matplotlib legend box?
- How to add titles to the legend rows in Matplotlib?
- How to move the legend to outside of a Seaborn scatterplot in Matplotlib?
- How to set legend marker size and alpha in Matplotlib?

Advertisements