Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Automatic Legend Placement Outside of Figures and Axes #3745

Closed
Void2258 opened this issue Oct 31, 2014 · 9 comments
Closed
Milestone

Comments

@Void2258
Copy link

The need to place a legend outside of the axes is very common, particularly if graphing unpredictable data. It is also an issue when data is dense enough that there is no clear space on the graph to place the legend. While we have a simple system for positioning the legend within the axes, positioning them outside the axes is an exercise in frustration.

Currently, in order to place a legend outside of a figure, a complicated manual positioning procedure is needed, involving manipulation of bbox_to_anchor as well as axes and figure size and positioning. This method requires extensive trial and error. Further, this manual work must be repeated anytime the graph size or shape must be changed. On a large number of projects, I personally have spent more time getting the legend and the graph sizing to cooperate when I must place the legend outside than on the rest of the project COMBINED (often requiring the writing of separate code just to test this without having to run the rest of the project over and over), and I have heard similar stories from many other people.

To place a legend inside the axes we have a simple location system wherein we simply set the loc parameter, which requires only so much trial and error as looking at a couple different locations at most, and requires no added work on changing the size and shape of the graph. Can this system be extended to allow for automatic positioning of the legend outside the axes?

@WeatherGod
Copy link
Member

I admit this should be much better, but one thing that can alleviate the
pain a bit is to do the bbox_to_anchor trick as normal, but then just
simply pass "bbox_inches='tight'" to the savefig command. The figure size
will automatically be adjusted to take into account the legend that spilled
outside the axes.

Note, this isn't a full solution because there are plenty of times when
bbox_inches='tight' is not appropriate, but it has worked well for me in
the past.

On Fri, Oct 31, 2014 at 2:56 PM, Void2258 notifications@github.com wrote:

The need to place a legend outside of the axes is very common,
particularly if graphing unpredictable data. It is also an issue when data
is dense enough that there is no clear space on the graph to place the
legend. Currently, while we have a simple system for positioning the legend
within the axes, positioning them outside the axes is an exercise in
frustration.

Currently, in order to place a legend outside of a figure, a complicated
manual positioning procedure is needed, involving manipulation of
bbox_to_anchor as well as axes and figure size and positioning. This
method requires extensive trial and error. Further, this manual work must
be repeated anytime the graph size or shape must be changed. On a large
number of projects, I personally have spent more time getting the legend
and the graph sizing to cooperate when I must place the legend outside than
on the rest of the project COMBINED (often requiring the writing of
separate code just to test this without having to run the rest of the
project over and over), and I have heard similar stories from many other
people.

To place a legend inside the axes we have a simple location system wherein
we simply set the loc parameter, which requires only so much trial and
error as looking at a couple different locations at most, and requires no
added work on changing the size and shape of the graph. Can this system be
extended to allow for automatic positioning of the legend outside the axes?


Reply to this email directly or view it on GitHub
#3745.

@Void2258
Copy link
Author

Void2258 commented Nov 6, 2014

What it needs is a fully integrated solution that doesn't require trial and error to place the legend. Trial and error is not always feasible, leading to the aforementioned times when you have to make separate programs, possibly quite complicated ones, in order to test the graphing separately from the rest of your project just for this. I have been in situations where projects fell behind by days due to this issue. It is a fairly severe issue, if one that is limited in scope.

@tacaswell tacaswell added this to the v1.5.x milestone Nov 6, 2014
@tacaswell
Copy link
Member

This ties back to using a linear constraint solver to deal with tight layout.

@WeatherGod
Copy link
Member

I absolutely agree with you that matplotlib needs a fully integrated
solution to this problem, but how it would be implemented is not entirely
clear. A figure starts off with a given size upon creation, and there are
only a few ways to change that size once it is created. The "bbox_inches"
option is one of them that can be applied at the savefig step (so, it
doesn't help for those doing show()). The only automated way of adjusting
the figure size is when that parameter is set to "tight".

Have you tried using this option for your purposes? I would be interested
in how it fall short of your needs in order to better understand how we
could solve this issue.

On Thu, Nov 6, 2014 at 5:37 PM, Void2258 notifications@github.com wrote:

What it needs is a fully integrated solution that doesn't require trial
and error to place the legend. Trial and error is not always feasible,
leading to the aforementioned times when you have to make separate
programs, possibly quite complicated ones, in order to test the graphing
separately from the rest of your project just for this. I have been in
situations where projects fell behind by days due to this issue. It is a
fairly severe issue, if one that is limited in scope.


Reply to this email directly or view it on GitHub
#3745 (comment)
.

@Tillsten
Copy link
Contributor

Tillsten commented Nov 7, 2014

Fully agree, while playing around with the constrained solver the same problem arises: which sizes are actually fixed and which are not? Is the sizes of the axis fixed or the size of figure? What if there are multiple axes and I insert a legend beside one of the axes, do I change the size of one axes, both axes or the figure? Note that all are possible correct solutions, depending on the user.

@languitar
Copy link
Contributor

👍

@story645
Copy link
Member

story645 commented Jul 1, 2016

related to #3857?

@tacaswell tacaswell modified the milestones: 2.1 (next point release), 2.2 (next next feature release) Oct 3, 2017
@github-actions
Copy link

github-actions bot commented Mar 7, 2023

This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

@github-actions github-actions bot added the status: inactive Marked by the “Stale” Github Action label Mar 7, 2023
@jklymak
Copy link
Member

jklymak commented Mar 7, 2023

I think this is largely met by #19743, so let's close and open new issues if the feature is not all the way there...

@jklymak jklymak closed this as completed Mar 7, 2023
@QuLogic QuLogic modified the milestones: future releases, v3.7.0 Mar 7, 2023
@QuLogic QuLogic removed the status: inactive Marked by the “Stale” Github Action label Mar 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants