Plotting a box plot and a time series on the same graph ? as a distribution over time. boxplot(re​shape(flux​O2tom01and​23,4,[])); and plot(o_opt​ode_mean1,​1:196)

boxplot(reshape(fluxO2tom01and23,4,[])); and
plot(o_optode_mean1,1:196),
I have tried
yyaxis left;
hAxL=gca;
boxplot(reshape(fluxO2tom01and23,4,[]));
yyaxis right;
hAxR=gca; %
plot(o_optode_mean1,1:196)
ylim(hAxL);
linkaxes([hAxL hAxR],'xy');
but it does not work, any ideas?

4 Comments

Are the two x-axes the same, as in, do you want to plot some distribution over time?
Thank you for your reply. Yes they both have the same number on the x axis - I want to look at the distribution over time
Interesting. Perhaps you can find something on fileexchange. If the question is not resolved I will give it a try later today.
I have looked there and can't find a solution, if you have time later that would be great. Thank you

Sign in to comment.

 Accepted Answer

I answered this in the previous Q? in response to the comment after you said you didn't have yyaxis, Rebecca... Answer_331785 (follow comments)

3 Comments

I have updated to Matlab R2018a and the script :
yyaxis left;
hAxL=gca; % get handle;
boxplot(reshape(fluxO2tom01and23,4,[]));
yyaxis right;
hAxR=gca; % ditto
plot(o_optode_mean1,1:48)
ylim(hAxL); % match up ylim to scale same
linkaxes([hAxL hAxR],'xy'); % keep in synch
doesnt draw the time series on the on the graph over the boxplot and the axis is wrong ? any ideas?
the optode data is just a time series to plot
ylim(hAxL); % match up ylim to scale same
is a "do nothing" statement--w/o a return value it would just echo the existing limits to screen but the semicolon suppresses the output...
Also
plot(o_optode_mean1,1:48)
doesn't look like correct syntax; if o_optode_mean1 is an ordinary array then plot would try to interpret it as the X value and 1:48 as Y values; if it were an actual timeseries object I think it would error...mayhaps you meant
plot(o_optode_mean1(1:48))
???
What does
whos o_optode_mean1
return?
If it is actually one of the timeseries classes; they have overloaded plot functions and the x-axis will be that time vector I suspect while boxplot is nothing but ordinal position; it doesn't have the facility to do anything else on x-axis (a major weakness, "but that's the way it is").
If it is a real timeseries, you'll have to retrieve the values into "regular" double and plot that instead to make the x axes overlay correctly.
This is a presumption based on simply the words chosen, can't see whether the assumption is true or not without supporting info.
yyaxis left; hAxL=gca; % get handle; boxplot(reshape(fluxO2tom01and23,4,[])); yyaxis right; hAxR=gca; % ditto plot(o_oxygen_hour(1:48)); ylim(hAxL); % match up ylim to scale same linkaxes([hAxL hAxR],'xy'); % keep in synch
works now thank you - how would I set the limits on the left axis to -10 and 10 ? and fix the x axis to every 5 numbers?

Sign in to comment.

More Answers (1)

Not sure if this is the complete answer, but you can vary the position of the boxes by the 'position' argument. datetime format is not supported but you can use the older format. See example:
%%Some data
X=randn(100,3)
%%Time vector
t=[datenum('2000-1-1') datenum('2001-1-1') datenum('2002-1-1')];
%%Plot line and boxplot
plot(t,[0 3 0]);hold on
boxplot(X,'position',t)
%%Fix axis and ticks
ax=gca;
tix=ax.XTick;
ax.XTickLabels=num2cell(tix)
datetick('x','yyyy-mm-dd','keepticks');

22 Comments

yyaxis left; hAxL=gca; % get handle; boxplot(reshape(fluxO2tom01and23,4,[])); yyaxis right; hAxR=gca; % ditto plot(o_oxygen_hour(1:48)); ylim(hAxL); % match up ylim to scale same linkaxes([hAxL hAxR],'xy'); % keep in synch
This works - i am just struggling to line up the left hand axis to -10 and 10 ? and only show a few numbers on the x axis?
Nice!
Are those xticks your actual values? Because the boxplot tends to mess up the 'XTickLabels'. No matter what you give it as 'position' input, it still labels the boxes from 1 to n, where n is the number of boxes. The actual value is however correct, just the label is wrong. You have to relabel the axis, which is simple:
ax=gca;
tix=ax.XTick;
ax.XTickLabels=num2cell(tix)
You can also reduce the number of ticks, just write something like to reduce them:
ax.XTick=tix(1:2:end);
Changing the limits of the y-axis is easy, just type
set(gca,'ylim',[-10 10])
when that axis is your current axis, i.e. right after you plot.
how would i change the left and right y axis? y1 and y2 ?
tix=ax.XTick;
Invalid or deleted object.
>> ax.XTickLabels=num2cell(tix) Invalid or deleted object.
>> ax.XTick=tix(1:2:end); Invalid or deleted object.
and set(gca,'ylim',[-10 10]) sets the only the right hand axis not the left?
and with x axis - datetick('x','2018-05-20','keepticks'); it has too many values?
Simply toggle the current axis before you change their properties
%%Change left axes
yyaxis left
set(gca,'ylim',[-10 10])
You can also do it like this:
ax=gca;
%%Change left
ax.YAxis(1).Limits=[-10 10];
%%Change right
ax.YAxis(2).Limits=[-10 5];
With regards to your second comment:
  • Please format your code by selecting all code and click the {} Code button.
  • Don't forget to type ax=gca first, probably why it says deleted object. Alternatively you closed the figure.
  • The format of datetick is wrong. It should say "yyyy-mm-dd", or any other format. Also, yes you have too many ticks. Reduce them further by changing the increment in this line, e.g.:
ax.XTick=tix(1:10:end);
How long is your time-series? You may not want to show the year in the XTickLabel format if you only have a few days, for example.
ax.XTick=tix(1:10:end); comes out as : Invalid or deleted object.
Then you want to use another format. You can use anything you want. Some examples:
'ddd HH:MM'
'ddd HH:MM AM' AM/PM, US format
'HH:MM'
'mm/dd HH:MM'
See this link for a complete list of syntax
On your other comment:
ax.XTick=tix(1:10:end); comes out as : Invalid or deleted object.
Please show me your correctly formated code. Have you defined ax?
Some comments
  • You are still not using the 'position' argument of boxplot?
  • I don't think you can save the right and left yaxis like you are doing with hAxL and hAxR. There is only one axes, so both of those are identical.
  • linkaxes..., I don't think this does anything at all. Try removing it.
  • datetick..., change format as I suggested above.
Dont you want to plot a time-vector? Right now the ticks on your xaxis are just numbers without significance. If you have a time-vector, use it as input in both boxplot (via the position property) and plot (as the first argument)
yyaxis left;
hAxL=gca; % get handle;
boxplot(reshape(fluxO2tom01and23,4,[]));
yyaxis right;
hAxR=gca; % ditto
plot(o_oxygen_hour(1:48));
ylim(hAxL); % match up ylim to scale same
linkaxes([hAxL hAxR],'xy'); % keep in synch
yyaxis left
set(gca,'ylim',[-10 10])
yyaxis right;
label 'optode oxygen')
datetick('x','2018-05-20','keepticks');
This is as far as I have gotten - just trying to sort out the lables and axis
I should reiterate that you need a time-vector, otherwise you can not fix your x-axis ticks.
yy
axis left;
hAxL=gca; % get handle;
boxplot(reshape(fluxO2tom01and23,4,[]));
yyaxis right;
hAxR=gca; % ditto
plot(o_oxygen_hour(1:48));
ylim(hAxL); % match up ylim to scale same
% linkaxes([hAxL hAxR],'xy'); % keep in synch
yyaxis left
set(gca,'ylim',[-10 10])
yyaxis right;
% label 'optode oxygen')
datetick('x','2018-05-20','keepticks');
I mean, the entire point of this answer was that you can give the time-vector as input to the boxplot through the 'position' property. I assume you have a time-vector that you want on the x-axis no? If t is your time-vector, you plot it by:
plot(t,o_oxygen_hour(1:48))
and
boxplot(reshape(fluxO2tom01and23,4,[]),'position',t);
Can you just give me the data? This correspondance is getting out of hand. I can fix this figure for you, but then you should do a basic MATLAB tutorial.
yyaxis left;
hAxL=gca; % get handle;
boxplot(reshape(fluxO2tom01and23,4,[]));
yyaxis right;
hAxR=gca; % ditto
plot(o_oxygen_hour(1:48));
ylim(hAxL); % match up ylim to scale same
% linkaxes([hAxL hAxR],'xy'); % keep in synch
yyaxis left
set(gca,'ylim',[-10 10])
yyaxis right;
% label 'optode oxygen')
datetick('x','2018-05-20','keepticks');
I will do a tutorial I apologize for the my lack of understanding on coding, sorry. Thank you very much for your help though.
My advice. Learn the basics of MATLAB! It will help you in the long run. If you ask about boxplots and yyaxis on this forum, people will assume that you know some basics about MATLAB. So, in order to appreciate the feedback, you need to know some basics :)
I fixed your code and added comments so that you can follow the steps. A popup will ask you to give the start of the time-series, as you did not provide any time-vector. Just write something like: 2000-05-03 23:00, or whatever your start time is.
%%Load data
data=load('matlabhelp.mat');
fluxO2tom01and23=data.fluxO2tom01and23;
o_oxygen_hour=data.o_oxygen_hour;
%%Request user input -- Start time -- Format -- year-month-day hour:minute
t_start=inputdlg('Enter start time, as yyyy-mm-dd HH:MM','s')
t_start=datenum(t_start{1});
%%Build time vector -- hourly values for two days
t=t_start:1/24:t_start+2;
%%Toggle left yaxis and plot boxplot
yyaxis left;
boxplot(reshape(fluxO2tom01and23,4,[]),'position',t);
%%Set left yaxis limits
set(gca,'ylim',[-10 10])
%%Toggle right yaxis and plot line
yyaxis right;
plot(t(1:48),o_oxygen_hour(1:48));
%Fix ticks
ax=gca;
ax.XTick=ax.XTick(1:12:end)
%Set xtickformat -- read more about possible formats:
%https://se.mathworks.com/help/matlab/ref/datestr.html
datetick('x','ddd HH:MM','keepticks');

Sign in to comment.

Tags

Asked:

on 8 Aug 2018

Edited:

on 8 Aug 2018

Community Treasure Hunt

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

Start Hunting!