Add custom legend without any relation to the graph

后端 未结 2 690
我在风中等你
我在风中等你 2020-12-09 02:38

I wish to insert a legend that is not related to the graph whatsoever:

figure;
hold on;
plot(0,0,\'or\');
plot(0,0,\'ob\');
plot(0,0,\'ok\');
leg = legend(\'         


        
2条回答
  •  -上瘾入骨i
    2020-12-09 03:03

    Your question is a little unclear. However, the first thing I thought of when reading it was the text function in Matlab.

    You can use the text function to add text to a Matlab figure. It's use is

    >> text(x, y, str);
    

    where x and y are the coordinates in the figure where you want to add the text str. You can use the Color option of text for colours and TeX to draw lines or even _. I've gotten very creative with plots using text.

    Here's a quick and dirty example of emulating a legend with text

    x = 0:pi/20:2*pi;
    y = sin(x);
    plot(x,y)
    axis tight
    
    legend('sin(x)');
    
    text(5.7, 0.75, 'sin(x)');
    text(5.1, 0.78, '_____', 'Color', 'blue');
    

    which produces

                 

    For this specific case you could use the specific command (noted by @Hoki in the comments).

    ht = text(5, 0.5, {'{\color{red} o } Red', '{\color{blue} o } Blue', '{\color{black} o } Black'}, 'EdgeColor', 'k');
    

    to produce

                 

    by retrieving the handle to the text object it becomes trivial to copy it to a new figure, copyobj(ht, newfig). [1]

提交回复
热议问题