Change legend guide to object oriented approach #20792
Conversation
Not sure if one of the plt examples should be left in as a resource but otherwise is awesome. |
line_up, = plt.plot([1, 2, 3], label='Line 2') | ||
line_down, = plt.plot([3, 2, 1], label='Line 1') | ||
plt.legend(handles=[line_up, line_down]) | ||
line_up, = ax.plot([1, 2, 3], label='Line 2') |
plt.subplot(211) | ||
plt.plot([1, 2, 3], label="test1") | ||
plt.plot([3, 2, 1], label="test2") | ||
ax1 = plt.subplot(211) |
84a7c7a
to
9d821e5
9d821e5
to
078a87e
totally agree w/ you about adding in something on location, possibly as an expansion of https://62747-1385122-gh.circle-artifacts.com/0/doc/build/html/tutorials/intermediate/legend_guide.html#legend-location & maybe using the cheatsheet as inspiration? |
line_up, = plt.plot([1, 2, 3], label='Line 2') | ||
line_down, = plt.plot([3, 2, 1], label='Line 1') | ||
plt.legend([line_up, line_down], ['Line Up', 'Line Down']) | ||
line_up, = ax.plot([1, 2, 3], label='Line 2') |
story645
Aug 5, 2021
Member
line_up, = ax.plot([1, 2, 3], label='Line 2') | |
fig, ax = plt.subplots() | |
line_up, = ax.plot([1, 2, 3], label='Line 2') |
yeah is repetitive but I think they're all supposed to be self contained
# plt.legend(bbox_to_anchor=(1, 1), | ||
# bbox_transform=plt.gcf().transFigure) | ||
# ax.legend(bbox_to_anchor=(1, 1), | ||
# bbox_transform=fig.transFigure) |
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0.) | ||
ax2.plot([1, 2, 3], label="test1") | ||
ax2.plot([3, 2, 1], label="test2") | ||
# Place a legend on top center of this subplot. |
QuLogic
Aug 5, 2021
Member
I think the point is to be outside on this one. We can replicate the layout with funny subplot
mixing, using subplot_mosaic
:
fig, ax_dict = plt.subplot_mosaic([
['top', 'top'],
['bottom', '-'],
])
dmatos2012
Aug 5, 2021
Author
Contributor
This is actually pretty cool, wasnt aware of that, bc I was struggling with using fig, ax = plt.subplots()
to get that layout, hence changing the label to the inside. Thx for the tip @QuLogic
|
||
# Create a legend for the first line. | ||
first_legend = plt.legend(handles=[line1], loc='upper right') | ||
first_legend = ax.legend(handles=[line1], loc='upper right') | ||
|
||
# Add the legend manually to the current Axes. |
QuLogic
Aug 5, 2021
Member
# Add the legend manually to the current Axes. | |
# Add the legend manually to the Axes. |
fig, ax = plt.subplots() | ||
z = randn(10) | ||
|
||
red_dot, = plt.plot(z, "ro", markersize=15) | ||
red_dot, = ax.plot(z, "ro", markersize=15) |
QuLogic
Aug 5, 2021
Member
fig, ax = plt.subplots() | |
z = randn(10) | |
red_dot, = plt.plot(z, "ro", markersize=15) | |
red_dot, = ax.plot(z, "ro", markersize=15) | |
z = randn(10) | |
fig, ax = plt.subplots() | |
red_dot, = ax.plot(z, "ro", markersize=15) |
l = ax.legend([(p1, p2)], ['Two keys'], numpoints=1, | ||
handler_map={tuple: HandlerTuple(ndivide=None)}) |
QuLogic
Aug 5, 2021
Member
l = ax.legend([(p1, p2)], ['Two keys'], numpoints=1, | |
handler_map={tuple: HandlerTuple(ndivide=None)}) | |
l = ax.legend([(p1, p2)], ['Two keys'], numpoints=1, | |
handler_map={tuple: HandlerTuple(ndivide=None)}) |
@@ -238,6 +243,8 @@ | |||
|
|||
import matplotlib.patches as mpatches | |||
|
|||
fig, ax = plt.subplots() |
@@ -272,6 +279,8 @@ def legend_artist(self, legend, orig_handle, fontsize, handlebox): | |||
|
|||
from matplotlib.legend_handler import HandlerPatch | |||
|
|||
fig, ax = plt.subplots() |
ax.legend([AnyObject()], ['My first handler'], | ||
handler_map={AnyObject: AnyObjectHandler()}) |
QuLogic
Aug 5, 2021
Member
ax.legend([AnyObject()], ['My first handler'], | |
handler_map={AnyObject: AnyObjectHandler()}) | |
ax.legend([AnyObject()], ['My first handler'], | |
handler_map={AnyObject: AnyObjectHandler()}) |
ax.legend([c], ["An ellipse, not a rectangle"], | ||
handler_map={mpatches.Circle: HandlerEllipse()}) |
QuLogic
Aug 5, 2021
Member
ax.legend([c], ["An ellipse, not a rectangle"], | |
handler_map={mpatches.Circle: HandlerEllipse()}) | |
ax.legend([c], ["An ellipse, not a rectangle"], | |
handler_map={mpatches.Circle: HandlerEllipse()}) |
078a87e
to
5cb4dcf
I was thinking perhaps something like creating a 3x3 (or 2 x2 w.e) |
I think it's a good idea to leave it to another issue since that's gonna be a bigger change to the guide content so folks might have more opinions. |
PR Summary
Addresses #20754 to change legend guide from all
pyplot
interface to theax
objected oriented approachPR Checklist
pytest
passes).flake8
on changed files to check).flake8-docstrings
and runflake8 --docstring-convention=all
).doc/users/next_whats_new/
(follow instructions in README.rst there).doc/api/next_api_changes/
(follow instructions in README.rst there).