- passing them into plot as a matrix, plot will make a line for each column of the matrix if you don't specify x values. If you do specify x values plot will match up the appropriate dimension of the matrix.
- calling plot multiple times, making sure to specify hold on some time before the second call
Plotting a graph with different colors depending on different vectors
86 views (last 30 days)
Show older comments
Hi! I would like to plot a graph that has different colors depending on different vectors. I want to sum up all vectors but still be able to tell them apart by showing them in different colors. There are 4 vectors with 8760 values each. How can I do this?
0 Comments
Answers (2)
Dave B
on 13 Apr 2022
You can plot multiple vectors by either:
You can specify the color explicitly for each line, or let MATLAB select from the current color order. You can change the colors in that list using the colororder function, and you can change the mapping of each color to its index into the color order using the SeriesIndex property.
Example data as you described it
y1=randn(8760,1);
y2=randn(8760,1)+5;
y3=randn(8760,1)+10;
y4=randn(8760,1)+15;
Plot of the four variables:
plot([y1 y2 y3 y4])
You mentioned the sum:
hold on
thesum = sum([y1 y2 y3 y4],2);
plot(thesum)
A legend to show what the colors refer to
legend(["y1" "y2" "y3" "y4" "sum"])
Alternate, use hold on:
figure
plot(y1)
hold on
plot(y2)
plot(y3)
plot(y4)
Change the colors using colororder:
figure
plot([y1 y2 y3 y4])
colororder([1 0 0; 0 1 0; 0 0 1; 0 1 1])
Specify the colors explicitly for each line:
figure
plot(y1,'black')
hold on
plot(y2,'red')
plot(y3,'blue')
plot(y4,'green')
0 Comments
DGM
on 13 Apr 2022
Depends what your data is and what you actually want. Are you thinking of an area plot?
n = 6;
A = 0.5*rand(n,1) + 0.5;
B = 0.5*rand(n,1) + 0.5;
C = 0.5*rand(n,1) + 0.5;
D = 0.5*rand(n,1) + 0.5;
S = A+B+C+D;
ha = area([A B C D]);
hold on;
hp = plot(S,'linewidth',2);
legend([ha hp],{'A','B','C','D','A+B+C+D'})
That only really makes sense if each vector is always positive-valued.
0 Comments
See Also
Categories
Find more on Discrete Data Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!