FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Moving nice legend guide to official docs · Issue #10079 · matplotlib/matplotlib · GitHub

Repository navigation

Moving nice legend guide to official docs #10079

Open

Description

I just found that @ImportanceOfBeingErnest wrote a nice legend guide at https://stackoverflow.com/a/43439132/378080. Perhaps worth moving to the official docs (if he agrees to it)?

Edit: another legend example that I forgot about :-) https://stackoverflow.com/questions/25830780/tabular-legend-layout-for-matplotlib

Activity

  1. ImportanceOfBeingErnest commented on Dec 23, 2017

    Member

    Sure, please use this answer for whatever purpose you can think of as long as it helps people using matplotlib. Same with just any answer I provided at Stackoverflow. (As I see it, SO content is provided under some public license anyways).

  2. anntzer commented on Dec 24, 2017

    ContributorAuthor

    Could you share the script that you used to generate the first annotated plot of that guide? Thanks.

  3. ImportanceOfBeingErnest commented on Jan 11, 2018

    Member

    The script that was used to create the plots in said article is the following.

    import numpy as np; np.random.seed(1) 
    import matplotlib.pyplot as plt
    
    
    x = np.linspace(-2.*np.pi,2*np.pi, num=201)
    y = np.sin(x)
    y2 = np.cos(x)
    y3 = np.sin(x-np.pi/2)
    
    fig, ax= plt.subplots()
    ax.plot(x,y, color="#7aa0c4", label="y=sin(x)")
    ax.plot(x,y2, color="#ca82e1",label="y=cos(x)")
    ax.plot(x,y3, color="#8bcd50",label=r"y=sin(x-$\frac{\pi}{2}$)")
    
    case = -1
    
    if case == -1:
        plt.subplots_adjust(bottom=0.2,right=0.7)
        l1 = plt.legend(bbox_to_anchor=(1.04,1), borderaxespad=0)
        ax.add_artist(l1)
        l2 = plt.legend(bbox_to_anchor=(1.04,0), loc="lower left", borderaxespad=0)
        ax.add_artist(l2)
        l3 = plt.legend(bbox_to_anchor=(1.04,0.5), loc="center left", borderaxespad=0)
        ax.add_artist(l3)
        l4 = plt.legend(bbox_to_anchor=(0,1.02,1,0.2), loc="lower left", mode="expand", borderaxespad=0, ncol=3)
        ax.add_artist(l4)
        l5 = plt.legend(bbox_to_anchor=(1,0), loc="lower right", bbox_transform=fig.transFigure, ncol=3)
        ax.add_artist(l5)
        l6 = plt.legend(bbox_to_anchor=(0.4,0.8), loc="upper right")
        plt.savefig(__file__+".svg", facecolor="#f6f9fd", frameon=True)
    else:
        fig.set_size_inches( 6,2 )
    
    if case == 0:
        plt.title("No adjustment")
        l1 = plt.legend(bbox_to_anchor=(1.04,1), borderaxespad=0)
        plt.savefig(__file__+str(case)+".svg", facecolor="#f6f9fd", frameon=True)
        
    elif case ==1: #subplotsadjust
        plt.title("plt.subplots_adjust(right=0.75)")
        plt.subplots_adjust(right=0.75)
        l1 = plt.legend(bbox_to_anchor=(1.04,1), borderaxespad=0)
        plt.savefig(__file__+str(case)+".svg", facecolor="#f6f9fd", frameon=True)
        
    elif case == 2:
        plt.title("plt.tight_layout(rect=[0,0,0.75,1])")
        l1 = plt.legend(bbox_to_anchor=(1.04,1), borderaxespad=0)
        plt.tight_layout(rect=[0,0,0.75,1])
        plt.savefig(__file__+str(case)+".svg", facecolor="#f6f9fd", frameon=True)
    elif case == 3:
        plt.title('plt.savefig(...,bbox_inches="tight")')
        l1 = plt.legend(bbox_to_anchor=(1.04,1), borderaxespad=0)
        plt.savefig(__file__+str(case)+".svg", bbox_inches="tight", facecolor="#f6f9fd", frameon=True)
    elif case == 4:
        l1 = plt.legend(loc=7)    
        plt.savefig(__file__+str(case)+".svg", bbox_inches="tight", facecolor="#f6f9fd", frameon=True)
    
    
    plt.show()
    

    Note that the image that is shown there is not completely done in matplotlib. All annotations for the positions are manually placed using Inkscape. The following zip file includes this svg image.

    SO_Legend.zip

  4. added this to the milestone on Jan 11, 2018
  5. github-actions commented on Apr 28, 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!

  6. tacaswell commented on Apr 28, 2023

    Member

    This is still a good idea.

  7. added
    🌱 Good first issueOpen a pull request against these issues if there are no active ones!
    and removed
    status: inactiveMarked by the “Stale” Github Action
    on Apr 28, 2023
  8. story645 commented on Apr 28, 2023

    Member

    Marking good first issue because

    1. the tabular example would slot really nicely into https://matplotlib.org/devdocs/gallery/text_labels_and_annotations/legend_demo.html (which could benefit from some headings) (easy difficult)
    2. the location example could be added to or integrated with https://matplotlib.org/devdocs/users/explain/axes/legend_guide.html#legend-location, though here we may want to start this section with the set of named locations and make an explicit bbox_to_anchor section as he does. (medium difficulty)
  9. AlexSzatmary commented on Aug 9, 2023

    Contributor

    I'd like to try the location example as a good first issue. I have read through this: https://matplotlib.org/devdocs/users/explain/axes/legend_guide.html#legend-location and this: https://stackoverflow.com/questions/4700614/how-to-put-the-legend-outside-the-plot/43439132#43439132 . I'm considering copying in the SO answer, which is lengthy (in a good way!) and then work in any missing ideas from the current doc. It's a big change but the popularity of the SO answer shows how helpful it already has been to the community.

    I'd like a general read on whether that's a good approach and any particular bits that seem really great from either. The SO answer has the excellent annotated figure, the visualization of what the loc number arguments mean, and numerous helpful tidbits. The current doc makes good use of the outside keyword.

    PS Thanks @ImportanceOfBeingErnest !

  10. story645 commented on Aug 11, 2023

    Member

    I'm considering copying in the SO answer, which is lengthy (in a good way!) and then work in any missing ideas from the current doc. It's a big change but the popularity of the SO answer shows how helpful it already has been to the community.

    That seems like an ok start, but I think the underlying strength of this answer is that it's really well scoped to location and the organization is very cleanly organized from "here's a kwarg" to custom, to custom in custom. I think in integrating it, it may be worth thinking about maybe reorganizing the "handler" part of the guide to also be a bit cleaner in scoping and narrative?

  11. github-actions commented on Aug 12, 2024

    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!

  12. added
    keepItems to be ignored by the “Stale” Github Action
    and removed
    status: inactiveMarked by the “Stale” Github Action
    on Aug 12, 2024
  13. saumyacoder1709 commented on Dec 8, 2025

    Contributor

    I would like to work on this issue. Can somebody guide me what needs to be done.

  14. removed
    🌱 Good first issueOpen a pull request against these issues if there are no active ones!
    on Dec 8, 2025
  15. story645 commented on Dec 9, 2025

    Member

    @saumyacoder1709 I suggest your start w/ what I think is the easier task, which is to add the tabular example into https://matplotlib.org/devdocs/gallery/text_labels_and_annotations/legend_demo.html and add some subheadings to separate the examples

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocumentationkeepItems to be ignored by the “Stale” Github Action

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL