Adding a second plot and y-axis to existing code.

1 visualizzazione (ultimi 30 giorni)
aaron Harvey
aaron Harvey il 15 Feb 2016
Risposto: Gautam il 18 Feb 2016
I have many water quality variables taken over many years at many depths. The date variable is a an array of doubles in the format yyyymmdd. I have written a code to plot a variable for a given depth range, between certain dates. But I would like to be able to add another variable onto the same plot with a secondary y-axis using the same date range. I got this far but i cant think of a way to get a second plot on there?
function [out1] = simpleplot(var,depthvar,mindepth,maxdepth,datevar,startdate,enddate,xlab,ylab);
% Convert date from doubles to datetime, and also date limits.
y = floor(datevar/10000);
m = floor(mod(datevar, 10000) / 100);
d = mod(datevar, 100);
date = datetime(y, m, d);
starty = floor(startdate/10000);
startm = floor(rem(startdate,10000)/100);
startd = rem(startdate,100);
datetime(starty, startm, startd); %check
endy = floor(enddate/10000);
endm = floor(rem(enddate,10000)/100);
endd = rem(startdate,100);
datetime(endy, endm, endd); %check
% Find variable for only given depth range. match date variable to the same dimensions also.
depth = find(depthvar>mindepth & depthvar<maxdepth & ~isnan(var));
datedepth=date(depth);
variable_depth=var(depth);
out1=plot(datedepth, variable_depth,'sk--');
xlabel(xlab);
ylabel(ylab);
xmin=datenum(datetime(starty, startm, startd));
xmax=datenum(datetime(endy, endm, endd));
xlim([xmin xmax]);
ylim([min(variable_depth) max(variable_depth)]);
end

Risposte (1)

Gautam
Gautam il 18 Feb 2016
Hi Aaron,
If you want to plot another varaible on the same plot with same scale(of y-axes), you could use the plot command. On the other hand, if you want to plot another variable on the same plot with different scale(of y-axes), you could use the 'plotyy' command. Refer the documentations of 'plot' and 'plotyy' commands for more information regarding this.
As an example, execute the following code snippet to help you get started:
clear all
x=1:10;
y=x.*x;
z=x.^3;
%plot y and z data v/s x data. (Note: Same scale for y-axes)
figure(1)
h=plot(x,y,x,z);
%edit plot properties
h(1).Marker='*';
h(1).MarkerEdgeColor='b';
h(2).Marker='o';
h(2).MarkerEdgeColor='r';
%plot y and z data v/s x data. (Note: Different scale for y-axes)
figure(2)
[Ax,hxa,hxb] = plotyy(x,y,x,z);
%edit plot properties
hxa(1).Marker='*';
hxa(1).MarkerEdgeColor='b';
hxb(1).Marker='o';
hxb(1).MarkerEdgeColor='r';
Hope this helps.

Categorie

Scopri di più su Line Plots in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by