Multiple Axial Breaks

I am trying to create a graph on Matlab with several axial gaps (something like the following):

enter image description here

I tried to use things such as breakyaxisand breakaxisfrom the Matlab file file sharing, but it allows only one break, not a few.

Is there any way to implement this?

+4
source share
1 answer

Values NaN( N ot a N ) can be an unpleasant thing, but also convenient in this case.

, Matlab , (NaN). , , NaN Matlab , . Matlab , NaN.

, , 3 , , :

%% // sample data sets
yf = @(x) 2*x+40+randi(7,size(x)) ;
x1 = 57:61 ; y1 = yf(x1) ;
x2 = 72:76 ; y2 = yf(x2) ;
x3 = 80:83 ; y3 = yf(x3) ;

, Y. , . struture , cellfun . cellfun.

,

%% // have to group the data sets in a cell array or structure to implement global operations
xc = { x1 ; x2 ; x3 } ;
yc = { y1 ; y2 ; y3 } ;

:

%// find the maximum vertical span of the datasets and the total span
maxVal = cellfun(@max,yc) ;
minVal = cellfun(@min,yc) ;
maxYspan  = max( maxVal-minVal ) ;
totalSpan = max(maxVal)-min(minVal) ;

%// find a sensible Y value to add between the datasets, not too wide but
%// enough to see a break`
yBreakIncrement = round( totalSpan / 10 ) ; %// adjust that if necessary
yTickIncrement = round( maxYspan /5 ) ;     %// adjust that if necessary

%% // rebuild the Y datasets 
%// value to substract to each data set to bring them together (including the break space)
setSubstract = [0 ; cumsum( (minVal(2:end)-maxVal(1:end-1))- yBreakIncrement )  ] ;
%// get 3 new data sets brought together
Yall = cellfun(@minus , yc , num2cell(setSubstract) , 'uni',0) ;
%// concatenate the data sets, inserting NaN in the middle
Yall = cellfun( @(a,b) cat(2,a,b) , Yall , repmat({NaN},length(yc),1) , 'uni',0) ;
Yall = cat( 2, Yall{:} ) ;
%// remove the last trailing NaN
Yall(end) = [] ;

%% // Build the Y labels
%// generate ticks that covers each interval
Y_tickpos = cellfun(@colon, num2cell(minVal), repmat({yTickIncrement},length(yc),1) , num2cell(maxVal) , 'uni',0) ;
%// generate the Y labels based the real Y values
Y_labels  = cellstr( num2str( cat(2, Y_tickpos{:} ).') ) ;   %'// ignore this comment
%// now adjust the actual position
Y_tickpos = cellfun(@minus , Y_tickpos , num2cell(setSubstract) , 'uni',0) ;
Y_tickpos = cat( 2, Y_tickpos{:} ) ;

%% // Build the X labels (and axis)
%// create a continuous index for the X axis
X = 1:length(Yall) ; 
X_labels = cellstr( num2str( cat(2, xc{:} ).') ) ;  %'// generate the X labels based the X values
X_tickpos = X(~isnan(Yall)) ;                       %// prepare a vector for the label positions

%% // Display
plot(X,Yall) %// plot as usual 

%// Set the labels at the chosen positions
set(gca, 'XTick' , X_tickpos , 'XTickLabel' , X_labels )
set(gca, 'YTick' , Y_tickpos , 'YTickLabel' , Y_labels )

- :
axis break

, . .

+4

All Articles