How do I change the length of the bar in Legend axes, when a bar graph is plotted in MATLAB?

16 views (last 30 days)
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
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);

More Answers (1)

Gabor Horvath
Gabor Horvath on 21 Nov 2018
Dear MathWorks Support Team,
could you help me, how to adapt that solution in newer version, like 2016? Unmodified form doesn't work.
Thank you.

Products


Release

R2008a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!