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)

17 次查看(过去 30 天)
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 个评论
jonas
jonas 2018-8-8
Interesting. Perhaps you can find something on fileexchange. If the question is not resolved I will give it a try later today.

请先登录,再进行评论。

采纳的回答

dpb
dpb 2018-8-8
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 个评论
dpb
dpb 2018-8-8
编辑:dpb 2018-8-8
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.
Rebecca Ellis
Rebecca Ellis 2018-8-8
编辑:Rebecca Ellis 2018-8-8
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?

请先登录,再进行评论。

更多回答(1 个)

jonas
jonas 2018-8-8
编辑:jonas 2018-8-8
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 个评论
Rebecca Ellis
Rebecca Ellis 2018-8-8
I will do a tutorial I apologize for the my lack of understanding on coding, sorry. Thank you very much for your help though.
jonas
jonas 2018-8-8
编辑:jonas 2018-8-8
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');

请先登录,再进行评论。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by