Custom Legend Placement on a Plot

I want to place a legend below the Title but above the plot area of a scatter plot. It looks like the built-in methods place the legend within the Plot Area or outside of the area defined by the Plot Area and Title. Is this true?
One method I considered was to include the contents of the legend in the bottom rows of a multi-line Title. However, I cannot figure out how to replicate the line / marker styles in a string (I am specifying the line and marker styles to be used in the plot).

답변 (1개)

Peter
Peter 2013년 3월 21일

1 개 추천

You could create a multiline title, using spaces to create room for the legend, and then move the legend, using its position property, like:
title({'Title';' ';' ';' '})
h = legend('line1','line2');
pos = get(h,'Position');
posx = 0.5;
posy = 0.85;
set(h,'Position',[posx posy pos(3) pos(4)]);
Change posx and posy to position your legend.

댓글 수: 3

Jim
Jim 2013년 3월 22일
I am considering doing that. However, I don't know how to replicate the line / marker styles in the Title. In my particular case, I have different combinations of circle and square markers and solid and dashed lines. So, how do I display those line styles in the Title?
Peter
Peter 2013년 3월 22일
Don't the line/marker styles appear in the legend box that is created with the h = legend(...) command?
Sebastian Lopez
Sebastian Lopez 2020년 12월 30일
Works perfectly fine! Thanks

댓글을 달려면 로그인하십시오.

카테고리

태그

질문:

Jim
2013년 3월 21일

댓글:

2020년 12월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by