多轴断裂 [英] Multiple axis breaks

查看:105
本文介绍了多轴断裂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Matlab上创建具有多个轴中断的绘图(如下所示):

I'm trying to create a plot on Matlab with multiple axis breaks (so something like the following):

我已经尝试过使用Matlab文件交换中的breakyaxisbreakaxis之类的东西,但是这些只允许一个中断,而不是多个中断.

I've tried using things like breakyaxis and breakaxis from the Matlab File Exchange, but those only allow for one break, not multiple.

有什么办法可以实现?

推荐答案

NaN( N ot a N umber )个值可能是一件令人烦恼的事情,但在某些情况下也很方便.

The NaN (Not a Number) values can be an annoying thing but also a convenient one in some case.

在绘制数据时,Matlab将在每个没有值的数据点(NaN)处保留空白.因此,原理是将这些NaN插入数据集之间,并告诉Matlab绘制全部图.在有NaN的任何地方,Matlab都会自动将其留空.

When you plot data, Matlab will leave a blank in place of every data point which has no value (the NaN). So the principle is to insert these NaN between your datasets and tell Matlab to plot the whole lot. Matlab will leave a blank automatically everywhere there is a NaN.

这里是一个示例,由于您没有提供样本数据,因此我首先必须定义3个简短的数据集,这些数据集与您在图中的数据集相似:

Here is an example, since you didn't supply sample data I first have to define 3 short data sets resembling the ones you have in your figure:

%% // 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轴上的中断.为了能够对数据集调用全局操作,我必须将它们重新分组为单元格数组或结构.结构方法将在不同的数据集上使用循环,而单元格数组允许使用cellfun压缩代码.我选择了这种方法并广泛使用cellfun.

This is an edited answer to take into account the breaks in the Y axis. To be able to call global operations on the datasets I have to regroup them into a cell array or a structure. The struture approach would use loops on the different data sets, while the cell array allow the use of cellfun to compact the code. I chose this approach and use cellfun extensively.

因此,第一步是将所有数据集放入单元格数组中

So first step is put all your data sets in a cell array

%% // 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 )

这应该给你类似的东西:

That should give you something like:

希望有足够的机会让您入门.尝试使原理适应您的数据.

Hopefully enough to get you started. Try to adapt the principle to your data.

这篇关于多轴断裂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆