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).
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.
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!
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.
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?
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!
Activity
ImportanceOfBeingErnest commented on Dec 23, 2017
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).
anntzer commented on Dec 24, 2017
Could you share the script that you used to generate the first annotated plot of that guide? Thanks.
ImportanceOfBeingErnest commented on Jan 11, 2018
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
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!
tacaswell commented on Apr 28, 2023
This is still a good idea.
story645 commented on Apr 28, 2023
Marking good first issue because
AlexSzatmary commented on Aug 9, 2023
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 !
story645 commented on Aug 11, 2023
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?
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!
saumyacoder1709 commented on Dec 8, 2025
I would like to work on this issue. Can somebody guide me what needs to be done.
story645 commented on Dec 9, 2025
@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