MATLAB Answers

Aligning axes of two plots, one categorical axis, one datetime axis

조회 수: 11(최근 30일)
Louise Wilson
Louise Wilson 16 Nov 2020
댓글: Adam Danz 18 Nov 2020
I have the following figure.
As you can see, the x axes of the bottom two plots are perfectly aligned. These axes are datetime and so I was able to set the xlim and xticks the same on these two plots.
However, I would like the top plot to line up in some way too. At the moment, it's all over the place and the position of the xticks on the top plot changes in relation to the lower two plots, as you move to the right.
%Plot!
figure(7);
subplot(5,1,1)
boxchart(data_dB1);
hold on
boxchart(data_dB2);
boxchart(data_dB3);
ylim([80 150]);
xlabel('Date','FontSize',14);
ylabel('SPL_{rms}', 'FontSize',14);
xtickangle(45);
legend(["50-24,000 Hz","50-5000Hz","50-1500 Hz"]);
title(site,'FontSize',14);
axis tight
xticklabels(datestr(dates_concatenated1,'dd-mm-yy'));
%plot boat count data
hold on
subplot(5,1,2)
boatcounts_dn=datenum(boatcounts.DateTime);
scatter(boatcounts_dn,categorical(boatcounts.BoatCount));
startdate=datenum(2019,08,20,8,0,0);
enddate=datenum(2019,09,10,17,0,0);
xlim([startdate, enddate]) %set x axis limits
ax=gca; ax.YAxis.Limits=categorical({'0','1'}); %set y axis limits
%plot missing boat count data, if any
hold on
boatcounts_missing_dn=datenum(boatcounts_missing.DateTime);
scatter(boatcounts_missing_dn,categorical(boatcounts_missing.BoatCount),...
'MarkerEdgeColor',[0 0 0]);
legend(); legend(["Number of boats","Data deficient"]);
%add x tick labels
x_ticks=startdate:1:enddate;
xticks(x_ticks)
xtickangle(45);
x_dates=datetime(x_ticks,'ConvertFrom','datenum');
xticklabels(datestr(x_dates,'dd-mm-yy HH:MM'));
xlabel('Date','FontSize',14);
ylabel('Boat Number','FontSize',14);
%add wind
hold on
subplot(5,1,3)
axis tight
plot(gust_hourly.Date_NZST_,gust_hourly.Speed_m_s_);ylabel('Wind speed (m/s)','FontSize',14);
yyaxis right
plot(rain_hourly.Date_NZST_,rain_hourly.Amount_mm_);ylabel('Rain (mm)','FontSize',14);
startdate=datetime(2019,08,20,8,0,0);
enddate=datetime(2019,09,10,17,0,0);
xlim([startdate, enddate]) %set x axis limits
xticks(startdate:1:enddate);
xticklabels(datestr(x_dates,'dd-mm-yy HH:MM'));
xtickangle(45);
xlabel('Date','FontSize',14);
  댓글 수: 6
Adam Danz
Adam Danz 17 Nov 2020
I'll show you in my answer below.

댓글을 달려면 로그인하십시오.

답변(1개)

Adam Danz
Adam Danz 17 Nov 2020
편집: Adam Danz 17 Nov 2020
Convert the datetime strings to datetime values and use them as the grouping variable in the boxplot. Set the datetime format before creating the boxplot.
Also, this solution uses boxplot instead of boxchart because it's much more flexible.
boxplot(dB_values,datetime(dates_concatenated1,'format','dd-MM-yy'))
xtickangle(90)
  댓글 수: 2
Adam Danz
Adam Danz 18 Nov 2020
This answer addresses the problems you described in your comment above but it's not clear how you're doing the grouping.
It's a demonstration how to plot with datetime values. If you can apply this to your plots, you will be able to set the xlim for all x-axes so that your data are aligned.
I don't understand what this means: I'd like to plot and group all three (db values).

댓글을 달려면 로그인하십시오.

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by