Plotting a box plot and a time series on the same graph ? as a distribution over time. boxplot(reshape(fluxO2tom01and23,4,[])); and plot(o_optode_mean1,1:196)
Show older comments
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
jonas
on 8 Aug 2018
Are the two x-axes the same, as in, do you want to plot some distribution over time?
Rebecca Ellis
on 8 Aug 2018
jonas
on 8 Aug 2018
Interesting. Perhaps you can find something on fileexchange. If the question is not resolved I will give it a try later today.
Rebecca Ellis
on 8 Aug 2018
Accepted Answer
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
Rebecca Ellis
on 8 Aug 2018
Edited: Rebecca Ellis
on 8 Aug 2018
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.
Rebecca Ellis
on 8 Aug 2018
Rebecca Ellis
on 8 Aug 2018
Edited: Rebecca Ellis
on 8 Aug 2018
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);
Rebecca Ellis
on 8 Aug 2018
jonas
on 8 Aug 2018
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.
Rebecca Ellis
on 8 Aug 2018
Rebecca Ellis
on 8 Aug 2018
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?
Rebecca Ellis
on 8 Aug 2018
jonas
on 8 Aug 2018
Glad to help. Can I see the end result? :)
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)
Rebecca Ellis
on 8 Aug 2018
jonas
on 8 Aug 2018
I should reiterate that you need a time-vector, otherwise you can not fix your x-axis ticks.
Rebecca Ellis
on 8 Aug 2018
Rebecca Ellis
on 8 Aug 2018
Rebecca Ellis
on 8 Aug 2018
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.
Rebecca Ellis
on 8 Aug 2018
Rebecca Ellis
on 8 Aug 2018
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');

Categories
Find more on Exploration and Visualization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



