Connect data vertically.
Mostra commenti meno recenti
Hello, I have a matrix 15x12 and a vector 1x12. I am trying to plot both of them in the same plot. However I want each row to have different color from the previous. For exp the first should be green and then the color will change between blue and red. Also I want to connect the data of the same column in the plot. So far I have tried the following code and I was able to plot my data and add vertical lines at each data of the vector. But I do not know how to connect the data of each column of the matrix and make the color of each row to change. I appreciate any help. Thanks in advance
x = (1:12);
y = rand(15,12); %in reality y values are different
plot(x,0.05,'g*'),hold on
for icount=1:1:15;
plot(y(icount,:),icount / 10,'r*')
end
yL = get(gca,'YLim');
for r = 1:1:12;
line([x(1,r) x(1,r)],yL,'Color','g');
end

Risposte (1)
Why waste your time fighting MATLAB with loops and other ugly pieces of code, when you can learn to use MATLAB. Plotting the columns of a matrix is exactly what plot does: there is no point in trying to replicate this inbuilt behavior by writing lots of loops: the inbuilt code that has already been written and tested and works perfectly. Just learn to think in terms of columns and using MATLAB will suddenly make sense and be easy to use:
M = rand(10,4);
plot(M,'*-')
creates this:

And if you want to select the line colors, try something like this (works for older versions, you will need to check how the newer versions control this):
M = rand(10,4);
axh = axes('ColorOrder',cool(4),'NextPlot','replacechildren');
plot(axh,M,'*-')

2 Commenti
manoshn
il 31 Ago 2016
@manoshn: as long as each line has one color then you can exactly the method that I showed in my answer. It works: did you try it ?
If you want the lines to have different colors then this is more difficult, but you would essentially have to plot each color separately.
When I ran your sample code I got a different figure to the one you have uploaded. If you provided a clearer explanation of the desired output then we could help you achieve that.
Categorie
Scopri di più su Annotations in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!