필터 지우기
필터 지우기

how to plot a matrix graph of D(n,m) versus C(n,m) with different colors

조회 수: 51 (최근 30일)
albert Kinda
albert Kinda 2024년 7월 17일 9:35
댓글: albert Kinda 2024년 7월 21일 11:48
I would like to plot a graph of D versus C. For each i have a different color

채택된 답변

Ayush
Ayush 2024년 7월 17일 10:01
You can leverage the "Color" attribute of the "plot" function with a for loop to iterate over each i to plot a graph of D versus C with different colors. You can refer to the following documentation to know more about the "Color" Name-Value argument:
Here is an example code as well for additional reference built-on top of your code:
clc;
clear all;
close all;
C = zeros(12, 49);
D = zeros(12, 49);
for i = 1:12
for j = 1:49
C(i, j) = i * j;
D(i, j) = i - j;
end
end
% Define colors for each 'i'
colors = lines(12); % Using the 'lines' colormap for 12 different colors
% Plot D versus C with different colors for each 'i'
figure;
hold on;
for i = 1:12
plot(D(i, :), C(i, :), 'Color', colors(i, :), 'DisplayName', ['i = ' num2str(i)]);
end
hold off;
xlabel('D');
ylabel('C');
title('Plot of D versus C');
legend('show');
grid on;
  댓글 수: 4
albert Kinda
albert Kinda 2024년 7월 21일 11:48
Hi, how to have different markers for each i in order to differentiate them?

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Shantanu Dixit
Shantanu Dixit 2024년 7월 17일 10:23
Hi Albert,
It is my understanding that you want to plot the two matrix values against each other and have different colors corresponding to each row in the plots.
You can use ‘plot(X,Y,LineSpec)’ from ‘plot’ functionality in MATLAB.
plot(X,Y,LineSpec) creates the plot using the specified line style, marker, and color.
X cand Y point to the corresponding rows from D and C respectively. Colors can be defined using ‘colormap’ as a 12X3 matrix and be passed as LineSpec to the ‘plot’ function.
Additionally, you can refer to the following MathWorks documentation link for better understanding on plotting and colormap:
  1. https://www.mathworks.com/help/matlab/ref/plot.html
  2. https://www.mathworks.com/help/matlab/ref/colormap.html

Rahul
Rahul 2024년 7월 17일 10:30
I was able to use the code you provided to obtain the plot figure. The following changes can help you get the desired plot:
  • Firstly, I defined a colormap using the 'lines' function.
  • Further, I made the use of 'hold on' and 'hold off' in the loop while plotting to obtain all points in the same plot.
colors = lines(12);
hold on;
for i=1:1:12
for j=1:1:49
plot(D(i,j), C(i,j), '.', 'Color', colors(i, :)); % Use different color for each i
end
end
hold off;
You can refer to this link for more information regarding 'hold' function: https://www.mathworks.com/help/releases/R2024a/matlab/ref/hold.html?searchHighlight=hold&s_tid=doc_srchtitle
Hope this helps!

카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by