Assigning Specific Color Values when MATLAB plots several data sets simultaneously.

조회 수: 9 (최근 30일)
I am having trouble trying to figure out how to change the color of my plots when I am plotting a 2D array of y-values, along with t-values....
The context of the problem is this: I am plotting PREDATOR vs. PREY relations in nature, and thus at each time value t, there are two y-values, one for the predators, and one for the prey. In previous plots, I could change the color of each line to a particular RGB calue (making very beautiful plots), but only because the plot function was fun once for each plot created. (and then the 'hold on;' function was used).... This time however, the two sets are plotted at the same time, so using ->
plot(t,y,'color',k');
will result in "both" plots becoming black. Otherwise, the default colors are used. I am sure there is a way to change this. Any suggestions? I have included my code below. The functions use a 2nd-order Runge Kutta Approximation for ODE's and two differential equations if that helps... Thank you in advance!
%% MAIN SCRIPT - Numerical Method; System of Linear 1rst Order ODE's.
% This is a demo fucntion to act as a resource for the solving of systems
% of 1rst order ODE's. This particular example is a linear fox and rabbit
% populations as the PREDATOR vs. PREY model developed by Lotka and
% Volterra.
close all;
clear all;
clc;
%MAIN SCRIPT
a=1; b=0.5; c=0.75; d=0.25; h=0.05;
R0=2; F0=1; tspan=[0 30];
[t,y]=RK2(@(t,y) FR(t,y,a,b,c,d), tspan, [R0 F0],h);
plot(t,y, 'Linewidth', 1.8)
%% Function Scripts
% Here we will define the ODE and Runge-Kutta Scripts to calculate our t,y
% values.
%ODE Definition Function
function yp = FR(t,y,a,b,c,d)
yp(1) = a*y(1)-b*y(1)*y(2);
yp(2) = -c*y(2)+d*y(1)*y(2);
end
%Runge-Kutta 2nd Order Accuracy Function
function [t,y] = RK2(f,tspan,y0,h)
t = tspan(1):h:tspan(2);
y(1,:)=y0;
for n=1:length(t)-1
k1=f(t(n),y(n,:));
k2=f(t(n)+h,y(n,:)+h*k1);
y(n+1,:)=y(n,:) + 0.5*h*(k1+k2);
end
end

채택된 답변

Mehmed Saad
Mehmed Saad 2020년 5월 13일
Set ColorOrder of axes. Currently i am setting it to random, You can select your custom color
close all;
clear all;
clc;
%MAIN SCRIPT
a=1; b=0.5; c=0.75; d=0.25; h=0.05;
R0=2; F0=1; tspan=[0 30];
[t,y]=RK2(@(t,y) FR(t,y,a,b,c,d), tspan, [R0 F0],h);
%
ax = gca;
ax.ColorOrder = rand(10,3);hold on
plot(t,y, 'Linewidth', 1.8)
  댓글 수: 1
Michael McDermott
Michael McDermott 2020년 5월 13일
Thank you!
I figured out how to set the 'gca' with an array of RGB values.
ax = gca;
ax.ColorOrder = 1/255*[[51 61 62]; 2.2*[51 61 62]; 3.6*[51 61 62]]
Here the 2.2 and 3.6 multipliers just adjust the "shade" of the original color. Thank you again for the help!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Visual Exploration에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by