Search code examples
matlablayoutplotmatlab-figureaxis-labels

Cut/delete part of plot


I have the following plot:

enter image description here

As you can see, the plot has some data on the left (blue) and on the right (red). However, I want them to be closer together. For example by cutting the box area, because there is no data, such that the 'red area' is more shifted to the left (and thus closer to the 'blue area').

How to proceed further?

MWE:

x = 0:0.05:1.3
y = x;
plot(x,y,'color','k','Linewidth',1);
hold on;
x1 = [0.1:0.05:x(end)];
y1 = [0:0.05:y(end)-0.1];
plot(x1,y1,'--k','Linewidth',1);
x2 = [0:0.05:x(end)];
y2 = [0.10:0.05:y(end)+0.1];
plot(x2,y2,'--k','Linewidth',1);
xlim([0 x(end)]);
ylim([0 y(end)]);
hold on
for i = 1:9;
   a = plot(P(i).mHlfA1,P(i).sHlfA1, 'bx');
   hold on
   b = plot(P(i).mHlfA1,P(i).sLFA1, 'bo');
   hold on
   c = plot(P(i).mHlfA2,P(i).sHlfA2, 'rd');
   hold on
   d = plot(P(i).mHlfA2,P(i).sLFA2, 'r*');
   hold on;
end

Solution

  • Here is an alternative tweak (to that suggested in the comments), using the XtickLable:

    x = [1:10 200:2:230];
    y = 3.*x-9;
    % define the parts of he x-axis:
    xgap = 11;
    % pasting together the two parts:
    new_x = [x(1:xgap-1) x(xgap-1)+(x(xgap:end)-x(xgap))+(x(xgap+2)-x(xgap+1))];
    ax = axes;
    plot(ax,new_x,y,'o');
    % get the ticks to change:
    left_x_tick = ax.XTick(ax.XTick<=x(xgap-1));
    right_x_tick = ax.XTick(ax.XTick>x(xgap-1));
    slash = left_x_tick(end)+floor((right_x_tick(1)-left_x_tick(end))/2);
    ax.XTick = [left_x_tick slash right_x_tick];
    % get the spacing between ticks
    inc = right_x_tick - min(right_x_tick);
    % create new tick labels:
    new_tick = cellstr(num2str(inc.' + min(x(xgap:end))+(right_x_tick(1)-new_x(xgap))));
    % replace the tick labels in the plot
    ax.XTickLabel(ax.XTick>new_x(xgap)) = new_tick;
    % put a // where there is a gap:
    slash_loc = find(ax.XTick==slash,1,'first');
    ax.XTickLabel(slash_loc) = {'//'};
    ax.FontSize = 14;
    

    This is before:

    no gap in x axis

    and after:

    gap in x axis

    This is just an idea for workaround, it could be further adjusted to the specific needs.