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)
17 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
jonas
le 8 Août 2018
Interesting. Perhaps you can find something on fileexchange. If the question is not resolved I will give it a try later today.
Réponse acceptée
dpb
le 8 Août 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 commentaires
dpb
le 8 Août 2018
Modifié(e) : dpb
le 8 Août 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.
Plus de réponses (1)
jonas
le 8 Août 2018
Modifié(e) : jonas
le 8 Août 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 commentaires
jonas
le 8 Août 2018
Modifié(e) : jonas
le 8 Août 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');
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/193906/image.jpeg)
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!