Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

move legend position in an empty subplot

Tags:

plot

matlab

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?

like image 260
BlueBit Avatar asked Jul 24 '13 10:07

BlueBit


People also ask

How do you move a legend position in Matlab?

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.

How do I change the position of a legend in Matplotlib?

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.

How do you move your legend position in Seaborn?

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.

How do you add a legend to a subplot?

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.


2 Answers

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)])
like image 68
Lucius II. Avatar answered Sep 23 '22 03:09

Lucius II.


Maybe try legendflex from the File Exchange, it looks like it can do what you want.

like image 45
am304 Avatar answered Sep 23 '22 03:09

am304