display legends with different line styles in matlab
Ältere Kommentare anzeigen
How one can draw a graph with different line styles corresponding to different legends? For instance, my data is 5 by 4 as follows:
__________________________________________________
A B C D
__________________________________________________
0.84702 0.64419 0.61728 0.48538
0.76445 0.1864 0.39728 0.15959
0.04743 0.0412 0.09432 0.86721
0.4256 0.1458 0.05432 0.803192
0.9855 0.8242 0.5679 0.7102
___________________________________________________
So far I tried the following, first I saved this data file as 'myData.m'. Next, I load it on my Matlab window using the command 'load myData.m'. Then after, I have used the followig codes:
[n,p] = size(myData);
t = 1:n;
plot(t,myData)
legend('A','B','C','D','Location','NorthEast')
xlabel('Time'); ylabel('Value');
How can I display legends with different line styles((like '-','.-','.' and '--')) along with different colours using Matlab?
Akzeptierte Antwort
Weitere Antworten (1)
Samuel Suakye
am 23 Okt. 2022
0 Stimmen
col = [0,0,1;0.9,0.4,0.17;0,1,0]; % Blue, Orange, Green How do I change this to linestyle
8 Kommentare
Walter Roberson
am 23 Okt. 2022
Those are not valid line styles.
The plot routines often accept a "linespec" which is a character vector that potentially encodes color and style and marker shape. linespec only permit color names or color codes such as 'black' or 'k' to specify the color. linespec do not permit rgb triples (unless there is a formal I am forgetting.) To pass an rgb color use 'Color' name/value pair, but you can only use one of those per plot() call. If you are plotting multiple items in the same call and want specific rgb color then record the plot handles and set the Color associated with the line objects.
Samuel Suakye
am 23 Okt. 2022
col = [0,0,1;0.9,0.4,0.17;0,1,0]; % Blue, Orange, Green This line of code in the codes where multiple items are plotted in the same call ...what I want to do now is to change it to linestyle...
Samuel Suakye
am 23 Okt. 2022
How do you combine both the linestyle and colour?
Samuel Suakye
am 23 Okt. 2022
Thank you
Samuel Suakye
am 23 Okt. 2022
Tried but still not working
Walter Roberson
am 24 Okt. 2022
Change
col = [0,0,1;0.9,0.4,0.17;0,1,0]; % Blue, Orange, Green
to
col = {'-', ':', '--'};
Change
plot(linAx,W*1e-12,JxL*1e13,'color',col(i,:))
to
plot(linAx,W*1e-12,JxL*1e13, col{i});
Samuel Suakye
am 24 Okt. 2022
Thank you
Kategorien
Mehr zu 2-D and 3-D Plots finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!