meatball1982 发表于 2017-5-31 10:46:20

豆粑粑 matlab 断yaxis ,修改 breakYAxes.

别人的问题:
http://www.ilovematlab.cn/forum.php?mod=viewthread&tid=509797&page=1&extra=#pid3224636

clear all
clc

%% outline
% break y axis

%% main
x=linspace(0,10*pi,100);
% y1 = sqrt(3)/3*x +8;
y=exp(x/7)+sin(x)-30;


y_break_start = 10;
y_break_end = 25;
break_type_w = 'Wave';
% h=BreakPlot(x,y,y_break_start,y_break_end,break_type);
break_type_R = 'RPatch';

% figure;
subplot(1,2,1)
plot(x,y,'.')
% subplot(2,2,2)
% % BreakPlot(rand(1,21),,10,40,'Line');
% h=BreakPlot(x,y,y_break_start,y_break_end,break_type_R);
subplot(1,2,2)
h=BreakPlot(x,y,y_break_start,y_break_end,break_type_w);
% hold on
% plot(x,y1,'r-')


h=gcf;
fi_na=['./fig_split_y_axis'];
fun_work_li_035_myfig_out(h,fi_na,3);

%% logs
% mod : 31-May-2017 09:07:50
%


function h=BreakPlot(x,y,y_break_start,y_break_end,break_type)
% BreakPlot(x,y,y_break_start,y_break_end,break_type)
% Produces a plot who's y-axis skips to avoid unecessary blank space
%
% INPUT
% x
% y
% y_break_start
% y_break_end
% break_type
%    if break_type='RPatch' the plot will look torn
%       in the broken space
%    if break_type='Patch' the plot will have a more
%       regular, zig-zag tear
%    if break_plot='Line' the plot will merely have
%       some hash marks on the y-axis to denote the
%       break
%
% USAGE:
% figure;
% BreakPlot(rand(1,21),,10,40,'Line');
% figure;
% BreakPlot(rand(1,21),,10,40,'Patch');
% figure;
% BreakPlot(rand(1,21),,10,40,'RPatch');
% figure;
% x=rand(1,21);y=;
% subplot(2,1,1);plot(x(y>=40),y(y>=40),'.');
% set(gca,'XTickLabel',[]);
% subplot(2,1,2);plot(x(y<=20),y(y<=20),'.');
%
% IT'S NOT FANCY, BUT IT WORKS.

% Michael Robbins
% robbins@bloomberg.net
% michael.robbins@bloomberg.net

% data
if nargin<5 break_type='RPatch'; end;
if nargin<4 y_break_end=40; end;
if nargin<3 y_break_start=10; end;
if nargin<2 y=; end;
if nargin<1 x=rand(1,21); end;


y_break_mid=(y_break_end-y_break_start)./2+y_break_start;
= min(abs(y-y_break_mid));
x_break_mid = x(ind);
b_shift= y_break_start-sqrt(3)/3*x_break_mid;
b_shift
x_break_mid
y_break_mid
% erase useless data
x(y>y_break_start & y <y_break_end)=[];
y(y>y_break_start & y <y_break_end)=[];

% leave room for the y_break_end
% % =min(y>=y_break_end);
% % if i>y_break_end
% %   x=;
% %   y=;
% % end;

% remap
y2=y;
y2(y2>=y_break_end)=y2(y2>=y_break_end)-(y_break_end-y_break_start);

% plot
h=plot(x,y2,'.');
% h=plot(x,y,'.');

% make break
xlim=get(gca,'xlim');
ytick=get(gca,'YTick');
=min(ytick<=y_break_start);
y=(ytick(i)-ytick(i-1))./2+ytick(i-1);
dy=(ytick(2)-ytick(1))./7;
xtick=get(gca,'XTick');
x=xtick(1);
dx=(xtick(2)-xtick(1))./2;
n_int=100;
switch break_type
    case 'Wave',
         dx=(xlim(2)-xlim(1))./10;
%         xx = xlim(1)+dx.*;
      xx = linspace(xlim(1),xlim(end),n_int);
%         yy = sin(xx)+sqrt(3)/3*xx+ytick(i-1)+(ytick(i)-ytick(i-1))./2;
%      yy =sqrt(3)/3*xx+b_shift;
         yy = dx*sin(xx/6*pi)+ sqrt(3)/3*xx+b_shift;
      patch(, ...
      , ...
            )
      
    case 'Patch',
      % this can be vectorized
      dx=(xlim(2)-xlim(1))./10;
      yy=repmat(,1,6);
      xx=xlim(1)+dx.*;
      patch(, ...
            , ...
            )
    case 'RPatch',
                % this can be vectorized
      dx=(xlim(2)-xlim(1))./100;
      yy=y+rand(101,1).*2.*dy;
      xx=xlim(1)+dx.*(0:100);
                patch(, ...
            , ...
            [.8 .8 .8])
    case 'Line',
                line(,);
                line(,);
                line(,);
                line(,);
end;
set(gca,'xlim',xlim);

% map back
ytick(ytick>y_break_start)=ytick(ytick>y_break_start)+y_break_mid;
for i=1:length(ytick)
   yticklabel{i}=sprintf('%4.3d',ytick(i));
end;
set(gca,'yticklabel',yticklabel);

页: [1]
查看完整版本: 豆粑粑 matlab 断yaxis ,修改 breakYAxes.