![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/178416/image.png)
How to plot row vectors in a matrix in which every marker is a different color and repeated amongst the other rows?
조회 수: 8 (최근 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?
댓글 수: 0
채택된 답변
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/178416/image.png)
댓글 수: 2
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개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Line Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!