Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 696185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:02:14+00:00 2026-05-14T03:02:14+00:00

I’m drawing a legend on an axes object in matplotlib but the default positioning

  • 0

I’m drawing a legend on an axes object in matplotlib but the default positioning which claims to place it in a smart place doesn’t seem to work. Ideally, I’d like to have the legend be draggable by the user. How can this be done?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T03:02:14+00:00Added an answer on May 14, 2026 at 3:02 am

    Note: This is now built into matplotlib

    leg = plt.legend()
    if leg:
        leg.draggable()
    

    will work as expected


    Well, I found bits and pieces of the solution scattered among mailing lists. I’ve come up with a nice modular chunk of code that you can drop in and use… here it is:

    class DraggableLegend:
        def __init__(self, legend):
            self.legend = legend
            self.gotLegend = False
            legend.figure.canvas.mpl_connect('motion_notify_event', self.on_motion)
            legend.figure.canvas.mpl_connect('pick_event', self.on_pick)
            legend.figure.canvas.mpl_connect('button_release_event', self.on_release)
            legend.set_picker(self.my_legend_picker)
    
        def on_motion(self, evt):
            if self.gotLegend:
                dx = evt.x - self.mouse_x
                dy = evt.y - self.mouse_y
                loc_in_canvas = self.legend_x + dx, self.legend_y + dy
                loc_in_norm_axes = self.legend.parent.transAxes.inverted().transform_point(loc_in_canvas)
                self.legend._loc = tuple(loc_in_norm_axes)
                self.legend.figure.canvas.draw()
    
        def my_legend_picker(self, legend, evt): 
            return self.legend.legendPatch.contains(evt)   
    
        def on_pick(self, evt): 
            if evt.artist == self.legend:
                bbox = self.legend.get_window_extent()
                self.mouse_x = evt.mouseevent.x
                self.mouse_y = evt.mouseevent.y
                self.legend_x = bbox.xmin
                self.legend_y = bbox.ymin 
                self.gotLegend = 1
    
        def on_release(self, event):
            if self.gotLegend:
                self.gotLegend = False
    

    …and in your code…

    def draw(self): 
        ax = self.figure.add_subplot(111)
        scatter = ax.scatter(np.random.randn(100), np.random.randn(100))
    
    
    legend = DraggableLegend(ax.legend())
    

    I emailed the Matplotlib-users group and John Hunter was kind enough to add my solution it to SVN HEAD.

    On Thu, Jan 28, 2010 at 3:02 PM, Adam
    Fraser
    wrote:

    I thought I’d share a solution to the draggable legend problem since
    it took me forever to assimilate all the scattered knowledge on the
    mailing lists…

    Cool — nice example. I added the code to
    legend.py. Now you can do

    leg = ax.legend()
    leg.draggable()

    to enable draggable mode. You can
    repeatedly call this func to toggle
    the draggable state.

    I hope this is helpful to people working with matplotlib.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to clean up various Word 'smart' characters in user input, including but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.