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

6 次查看(过去 30 天)
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?

采纳的回答

Stephen23
Stephen23 2017-9-27
编辑:Stephen23 2017-9-27
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 个评论
BM
BM 2017-9-28
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 2017-9-28
" 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.

请先登录,再进行评论。

更多回答(1 个)

thalia
thalia 2018-11-5
Thanks works great!

类别

Help CenterFile Exchange 中查找有关 Line Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by