Adding a legend to a SubFigure
doesn't work
#20723
Labels
Milestone
Comments
Yep that was just an oversight, not a design decision ;-) |
I don't want to complicate this too much, but a further issue arrises even after changing the line I suggest above (let me know if this should be a separate issue) fig = plt.figure()
subfig = fig.subfigures()
ax = subfig.subplots()
ax.plot([0, 1, 2], [0, 1, 2], label="test")
subfig.legend()
print(fig.get_default_bbox_extra_artists()) doesn't include the legend and so the legend is cut off if saving the figure with [<matplotlib.figure.SubFigure object at 0x7fb5cbbe8790>,
<matplotlib.lines.Line2D object at 0x7fb5cbc0db50>,
<matplotlib.spines.Spine object at 0x7fb5cadc73a0>,
<matplotlib.spines.Spine object at 0x7fb5cb8ed820>,
<matplotlib.spines.Spine object at 0x7fb5cb8ed6d0>,
<matplotlib.spines.Spine object at 0x7fb5cad61f70>,
<matplotlib.axis.XAxis object at 0x7fb5cadc7be0>,
<matplotlib.axis.YAxis object at 0x7fb5cbbe8e50>,
<matplotlib.patches.Rectangle object at 0x7fb5cbbfd760>] Adding something like if self.subfigs:
return [artist for subfig in self.subfigs
for artist in subfig.get_default_bbox_extra_artists()] to the start of matplotlib/lib/matplotlib/figure.py Lines 1578 to 1584 in 62c1588 seems to do the trick, but let me know if there are any better ideas. |
OK, so all that logic seems wrong - |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Bug report
Bug summary
Adding a legend to a
SubFigure
doesn't workCode for reproduction
Actual outcome
Expected outcome
I'd expect this to work and produce a legend. The example is of course a bit contrived but it would be useful to allow a legend per subfigure
Changing L437 here to check against
FigureBase
fixes it.matplotlib/lib/matplotlib/legend.py
Lines 433 to 442 in 62c1588
I can make a PR at some point but wanted to flag the issue here in case anyone gets to it first.
Matplotlib version
import matplotlib; print(matplotlib.__version__)
): 3.4.2.post1350+gdba02be18eprint(matplotlib.get_backend())
): TkAggThe text was updated successfully, but these errors were encountered: