MATLAB Answers

Combining lines in a graph

14 views (last 30 days)
Tracy Campbell
Tracy Campbell on 18 Sep 2017
Edited: KSSV on 18 Sep 2017
Hello,
I currently am able to make two separate graphs-but am trying to find a way to write the code so that both trials (CC and NW) appear on the same plot. The variable being graphed is the same, and so are the x and y axes. Below is the code I'm currently trying to use:
% Attempt to graph P Yield for multiple scenarios
startTime = now;
% NW=1,AI=2,CC=3,AR=4
load LULCmaster_50_CC_50.mat
% Do I need this for graph?
% watershed area
areaM2 = 27944*219.456^2;
areaKM2 = areaM2/1000000;
areaHa = areaM2/10000;
areaAc = areaM2/4046.86;
% Set scenario
scen = 1;
if scen == 1;
color = [1 0.114 0.145];
scStr = '50_CC_50';
end
% Set version
vers = 1;
if vers<10
versStr = ['0' num2str(vers)];
else
versStr = num2str(vers);
end
% Make sure correct avgAnn2 was used
avgAnn2 = zeros(85,20);
years = (1986:2070)';
% years = (1980:2070)';
s6 = '_85y';
% s6 = '_110y'
% P YIELD
var=5
s0 = 'prunoff';
s01 = 'atotrunp';
s1 = 'Annual Average P yield [kg/ha]';
s2 = ['P YIELD - ' scStr versStr];
s3 = ['Pyield_AvgAnnual_' scStr versStr];
ymin = 0;
ymax = 3.5;
s4 = ['/Users/Kucharikgradcomp/Desktop/Agro-IBIS_output/' scStr '/' s0 '.nc'];
% Read-in the netCDF datasets
run2 = read_netCDF(s4);
% Determine the proper variable index
ind = find(strcmp(run2.varnames,s01)==1);
% if variable name exists in netCDF, then plot and save figure
if isempty(ind)==0
% Determine the land-average value per year for run2
ann2 = run2.data{1,ind};
for k=1:size(ann2,3) % year
count2 = 0;
sum2 = 0;
for i=1:size(ann2,1)
for j=1:size(ann2,2)
if ann2(i,j,k) < 1E20
count2 = count2 + 1;
sum2 = sum2 + ann2(i,j,k);
end
end
end
avgAnn2(k,var) = sum2/count2;
end
end
% Create figure
figure1 = figure;
% Create axes
axes1 = axes('Parent',figure1,'YGrid','on','XGrid','on','FontSize',20);
ylim(axes1,[ymin ymax]);
xlim(axes1,[1980,2080]);
box(axes1,'on');
hold(axes1,'on');
% Create plot
plot(years,avgAnn2(:,var),'LineWidth',2,'Color',[0 0 0]);
hold on
%{
% plot(years,avgAnn2(var),'LineWidth',2,'Color',[0 0 0]);
% Create xlabel
xlabel({'Year'});
% Create ylabel
ylabel({s1},'FontSize',20);
% Create title
title({s2});
% s5 = [scStr versStr s6 '/yearly/' s3];
s5 = [scStr versStr s6 s3];
print(s5, '-djpeg', '-r300')
end
%}
% Add another scenario to plot
% Set scenario
scen = 2;
if scen == 2;
scStr = '50_NW_50';
color2 = [1 0.576 0.118];
end
% Set version
vers = 1;
if vers<10
versStr = ['0' num2str(vers)];
else
versStr = num2str(vers);
end
% Make sure correct avgAnn2 was used
avgAnn2 = zeros(85,20);
years = (1986:2070)';
% years = (1980:2070)';
s6 = '_85y';
% s6 = '_110y'
% P YIELD
var=5
s0 = 'prunoff';
s01 = 'atotrunp';
s1 = 'Annual Average P yield [kg/ha]';
s2 = ['P YIELD - ' scStr versStr];
s3 = ['Pyield_AvgAnnual_' scStr versStr];
ymin = 0;
ymax = 3.5;
s4 = ['/Users/Kucharikgradcomp/Desktop/Agro-IBIS_output/' scStr '/' s0 '.nc'];
% Read-in the netCDF datasets
run2 = read_netCDF(s4);
% Determine the proper variable index
ind = find(strcmp(run2.varnames,s01)==1);
% if variable name exists in netCDF, then plot and save figure
if isempty(ind)==0
% Determine the land-average value per year for run2
ann2 = run2.data{1,ind};
for k=1:size(ann2,3) % year
count2 = 0;
sum2 = 0;
for i=1:size(ann2,1)
for j=1:size(ann2,2)
if ann2(i,j,k) < 1E20
count2 = count2 + 1;
sum2 = sum2 + ann2(i,j,k);
end
end
end
avgAnn2(k,var) = sum2/count2;
end
% Create figure
figure2 = figure;
% Create axes
axes1 = axes('Parent',figure2,'YGrid','on','XGrid','on','FontSize',20);
ylim(axes1,[ymin ymax]);
xlim(axes1,[1980,2080]);
box(axes1,'on');
hold(axes1,'on');
% Create plot
plot(years,avgAnn2(:,var),'LineWidth',2,'Color',[0 0 0]);
% plot(years,avgAnn2(var),'LineWidth',2,'Color',[0 0 0]);
hold off
% Create xlabel
xlabel({'Year'});
% Create ylabel
ylabel({s1},'FontSize',20);
% Create title
title({s2});
% s5 = [scStr versStr s6 '/yearly/' s3];
s5 = [scStr versStr s6 s3];
print(s5, '-djpeg', '-r300')
end

  0 Comments

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by