MATLAB graph plotting: assigning legend labels during plot

前端 未结 5 781
傲寒
傲寒 2020-12-29 12:11

I am plotting data in a typical MATLAB scatterplot format. Ordinarily when plotting multiple datasets, I would use the command \'hold on;\', and then plot each of the data,

5条回答
  •  猫巷女王i
    2020-12-29 12:31

    Use 'DisplayName' as a plot() property, and call your legend as

    legend('-DynamicLegend');
    

    My code looks like this:

    x = 0:h:xmax;                                  %// get an array of x-values
    y = someFunction;                              %// function
    plot(x, y, 'DisplayName', 'Function plot 1');  %// plot with 'DisplayName' property
    
    legend('-DynamicLegend',2);                    %// '-DynamicLegend' legend
    

    Source: http://undocumentedmatlab.com/blog/legend-semi-documented-feature/

提交回复
热议问题