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)

22 visualizzazioni (ultimi 30 giorni)
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 Commenti
jonas
jonas il 8 Ago 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
Rebecca Ellis il 8 Ago 2018
I have looked there and can't find a solution, if you have time later that would be great. Thank you

Accedi per commentare.

Risposta accettata

dpb
dpb il 8 Ago 2018
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 Commenti
dpb
dpb il 8 Ago 2018
Modificato: dpb il 8 Ago 2018
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 il 8 Ago 2018
Modificato: Rebecca Ellis il 8 Ago 2018
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?

Accedi per commentare.

Più risposte (1)

jonas
jonas il 8 Ago 2018
Modificato: jonas il 8 Ago 2018
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 Commenti
Rebecca Ellis
Rebecca Ellis il 8 Ago 2018
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 il 8 Ago 2018
Modificato: jonas il 8 Ago 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');

Accedi per commentare.

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by