@@ -901,52 +901,59 @@ def save(figure: "matplotlib.figure.Figure|None" = None):
901901 # Figures to save.
902902 to_save = [("main_figure" , figure , None )]
903903
904- # Go through and fix up a few little quirks on the axes within this figure.
904+ # Find all legends in this figure.
905+ legends = list (figure .legends )
905906 for axes in figure .get_axes ():
906- # Check if these axes have a legend.
907907 legend = axes .get_legend ()
908908 if legend :
909- # Want to save the legend as a separate figure.
910- if config .pgfutils ["separate_legend" ]:
911- # Create a new figure to hold the legend with empty axes.
912- legend_fig = plt .figure ()
913- legend_ax = legend_fig .add_subplot ()
914- legend_ax .axis ("off" )
915-
916- # Regenerate the legend on the new axes, allowing it to use the whole
917- # figure. Removing it from the original axes and then using add_artist
918- # on these axes seems to get the bounding box wrong.
919- legend_standalone = legend_ax .legend (
920- * legend .axes .get_legend_handles_labels (),
921- bbox_to_anchor = (0 , 0 , 1 , 1 ),
922- bbox_transform = legend_fig .transFigure ,
923- ncols = legend ._ncols ,
924- numpoints = legend .numpoints ,
925- scatterpoints = legend .scatterpoints ,
926- )
909+ legends .append (legend )
910+
911+ # Configure the legends.
912+ for legend in legends :
913+ # Want to save the legend as a separate figure.
914+ if config .pgfutils ["separate_legend" ]:
915+ # Create a new figure to hold the legend with empty axes.
916+ legend_fig = plt .figure ()
917+ legend_ax = legend_fig .add_subplot ()
918+ legend_ax .axis ("off" )
919+
920+ # Regenerate the legend on the new axes, allowing it to use the whole
921+ # figure. Removing it from the original axes and then using add_artist
922+ # on these axes seems to get the bounding box wrong.
923+ legend_standalone = legend_ax .legend (
924+ legend .legend_handles ,
925+ [text .get_text () for text in legend .texts ],
926+ bbox_to_anchor = (0 , 0 , 1 , 1 ),
927+ bbox_transform = legend_fig .transFigure ,
928+ ncols = legend ._ncols ,
929+ numpoints = legend .numpoints ,
930+ scatterpoints = legend .scatterpoints ,
931+ )
927932
928- # Measure its bounding box.
929- bbox = legend_standalone .get_tightbbox ()
930- if not bbox :
931- raise RuntimeError ("could not determine legend bounding box" )
933+ # Measure its bounding box.
934+ bbox = legend_standalone .get_tightbbox ()
935+ if not bbox :
936+ raise RuntimeError ("could not determine legend bounding box" )
932937
933- # This is in pixels; convert to inches as savefig() will need.
934- bbox = bbox .transformed (legend_fig .dpi_scale_trans .inverted ())
938+ # This is in pixels; convert to inches as savefig() will need.
939+ bbox = bbox .transformed (legend_fig .dpi_scale_trans .inverted ())
935940
936- # Remove the original legend and replace the reference.
937- legend .remove ()
938- legend = legend_standalone
941+ # Remove the original legend and replace the reference.
942+ legend .remove ()
943+ legend = legend_standalone
939944
940- # And save the standalone figure as a legend.
941- to_save .append (("legend" , legend_fig , bbox ))
945+ # And save the standalone figure as a legend.
946+ to_save .append (("legend" , legend_fig , bbox ))
942947
943- # There are no rcParams for the legend properties; set them directly.
944- frame = legend .get_frame ()
945- frame .set_linewidth (config .pgfutils ["legend_border_width" ])
946- frame .set_alpha (config .pgfutils ["legend_opacity" ])
947- frame .set_ec (config .pgfutils ["legend_border_color" ])
948- frame .set_fc (config .pgfutils ["legend_background" ])
948+ # There are no rcParams for the legend properties; set them directly.
949+ frame = legend .get_frame ()
950+ frame .set_linewidth (config .pgfutils ["legend_border_width" ])
951+ frame .set_alpha (config .pgfutils ["legend_opacity" ])
952+ frame .set_ec (config .pgfutils ["legend_border_color" ])
953+ frame .set_fc (config .pgfutils ["legend_background" ])
949954
955+ # Go through and fix up a few little quirks on the axes within this figure.
956+ for axes in figure .get_axes ():
950957 # Some PDF viewers show white lines through vector colorbars. This is a bug in
951958 # the viewers, but can be worked around by forcing the edge of the patches in
952959 # the colorbar to have the same colour as their faces. This doesn't work with
0 commit comments