Adding an upward arrow to legend

17 views (last 30 days)
Patrick Looij
Patrick Looij on 18 Aug 2017
Commented: Walter Roberson on 18 Aug 2017
I would like to add a small upward arrow with a text next to it similar to the four entries of the legend in the code below. I created the legend in a bit alternative way, because not all entries are in the figure, while I do want to show them in the legend. I cannot find any documentation on how to do this. Could anyone help me?
h = zeros(4, 1);
h(1) = plot(NaN,NaN,'color','k');
h(2) = plot(NaN,NaN,'color',[0, 0.1, 1]);
h(3) = plot(NaN,NaN,'color',[1, 0.647059, 0]);
h(4) = plot(NaN,NaN,'color','r');
[legh,objh,outh,outm] = legend(h, 'text1','text2','text3','text4');

Answers (1)

Walter Roberson
Walter Roberson on 18 Aug 2017
UP = char(8593);
[legh,objh,outh,outm] = legend(h, [UP 'text1'], [UP 'text2'],[UP 'text3'],[UP 'text4']);
Note: this will not work if you set the legend Interpreter property to LaTex, but will work with Interpreter Tex or None
  2 Comments
Patrick Looij
Patrick Looij on 18 Aug 2017
Thank you for your answer! Unfortunately I realised that I formulated my question incorrect. I would like to have an upward arrow as a new fifth entry, and describe what it means in text next to it. Additionally, I would like to be able to adjust the colour of the arrow as well.
Walter Roberson
Walter Roberson on 18 Aug 2017
You would need to create an additional graphics object to attach it to. You could use the same plot() of nan technique that you already used.
To adjust the color, index the objh output to get the 5th entry (that is, the one corresponding to the 5th legend text) to get the underlying text object; you can then set the object's Color property to the color you want the arrow to be.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!