I have a figure with 5 subplots. I declare subplot(2,3,X) which includes 6 subplots. The 6th subplot is empty. I am going to move legend to 6th empty position for all plots.
How is it possible?
To move the legend to a different tile, set the Layout property of the legend. Determined by Position property. Use the Position property to specify a custom location.
To change the position of a legend in Matplotlib, you can use the plt. legend() function. The default location is “best” – which is where Matplotlib automatically finds a location for the legend based on where it avoids covering any data points.
With Seaborn's move_legend() function, we can easily change the position to common legend places. For example, to move the legend to lower right of Seaborn plot we specify “lower right” to move_legend() function. Note now the legend is moved to the lower left corner of the plot.
Create a figure and a set of subplots, using the subplots() method, considering 3 subplots. Plot the curve on all the subplots(3), with different labels, colors. To place the legend for each curve or subplot adding label. To activate label for each curve, use the legend() method.
if you just want to use standard-matlab, you need the handle of the subplot and then you need its position. Then you set the position of the legend to the position of the subplot. Referring to the docs:
Note You can set the legend location by passing the 4–element position vector to the legend function using the ‘Location' option. To define the position of an existing legend, use the set function to assign the 4–element position vector to the ‘Position' property. You cannot use the Location option with the set function
for example:
subplot(2,3,1), plot(1:10,2:11)
myLegend=legend('text1')
set(myLegend,'Units', 'pixels')
myOldLegendPos=get(myLegend,'Position')
hold on
h=subplot(2,3,6)
set(h,'Units', 'pixels')
myPosition=get(h,'Position')
set(myLegend,'Position',[myPosition(1) myPosition(2) myOldLegendPos(3) myOldLegendPos(4)])
Maybe try legendflex from the File Exchange, it looks like it can do what you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With