cut out section of x axis
Show older comments
I am plotting two experiments on the same x axis. I want to keep the xaxis from 7/11 to 7/14 and 7/18 to 7/20, but I want to cut out the space inbetween (7/14 to 7/18). Is this possible.
This is my figure code now:
f1 = figure(1)
subplot(2,1,1)
hold on
plot(time_hr,ph_hr(1,:),'o','color','r','markersize',3);
plot(time_hr,ph_hr(2,:),'o','color',[1.0 .6 0.0],'markersize',3);
plot(time_hr,ph_hr(3,:),'o','color',[0 .4 .2],'markersize',3);
plot(time_hr,ph_hr(4,:),'o','color',[0 .6 1],'markersize',3);
plot(time_hr,ph_hr(5,:),'o','color',[.2 0 .6],'markersize',3);
% plot(MX,PK.PH(2,:),'color',[0 .4 .2])
% plot(MX,PK.PH(8,:),'color',[.2 0 .6])
set(gca,'fontsize', 16)
ylabel('pH')
title('HR2 and HR3')
x = datenum('July-11-2018'):1:datenum('July-20-2018');
xx = datenum('July-11-2018'):.25:datenum('July-20-2018');
set(gca,'XTick', x);
ax=gca;
ax.XAxis.MinorTick = 'on'
ax.XAxis.MinorTickValues = xx
datetick('x','mm/dd','keepticks')
xlim([datenum('July-11-2018 00:00') datenum('July-20-2018 00:00')]);
subplot(2,1,2)
hold on
plot(time_hr,temp_hr(1,:),'o','color','r','markersize',3);
plot(time_hr,temp_hr(2,:),'o','color',[1.0 .6 0.0],'markersize',3);
plot(time_hr,temp_hr(3,:),'o','color',[0 .4 .2],'markersize',3);
plot(time_hr,temp_hr(4,:),'o','color',[0 .6 1],'markersize',3);
plot(time_hr,temp_hr(5,:),'o','color',[.2 0 .6],'markersize',3);
% plot(MX,PK.TEMP(2,:),'color',[0 .4 .2])
% plot(MX,PK.TEMP(8,:),'color',[.2 0 .6])
set(gca,'fontsize', 16)
ylabel('Temperature \circC')
x = datenum('July-11-2018'):1:datenum('July-20-2018');
xx = datenum('July-11-2018'):.25:datenum('July-20-2018');
set(gca,'XTick', x);
ax=gca;
ax.XAxis.MinorTick = 'on'
ax.XAxis.MinorTickValues = xx
datetick('x','mm/dd','keepticks')
xlim([datenum('July-11-2018 00:00') datenum('July-20-2018 00:00')]);
4 Comments
Adam Danz
on 4 Sep 2019
What is time_hr? Could you give us some samples from that data?
Heidi Hirsh
on 4 Sep 2019
darova
on 4 Sep 2019
Can't you just move your data (see attached file)?
Or you want to change x ticks too?
Heidi Hirsh
on 4 Sep 2019
Accepted Answer
More Answers (1)
Kelly Kearney
on 4 Sep 2019
Rather than shifting data and manually overriding tick labels, I prefer to use multiple axes to achieve breaks like this. The advantage is that you don't have to make any modifications to your data, and all plot types are supported.
dt = hours(1);
t = [datetime(2019,7,11):dt:datetime(2019,7,14) datetime(2019,7,18):dt:datetime(2019,7,21)];
y = rand(length(t), 5);
tlim(1,:) = [datetime(2019,7,11) datetime(2019,7,14)];
tlim(2,:) = [datetime(2019,7,18) datetime(2019,7,21)];
dt = tlim(:,2) - tlim(:,1);
% Create two axes, right next to each other. The size of each
% matches the time interval to be plotted on each.
frac = dt./sum(dt);
dx = 0.8;
ax(1) = axes('position', [0.1 0.1 dx*frac(1) 0.8]);
ax(2) = axes('position', [0.1+frac(1)*dx 0.1 dx*frac(2) 0.8]);
% Plot the same data to both axes
for ii = 1:2
plot(ax(ii), t,y, 'o');
end
% Adjust axis limits so each axis shows a portion of the data.
% Also, turn off the color of the right y-axis so it looks
% like one continuous axis
set(ax, 'ylim', [0 1]);
set(ax(1), 'xlim', tlim(1,:), 'box', 'off');
set(ax(2), 'xlim', tlim(2,:), 'box', 'off', 'ycolor', 'none');
I've used datetimes in this example; the same technique will work with datenum and datetick if you have an older version of Matlab. Also, if you have an older version, you may have to settle for matching the color of the y-axis to the axis color (by default, white), since 'none' wasn't an option.
Categories
Find more on Data Exploration in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!