- Create two overlapping axes, and make sure to align them carefully with one another.
- Manually position text objects to "fake" a second x-axis.
Plot two x-axis in a graph with Time(calendar month/year) on axis and Hour on another x-axis
78 views (last 30 days)
Harsimran Singh on 14 Mar 2022
hello guys, I am facing an issue in plotting, would really appreciate if some can help/suggest any method.
I have data in 3 columns,
Column #1= Date(YYYY-MM-DD)
Column #2= Time(hr)
Column #3= Temp
I want to plot a graph with Temperature on y-axis and need to have two x-axis. One x-axis should have Time(hr) and other x-axis should have Date(YYYY-MM-DD).
At the end I want time(Hr) should match with date on the plot. Means when date is 2021-05-18 on one x-axis, time on the another x-axis should be 1000.44, as per data
I am using Matlab R2021b.
Benjamin Kraus on 14 Mar 2022
I'm having some trouble interpretting your data. I'm not sure I understood completely how the dates align with the times.
For example, the first data point is March 5, 2021 and 30.36361 hours and the 199th data point is March 5, 2021 and 32.82417 hours, but the 200th data point is March 10, 2021 and 57.55306 hours. More than 25 hours elapsed between March 5 and March 10, so there is some detail I'm clearly missing.
Regardless: There isn't really a great way (currently) in MATLAB to create two x-axes. You have two (not great) options:
In addition, there isn't a built-in way to create a discontinuous datetime ruler (as in, a ruler that uses datetimes as input data, but has gaps in time along the ruler). The only way to recreate this would be to manually create this effect by setting the desired tick values and labels.
Here is my attempt at interpretting what you are trying to create, using the first approach (two overlapping axes):
tbl = readtable('matlabproblem.xlsx');
tbl.Time = hours(tbl.Time);
% Create a tiledlayout to keep two axes aligned with one another.
layout = tiledlayout(1,1);
% Plot the hours data.
ax1 = nexttile(layout);
plot(ax1, tbl.Time, tbl.Temp);
% Create a second axes for the date labels.
ax2 = axes(layout);
ax2.Layout.Tile = 1;
% Plot the same data in the second axes.
p = plot(ax2, tbl.Time, tbl.Temp);
% Make sure the two axes stay in-sync with one another.
% Hide the second axes and data, but keep the second set of rulers.
p.Visible = 'off';
ax2.Visible = 'off';
ax2.XAxisLocation = 'top';
ax2.XAxis.Visible = true;
ax2.YAxisLocation = 'right';
ax2.YAxis.Visible = true;
% Loop over the dates and create manual tick labels for each unique data.
alldates = unique(tbl.Date);
centers = tbl.Time(1:numel(alldates));
labels = string(alldates);
for d = 1:numel(alldates)
times = tbl.Time(tbl.Date == alldates(d));
centers(d) = mean([min(times) max(times)]);
More Answers (1)
Peter Perkins on 14 Mar 2022
This made little sense to me until I realized that the times were elapsed times from the beginning of data collection i hours, and not time of day. You can plot temp against a datetime variable, or against a duration variable (you may want to convert those numeric elapsed times to durations using the hours function), but not against both on the same axis. I suspect that you may end up making a plot against one or the other, and then adding the one you did not use as text.