How do I make the legend color rectangles smaller?
20 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
In barplots legends I find too wide the boxes that show each color. I want they to be more like squares so I can have a narrower legend and save space. Is there any way to do it?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/624723/image.jpeg)
0 Kommentare
Antworten (1)
Girijashankar Sahoo
am 20 Mai 2021
legend({'A','B'},'Position',[0.2 0.6 0.1 0.2])
%% four-element vector of the form [left bottom width height]
4 Kommentare
Bill Tubbs
am 22 Mär. 2023
Bearbeitet: Bill Tubbs
am 22 Mär. 2023
This doesn't seem to have an effect on the size of the coloured boxes. For example, if I set my legend position to [0.15 0.68 0.0 0.21]` the rectangle size does not go lower than it's minimum which is about 0.2 inches.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1332580/image.png)
The width of these colour boxes seems a bit excessive. Especially when for some publications the whole figure has to be less than 3".
Miguel Ángel Palomo
am 10 Mär. 2024
After playing with the objects in a graph, I`ve found a way to change the size and shape of the boxes that show each color. I`ve found that each of the color boxes is a patch in the icons of the legend, and you can work from there.
[h,icons] = legend([A,B],{'F_mean','F_max'});
legend('boxoff') %This is optional
boxes=findobj(icons,'type','patch');
box1=boxes(1);
box1.XData=[0.4;0.4;0.6;0.6];
%The values here work for my case, but you may have to change them
box1.LineWidth=1;
box2=boxes(2);
box2.XData=[0.4;0.4;0.6;0.6];
box2.LineWidth=1;
%You may also change YData, but it is not necessary to make them square-like.
After this, the boxes look like this:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1639856/image.png)
Siehe auch
Kategorien
Mehr zu Legend finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!