MATLAB Answers

Changing length of bar in legend

조회 수: 48(최근 30일)
Steven
Steven 24 Oct 2014
댓글: Steven 28 Oct 2014
Here is discussed how to change the length of the bar in Legend axes, when a bar graph is plotted.
But how do you apply the code discussed there to a bar graphs with multiple bars per x tick?
For example on,
bar([1,2;3,4])
legend('one','two')
Thanks!

  댓글 수: 0

로그인 to comment.

채택된 답변

Kelly Kearney
Kelly Kearney 27 Oct 2014
It can be done pretty easily with my legend-reformatting function, legendflex:
bar([1,2;3,4]);
legendflex(gca, {'one','two'}, 'xscale', 0.5)

  댓글 수: 3

Steven
Steven 27 Oct 2014
Great! I wasn't aware of this function.. Thanks!
I do get an error message though:
Undefined function 'legendflex' for input arguments of type 'cell'.
I am using 2012b, so copied the file 'legendflex_pre2014b' and 'getpos' into my working directory.
What am I doing wrong?
Kelly Kearney
Kelly Kearney 27 Oct 2014
Two options... either rename legendflex_pre2014b.m to legendflex.m (and don't download my more recent version of legendflex.m).
Or just add all 3 functions to your path, then call legendflex... it will fall back on the pre2014b version automatically if it determines that you're running an older version of Matlab. This latter method is the one I intended, since that way everything will still work properly if you ever upgrade Matlab to a version with the new handle graphics.
Steven
Steven 28 Oct 2014
It works! Thanks a lot!

로그인 to comment.

추가 답변(2개)

Azzi Abdelmalek
Azzi Abdelmalek 24 Oct 2014
You can change the position
bar([1,2;3,4])
h=legend('one','two')
pos=get(h,'position')
new_pos=pos
new_pos(3)=pos(3)+0.4 % This is the x length
new_pos(1)=pos(1)-0.4 % The x position
set(h,'position',new_pos)

  댓글 수: 1

Steven
Steven 27 Oct 2014
Thanks for reply!
But why do you want to change the position? Is it possible to change the size of the boxes in the legends, making the overall size of the legend smaller, but keeping all else equal?

로그인 to comment.


Robert Cumming
Robert Cumming 27 Oct 2014
You can change the fontSize property which makes it smaller.
Otherwise if your using pre 2014b you can get to all the properties through the legend handle. Since the legend is built on a axes you can alter the axes children

  댓글 수: 0

로그인 to comment.

이 질문에 답변하려면 로그인을(를) 수행하십시오.


Translated by