I'm using MATLAB to plot some data. I would like to create the legend so that it divides information into two types, say Type1 and Type2. Each type of data has several specific lines of data which i am plotting. Suppose that:
This would make a 5 line plot. Now for the legend i can do something like:
legend('Type1: line1','Type1: line2','Type2: line1', ...
'Type2: line2','Type2: line3');
But that repeats the 'Types' and the best way would be something that would look like:
(final legend output)
Thanks a lot. Hope you can help with my first question here :D
The function LEGEND allows creation of only one legend object per axes. Multiple legends can be created by using the function COPYOBJ to make a copy of a legend object.
legend creates a legend with descriptive labels for each plotted data series. For the labels, the legend uses the text from the DisplayName properties of the data series. If the DisplayName property is empty, then the legend uses a label of the form 'dataN' .
x = linspace(0,10); y1 = sin(x); y2 = sin(0.9*x); y3 = sin(0.8*x); y4 = sin(0.7*x); y5 = sin(0.6*x); y6 = sin(0.5*x); plot(x,y1,'DisplayName','sin(x)') hold on plot(x,y2,'DisplayName','sin(0.9x)') plot(x,y3,'DisplayName','sin(0.8x)') plot(x,y4,'DisplayName','sin(0.7x)') plot(x,y5,'DisplayName','sin(0.6x)') plot(x,y6,' ...
You may want to have a look at LEGENDFLEX that you can download from the Matlab File Exchange
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With