Aligning axes of two plots, one categorical axis, one datetime axis
Mostra commenti meno recenti
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 Commenti
Adam Danz
il 16 Nov 2020
A lot of this mess can be avoided by plotting in datetime values instead of using date numbers and then changing the xticklabels to datetime format.
You're doing a lot of unnecessary work that can be greatly simplified by using datetime values for the x-coordinates.
Louise Wilson
il 16 Nov 2020
Adam Danz
il 16 Nov 2020
Why is the date categorical?
Are you reading it or producing the dates as categories?
Could you tell us more info about where those categorical datetime values come from and what they look like?
Louise Wilson
il 16 Nov 2020
Modificato: Louise Wilson
il 16 Nov 2020
Louise Wilson
il 16 Nov 2020
Adam Danz
il 17 Nov 2020
I'll show you in my answer below.
Risposte (1)
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 Commenti
Louise Wilson
il 18 Nov 2020
Modificato: Louise Wilson
il 18 Nov 2020
Adam Danz
il 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).
Categorie
Scopri di più su Axes Appearance in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!