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

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

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.
Thanks Adam. I am working to tidy this up. The problem is I don't know how to use datetime on x axis when data is categorical, is that possilbe? Also, would you know how I can more responsibly plot the groups of the boxchart, rather than plotting on top of each other? It would be clearer to have them plotted as three groups per xtick. Each group is in a matrix of 144*22 double. Should I be conatenating these to one matrix and then labelling each cell in a separate matrix?
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?
Perhaps it should not be categorical. I have read the dates from a datasheet where date is pulled from a filename which contians serial numbers and datetime. I have attached them here.
I have also attached the dB data. I tried concatenating each group into a single matrix (dB_values), and then creating a separate matrix (C) which acts as a label for each cell in dB_values, but this still does not work when applying boxchart.
>> boxchart(dB_values,'GroupByColor',C)
Error using
matlab.graphics.chart.primitive.BoxChart
The name 'GroupByColor' is not an accessible
property for an instance of class
'matlab.graphics.chart.primitive.BoxChart'.
Error in boxchart (line 128)
H =
matlab.graphics.chart.primitive.BoxChart('Parent',
cax,...
I see now that ydata (dB_values) should be a vector, but how would I do this when I have 22 values on the x-axis...
I'll show you in my answer below.

Accedi per commentare.

Risposte (1)

Adam Danz
Adam Danz il 17 Nov 2020
Modificato: Adam Danz il 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 Commenti

Thanks Adam! This works, but only plots one set of dB values. I'd like to plot and group all three? This also unfortunately doesn't help me with aligning the axes of the two separate plots.
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).

Accedi per commentare.

Categorie

Prodotti

Release

R2020a

Richiesto:

il 16 Nov 2020

Commentato:

il 18 Nov 2020

Community Treasure Hunt

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

Start Hunting!

Translated by