How to plot row vectors in a matrix in which every marker is a different color and repeated amongst the other rows?

11 Ansichten (letzte 30 Tage)
I am trying to essentially plot three things on a single 2-D graph. That is, say we have a matrix
p(m,n) = [1 2 4; 3 5 7; 6 7 9];
Let us now say that the indices m,n in p correspond to different parameters in an equation. Now my matrix p contains the solutions T to this particular equation when the parameters m and n take on values 1-3. My goal is to be able to plot m vs. T, but I want each element in p(1,:) to have a different color, say p(1,1)=red, p(1,2)=green, and p(1,3) = blue. For the next row, m = 2, I would like p(2,1)=red, ... I would then like to be able to connect all of the red markers with a line, all of the green markers with a line, and all of the blue markers with a line. In this way, I would be able to see how the values of T change in my plot as m and n vary all within the same graph.
For starters, I can use a for-loop to produce vectors of all one value of the proper length, and store them in a matrix:
p = [1 2 4; 3 5 7; 6 7 9];
x = ones(1,3);
for i = 1:1:3
vec = i*x; % Prints out a vector [i i i ...] for each i from 1 to 10
n(i,:) = vec; % Stores vectors in matrix (note i iteration index is necessary to define matrix)
end
n % prints out entire matrix
plot(n(1,:),p(1,:),'*')
hold on
plot(n(2,:),p(2,:),'^')
plot(n(3,:),p(3,:),'.')
I would now like to have each column in the matrix be a different color and connected by a line. Thus, for all of the values where n=1, there would be a line connecting them together. Does anyone have any suggestions by how I might accomplish this?

Akzeptierte Antwort

Stephen23
Stephen23 am 27 Sep. 2017
Bearbeitet: Stephen23 am 27 Sep. 2017
Because the markers and lines are perpendicular to each other (wrt. the input variables) it is not possible to define them both with just one plot call. But it is easy with two plot calls:
p = [1,2,4;3,5,7;6,7,9;10,11,12];
s = size(p);
x = ndgrid(1:s(1),1:s(2));
axh = axes('ColorOrder',[0,0,1;0,1,0;1,0,0], 'NextPlot','add'); % colors
plot(axh,x,p); % plot lines
lnh = plot(axh,x.',p.','.k'); % plot markers
set(lnh,{'Marker'},{'*';'^';'+';'d'})
giving:
  2 Kommentare
BM
BM am 28 Sep. 2017
Stephen, thanks. I take it I cannot ever change marker colour within a set? Nevertheless, this will absolutely work! Thanks for the lesson!
Stephen23
Stephen23 am 28 Sep. 2017
" I take it I cannot ever change marker colour within a set?"
Yes, you can change the marker color using set. Note that markers of the same shape will have the same color because they are on one line.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

thalia
thalia am 5 Nov. 2018
Thanks works great!

Kategorien

Mehr zu Line Plots finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by