How do I change the length of the bar in Legend axes, when a bar graph is plotted in MATLAB?
2 views (last 30 days)
Show older comments
I have a bar graph and an associated legend with it. The legend axes has a bar in it. Is it possible to change the length of the bar in the Legend axes?
Accepted Answer
MathWorks Support Team
on 27 Jun 2009
It is possible to change the length of bar in the Legend axes by changing the vertices of the patch object inside legend axes.
Please refer to the sample code below:
bar([2 1]); % Plot the bar graph
[legend_h,object_h,plot_h,text_strings] = legend('a');
l = findall(legend_h, 'type', 'patch'); % Get the patch object inside the legend % axes
v = get(l, 'vertices'); % Get the vertices of the patch
Length = v(3,1) - v(2,1) ;
v(3,1) = v(3,1) - Length/2; % Change the length to half
v(4,1) = v(4,1) - Length/2;
set(l, 'vertices', v);
0 Comments
More Answers (0)
See Also
Categories
Find more on Legend in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!