cut out section of x axis
98 次查看(过去 30 天)
显示 更早的评论
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 个评论
darova
2019-9-4
Can't you just move your data (see attached file)?
Or you want to change x ticks too?
采纳的回答
Adam Danz
2019-9-4
This solution identifies all data with dates past 7/18 and shifts them down by 5 days. Then it overrides the x ticks with custom xticklabels.
load('data.mat')
% define gap boundaries
bounds = datenum({'07/14/2018','07/18/2018'}); %[start,stop] of gap
% shift data that are beyond bound to the start of the bound
shift = range(bounds);
time_hr_altered = time_hr - (time_hr >= bounds(2))*shift;
f1 = figure(1)
subplot(2,1,1)
hold on
plot(time_hr_altered,ph_hr(1,:),'o','color','r','markersize',3);
plot(time_hr_altered,ph_hr(2,:),'o','color',[1.0 .6 0.0],'markersize',3);
plot(time_hr_altered,ph_hr(3,:),'o','color',[0 .4 .2],'markersize',3);
plot(time_hr_altered,ph_hr(4,:),'o','color',[0 .6 1],'markersize',3);
plot(time_hr_altered,ph_hr(5,:),'o','color',[.2 0 .6],'markersize',3);
% Set up daily ticks
xtic = floor(min(time_hr_altered)) : ceil(max(time_hr_altered));
xtic_altered = xtic + (xtic >= bounds(1))*shift;
xticLabels = datestr(xtic_altered, 'mm/dd');
set(gca,'XTick', xtic, 'XTickLabel', xticLabels);
% Draw vertical reference line where the dates are discontinuous
plot(bounds([1,1]), [min(ylim),max(ylim)], 'k--'); %use xline() if r2018b or later
2 个评论
Adam Danz
2019-9-4
I haven't used that function so I'm not sure what it does.
The demo in my answer plots more than one set of (x,y) so I'm not sure what you're asking.
If you want to add the "//" to the top and bottom of the axes,
ylim(ylim()); %set y ax lim so it doesn't change
text(xtic(xtic == bounds(1)), min(ylim()), '//','fontsize',15,'HorizontalAlignment', 'center')
text(xtic(xtic == bounds(1)), max(ylim()), '//','fontsize',15,'HorizontalAlignment', 'center')
更多回答(1 个)
Kelly Kearney
2019-9-4
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Axis Labels 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!