声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 10980|回复: 20

[绘图技巧] 多纵坐标多参数画图

[复制链接]
发表于 2009-11-29 20:32 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
这帖http://forum.vibunion.com/thread-88597-1-1.html引起个人一些兴趣, 搜索了也到官网逛了下, 整理下希望有用!
在"请教高手如何绘制多y 轴的曲线图啊"http://forum.vibunion.com/thread-24244-1-1.html 中,  bainhome给的plotyyy, 其原始网址在 http://www.mathworks.com/matlabcentral/fileexchange/1017-plotyyy
新的有注解, 可能较容易看懂! 方便阅读列出如下, 其中所附例子的图亦附上

  1. function [ax,hlines] = plotyyy(x1,y1,x2,y2,x3,y3,ylabels)
  2. %PLOTYYY - Extends plotyy to include a third y-axis
  3. %
  4. %Syntax:  [ax,hlines] = plotyyy(x1,y1,x2,y2,x3,y3,ylabels)
  5. %
  6. %Inputs: x1,y1 are the xdata and ydata for the first axes' line
  7. %        x2,y2 are the xdata and ydata for the second axes' line
  8. %        x3,y3 are the xdata and ydata for the third axes' line
  9. %        ylabels is a 3x1 cell array containing the ylabel strings
  10. %
  11. %Outputs: ax -     3x1 double array containing the axes' handles
  12. %         hlines - 3x1 double array containing the lines' handles
  13. %
  14. %Example:
  15. %x=0:10;
  16. %y1=x;  y2=x.^2;   y3=x.^3;
  17. %ylabels{1}='First y-label';
  18. %ylabels{2}='Second y-label';
  19. %ylabels{3}='Third y-label';
  20. %[ax,hlines] = plotyyy(x,y1,x,y2,x,y3,ylabels);
  21. %legend(hlines, 'y = x','y = x^2','y = x^3',2)
  22. %
  23. %m-files required: none

  24. %Author: Denis Gilbert, Ph.D., physical oceanography
  25. %Maurice Lamontagne Institute
  26. %Dept. of Fisheries and Oceans Canada
  27. %email: gilbertd@dfo-mpo.gc.ca  
  28. %Web: http://www.qc.dfo-mpo.gc.ca/iml/
  29. %April 2000; Last revision: 14-Nov-2001

  30. if nargin==6
  31.    %Use empty strings for the ylabels
  32.    ylabels{1}=' '; ylabels{2}=' '; ylabels{3}=' ';
  33. elseif nargin > 7
  34.    error('Too many input arguments')
  35. elseif nargin < 6
  36.    error('Not enough input arguments')
  37. end

  38. figure('units','normalized',...
  39.        'DefaultAxesXMinorTick','on','DefaultAxesYminorTick','on');

  40. %Plot the first two lines with plotyy
  41. [ax,hlines(1),hlines(2)] = plotyy(x1,y1,x2,y2);
  42. cfig = get(gcf,'color');
  43. pos = [0.1  0.1  0.7  0.8];
  44. offset = pos(3)/5.5;

  45. %Reduce width of the two axes generated by plotyy
  46. pos(3) = pos(3) - offset/2;
  47. set(ax,'position',pos);

  48. %Determine the position of the third axes
  49. pos3=[pos(1) pos(2) pos(3)+offset pos(4)];

  50. %Determine the proper x-limits for the third axes
  51. limx1=get(ax(1),'xlim');
  52. limx3=[limx1(1)   limx1(1) + 1.2*(limx1(2)-limx1(1))];
  53. %Bug fix 14 Nov-2001: the 1.2 scale factor in the line above
  54. %was contributed by Mariano Garcia (BorgWarner Morse TEC Inc)

  55. ax(3)=axes('Position',pos3,'box','off',...
  56.    'Color','none','XColor','k','YColor','r',...   
  57.    'xtick',[],'xlim',limx3,'yaxislocation','right');

  58. hlines(3) = line(x3,y3,'Color','r','Parent',ax(3));
  59. limy3=get(ax(3),'YLim');

  60. %Hide unwanted portion of the x-axis line that lies
  61. %between the end of the second and third axes
  62. line([limx1(2) limx3(2)],[limy3(1) limy3(1)],...
  63.    'Color',cfig,'Parent',ax(3),'Clipping','off');
  64. axes(ax(2))

  65. %Label all three y-axes
  66. set(get(ax(1),'ylabel'),'string',ylabels{1})
  67. set(get(ax(2),'ylabel'),'string',ylabels{2})
  68. set(get(ax(3),'ylabel'),'string',ylabels{3})
复制代码


[ 本帖最后由 ChaChing 于 2009-11-30 09:59 编辑 ]
plotyyy.jpg

点评

赞成: 5.0
赞成: 5
  发表于 2013-12-10 16:20

评分

1

查看全部评分

回复
分享到:

使用道具 举报

 楼主| 发表于 2009-11-30 00:42 | 显示全部楼层

三纵坐标多参数画图

原先的plotyyy执行多参数时会报错(即当y1/y2/y3为矩阵时), 我稍微修改下!

  1. function [ax,hlines] = plotyyyM(x1,y1,x2,y2,x3,y3,ylabels)
  2. %PLOTYYY - Extends plotyy to include a third y-axis
  3. %
  4. %Syntax:  [ax,hlines] = plotyyy(x1,y1,x2,y2,x3,y3,ylabels)
  5. %
  6. %Inputs: x1,y1 are the xdata and ydata for the first axes' line
  7. %        x2,y2/x3,y3 are the second/third axes' line
  8. %        ylabels is a 3x1 cell array containing the ylabel strings
  9. %
  10. %Outputs: ax -     3x1 double array containing the axes' handles
  11. %         hlines - nlinex1 double array containing the lines' handles
  12. %
  13. %m-files required: none
  14. %Author: Denis Gilbert, Ph.D., physical oceanography
  15. %Maurice Lamontagne Institute, Dept. of Fisheries and Oceans Canada
  16. %email: gilbertd@dfo-mpo.gc.ca, Web: http://www.qc.dfo-mpo.gc.ca/iml/
  17. %April 2000; Last revision: 14-Nov-2001

  18. if nargin==6, ylabels{1}=' '; ylabels{2}=' '; ylabels{3}=' ';
  19. elseif nargin > 7, error('Too many input arguments')
  20. elseif nargin < 6, error('Not enough input arguments'); end

  21. figure('units','normalized','DefaultAxesXMinorTick','on','DefaultAxesYminorTick','on');
  22. [ax,hlines1,hlines2] = plotyy(x1,y1,x2,y2); %Plot the first two lines
  23. cfig = get(gcf,'color'); pos = [0.1 0.1 0.7 0.8]; offset = pos(3)/5.5;

  24. pos(3) = pos(3)-offset/2; set(ax,'position',pos); %Reduce width of the two axes
  25. pos3=[pos(1) pos(2) pos(3)+offset pos(4)]; %the position of the third axes

  26. %Determine the proper x-limits for the third axes
  27. %Bug fix 14 Nov-2001: the 1.2 scale factor in the line above
  28. %was contributed by Mariano Garcia (BorgWarner Morse TEC Inc)
  29. limx1=get(ax(1),'xlim'); limx3=[limx1(1) limx1(1)+1.2*(limx1(2)-limx1(1))];
  30. ax(3)=axes('Position',pos3,'box','off', 'Color','none','XColor','k', ...
  31.            'YColor','r','xtick',[],'xlim',limx3,'yaxislocation','right');
  32. hlines3 = line(x3,y3,'Color','r','Parent',ax(3));
  33. limy3=get(ax(3),'YLim');

  34. %Hide unwanted portion of the x-axis line that lies
  35. %between the end of the second and third axes
  36. line([limx1(2) limx3(2)],[limy3(1) limy3(1)],'Color',cfig,'Parent',ax(3),'Clipping','off');
  37. axes(ax(2))

  38. %Label all three y-axes
  39. set(get(ax(1),'ylabel'),'string',ylabels{1})
  40. set(get(ax(2),'ylabel'),'string',ylabels{2})
  41. set(get(ax(3),'ylabel'),'string',ylabels{3})

  42. hlines = [hlines1; hlines2; hlines3];
复制代码
试试下

  1. x=(0:10)'; nn=[1,0.9,0.8,0.7,0.6]; nL=length(nn);
  2. y1=x*nn; y2=x.^2*nn; y3=x.^3*nn;
  3. ylabels={'First y-label','Second y-label','Third y-label'};
  4. [ax,hlines] = plotyyyM(x,y1,x,y2,x,y3,ylabels);
  5. legend(hlines(1:nL),num2str(nn'),2)
复制代码

[ 本帖最后由 ChaChing 于 2009-11-30 18:43 编辑 ]
plotyyyM.jpg
 楼主| 发表于 2009-11-30 10:05 | 显示全部楼层

四纵坐标ploty4

另还有一个四纵坐标ploty4, 其原始网址在http://www.mathworks.com/matlabcentral/fileexchange/4425
方便阅读列出如下, 其中所附例子的图亦附上

  1. function [ax,hlines] = ploty4(x1,y1,x2,y2,x3,y3,x4,y4,ylabels)
  2. %PLOTY4     Extends plotyy to include a third and fourth y-axis
  3. %
  4. % Syntax:   [ax,hlines] = ploty4(x1,y1,x2,y2,x3,y3,x4,y4,ylabels)
  5. %
  6. % Inputs:   x1,y1 are the xdata and ydata for the first axes' line
  7. %           x2,y2 are the xdata and ydata for the second axes' line
  8. %           x3,y3 are the xdata and ydata for the third axes' line
  9. %           x4,y4 are the xdata and ydata for the fourth axes' line
  10. %           ylabels is a 4x1 cell array containing the ylabel strings (optional)
  11. %
  12. % Outputs:  ax -        4x1 double array containing the axes' handles
  13. %           hlines -    4x1 double array containing the lines' handles
  14. %
  15. % Example:
  16. %           x = 0:10;
  17. %           y1=x;  y2=x.^2;  y3=x.^3;  y4=x.^4;
  18. %           ylabels{1} = 'First y-label';
  19. %           ylabels{2} = 'Second y-label';
  20. %           ylabels{3} = 'Third y-label';
  21. %           ylabels{4} = 'Fourth y-label';
  22. %           [ax,hlines] = ploty4(x,y1,x,y2,x,y3,x,y4,ylabels);
  23. %           leghandle = legend(hlines, 'y = x','y = x^2','y = x^3','y = x^4',2);
  24. %
  25. % See also Plot, Plotyy
  26. % Based on plotyyy.m (available at www.matlabcentral.com) by Denis Gilbert, Ph.D.

  27. % Check inputs
  28. msg=nargchk(8,9,nargin); error(msg);

  29. % Create figure window
  30. figure('units','normalized',...
  31.        'DefaultAxesXMinorTick','on','DefaultAxesYminorTick','on');

  32. %Plot the first two lines with plotyy
  33. [ax,hlines(1),hlines(2)] = plotyy(x1,y1,x2,y2);
  34. cfig = get(gcf,'color'); pos = [0.125 0.1 0.65 0.8]; offset = pos(3)/5.5;

  35. %Reduce width of the two axes generated by plotyy
  36. pos(1) = pos(1) + offset; pos(3) = pos(3) - offset;
  37. set(ax,'position',pos);

  38. %Determine the position of the third/fourth axes
  39. pos3 = [pos(1) pos(2) pos(3)+offset pos(4)];
  40. pos4 = [pos(1) - offset pos(2) pos(3)+offset pos(4)];

  41. %Determine the proper x-limits for the third and fourth axes
  42. scale3 = pos3(3)/pos(3); scale4 = pos4(3)/pos(3);
  43. limx1 = get(ax(1),'xlim');
  44. limx3 = [limx1(1) limx1(1)+scale3*(limx1(2)-limx1(1))];
  45. limx4 = [limx1(2)-scale4*(limx1(2)-limx1(1)) limx1(2)];

  46. %Create ax(3) & ax(4)
  47. ax(3) = axes('Position',pos3,'box','off',...
  48.    'Color','none','XColor',cfig,'YColor','r',...
  49.    'xtick',[],'xlim',limx3,'yaxislocation','right');
  50. ax(4) = axes('Position',pos4,'box','off',...
  51.    'Color','none','XColor',cfig,'YColor','k',...
  52.    'xtick',[],'xlim',limx4,'yaxislocation','left');

  53. %Plot x3,y3,x4,y4
  54. hlines(3) = line(x3,y3,'Color','r','Parent',ax(3));
  55. hlines(4) = line(x4,y4,'Color','k','Parent',ax(4));

  56. %Put ax(2) on top;
  57. axes(ax(2));

  58. %Set y-labels;
  59. if nargin==9
  60. set(cell2mat(get(ax,{'ylabel'})),{'String'},{ylabels{:}}');
  61. end
复制代码

[ 本帖最后由 ChaChing 于 2009-11-30 13:27 编辑 ]
ploty4.jpg
 楼主| 发表于 2009-11-30 10:05 | 显示全部楼层

四纵坐标多参数画图

与2F相同, 原先的ploty4执行多参数时会报错(即当y1/y2/y3/y4为矩阵时), 我稍微修改下!

  1. function [ax,hlines] = ploty4(x1,y1,x2,y2,x3,y3,x4,y4,ylabels)
  2. %PLOTY4     Extends plotyy to include a third and fourth y-axis
  3. %
  4. % Syntax:   [ax,hlines] = ploty4(x1,y1,x2,y2,x3,y3,x4,y4,ylabels)
  5. %
  6. % Inputs:   x1,y1 are the xdata and ydata for the first axes' line
  7. %           x2,y2 are the xdata and ydata for the second axes' line
  8. %           x3,y3 are the xdata and ydata for the third axes' line
  9. %           x4,y4 are the xdata and ydata for the fourth axes' line
  10. %           ylabels is a 4x1 cell array containing the ylabel strings (optional)
  11. %
  12. % Outputs:  ax -        4x1 double array containing the axes' handles
  13. %           hlines -    nlinex1 double array containing the lines' handles
  14. %
  15. % See also Plot, Plotyy
  16. % Based on plotyyy.m (available at www.matlabcentral.com) by Denis Gilbert, Ph.D.

  17. % Check inputs
  18. msg=nargchk(8,9,nargin); error(msg);

  19. % Create figure window
  20. figure('units','normalized','DefaultAxesXMinorTick','on','DefaultAxesYminorTick','on');

  21. %Plot the first two lines with plotyy
  22. [ax,hlines1,hlines2] = plotyy(x1,y1,x2,y2);
  23. cfig = get(gcf,'color'); pos = [0.125 0.1 0.65 0.8]; offset = pos(3)/5.5;

  24. %Reduce width of the two axes generated by plotyy
  25. pos(1) = pos(1) + offset; pos(3) = pos(3) - offset;
  26. set(ax,'position',pos);

  27. %Determine the position of the third/fourth axes
  28. pos3 = [pos(1) pos(2) pos(3)+offset pos(4)];
  29. pos4 = [pos(1) - offset pos(2) pos(3)+offset pos(4)];

  30. %Determine the proper x-limits for the third and fourth axes
  31. scale3 = pos3(3)/pos(3); scale4 = pos4(3)/pos(3);
  32. limx1 = get(ax(1),'xlim');
  33. limx3 = [limx1(1) limx1(1)+scale3*(limx1(2)-limx1(1))];
  34. limx4 = [limx1(2)-scale4*(limx1(2)-limx1(1)) limx1(2)];

  35. %Create ax(3) & ax(4)
  36. ax(3) = axes('Position',pos3,'box','off',...
  37.    'Color','none','XColor',cfig,'YColor','r',...
  38.    'xtick',[],'xlim',limx3,'yaxislocation','right');
  39. ax(4) = axes('Position',pos4,'box','off',...
  40.    'Color','none','XColor',cfig,'YColor','k',...
  41.    'xtick',[],'xlim',limx4,'yaxislocation','left');

  42. %Plot x3,y3,x4,y4
  43. hlines3 = line(x3,y3,'Color','r','Parent',ax(3));
  44. hlines4 = line(x4,y4,'Color','k','Parent',ax(4));

  45. %Put ax(2) on top;
  46. axes(ax(2));

  47. %Set y-labels;
  48. if nargin==9
  49. set(cell2mat(get(ax,{'ylabel'})),{'String'},{ylabels{:}}');
  50. end
  51. hlines = [hlines1; hlines2; hlines3; hlines4];
复制代码
试试下
  1. x=(0:10)'; nn=[1,0.9,0.8,0.7,0.6]; nL=length(nn);
  2. y1=x*nn; y2=x.^2*nn; y3=x.^3*nn; y4=x.^4*nn;
  3. ylabels={'First y-label','Second y-label','Third y-label','Fourth y-label'};
  4. [ax,hlines] = ploty4M(x,y1,x,y2,x,y3,x,y4,ylabels);
  5. legend(hlines(1:nL),num2str(nn'),2)
复制代码

[ 本帖最后由 ChaChing 于 2009-11-30 18:53 编辑 ]
ploty4M.jpg

评分

1

查看全部评分

发表于 2009-11-30 18:11 | 显示全部楼层

回复 楼主 ChaChing 的帖子

顶楼主,好好研究下,正是需要的
关于legend,不写成function,怎么画呢
请教对于线性如何更改,谢谢
楼主的主要是针对函数问题,如果是一些数据呢,对于不同参数的问题,如何应用呢,另外请教线型如果想自定义怎么弄得,谢谢

[ 本帖最后由 ChaChing 于 2009-11-30 18:34 编辑 ]
 楼主| 发表于 2009-11-30 18:59 | 显示全部楼层

回复 5楼 jhonbilly 的帖子

上面输入的y1/y2.., 并非针对函数, 而是矩阵数据!
对于不同参数的问题, 个人以为并不合适绘在同一图上, 虽然应该也可以!
每一条line都有对应之handle, 若要改变default, 在legend前先设定好LineStyle/Color/Marker...

还有请善用编辑功能, 方便别人阅读, 否则会有灌水嫌疑!
新手暂不处罚! 拜托下, 很浪费个人时间, 时间花在这处理, 就没空回帖!

自己先试下吧! 要拜肚子了

[ 本帖最后由 ChaChing 于 2010-2-23 08:37 编辑 ]
 楼主| 发表于 2010-1-7 23:26 | 显示全部楼层

两个横坐标或两个纵坐标

感觉这个连接使这个更完整
http://www.mathworks.com/matlabcentral/fileexchange/7426
例子可见: 两个横坐标一个纵坐标 http://forum.vibunion.com/thread-83012-1-1.html
  1. function [ax, h] = plot2axes(varargin)
  2. %PLOT2AXES Graphs one set of data with two sets of axes
  3. %
  4. %   PLOT2AXES(X, Y, 'Option1', 'Value1', ...) plots X versus Y with secondary
  5. %   axes.  The following options are accepted [default values]:
  6. %
  7. %     XLoc ['top']: location of secondary X-axis
  8. %     YLoc ['right']: location of secondary Y-axis
  9. %     XScale [1]: scaling factor for secondary X-axis (scalar)
  10. %     YScale [1]: scaling factor for secondary Y-axis (scalar)
  11. %                 
  12. %                 XScale and YScale can also be a character string
  13. %                 describing the relationship between the 2 axes, such as
  14. %                 the equation relating Celsius and Fahrenheit: '5/9*(x-32)'
  15. %
  16. %       Note: PLOT2AXES only works when the relationship is linear.
  17. %             Non-linearly related axes (e.g. 1/x, x^2, etc) do not work.
  18. %
  19. %     XLim [NaN NaN] : XLim in the primary axes (secondary is adjusted
  20. %                      accordingly). The default is naturally selected by
  21. %                      the plotting function.
  22. %     YLim [NaN NaN] : YLim in the primary axes (secondary is adjusted
  23. %                      accordingly). The default is naturally selected by
  24. %                      the plotting function.
  25. %
  26. %   PLOT2AXES(@FUN, ...) uses the plotting function @FUN instead of PLOT to
  27. %   produce the plot.  @FUN should be a function handle to a plotting
  28. %   function, e.g. @plot, @semilogx, @semilogy, @loglog ,@stem, etc. that
  29. %   accepts the syntax H = FUN(...).  Optional arguments accepted by these
  30. %   plotting functions are also allowed (e.g. PLOT2AXES(X, Y, 'r*', ...))
  31. %
  32. %   [AX, H] = PLOT2AXES(...) returns the handles of the primary and
  33. %   secondary axes (in that order) in AX, and the handles of the graphic
  34. %   objects in H.
  35. %
  36. %   The actual data is plotted in the primary axes.  The primary axes lie
  37. %   on top of the secondary axes.  After the execution of this function,
  38. %   the primary axes become the current axes.  If the next plot replaces
  39. %   the axes, the secondary axes are automatically deleted.
  40. %
  41. %   When you zoom in or out using the toolbar, it would only zoom the
  42. %   primary axes, so you should click on the 'Fix Axes' menu at the top of
  43. %   the figure to re-adjust the secondary axes limits.
  44. %
  45. %   PLOT2AXES('FixAxes') fixes the secondary limits of all figures created
  46. %   using plot2axes.
  47. %   
  48. %   Example 1:
  49. %     x = 0:.1:1;
  50. %     y = x.^2 + 0.1*randn(size(x));
  51. %     [ax, h] = plot2axes(x, y, 'ro', 'YScale', 25.4);
  52. %     title('Length vs Time');
  53. %     set(get(ax(1), 'ylabel'), 'string', 'inch');
  54. %     set(get(ax(2), 'ylabel'), 'string', 'millimeter');
  55. %     xlabel('time (sec)');
  56. %
  57. %   Example 2:
  58. %     [ax, h] = plot2axes(x, y, 'ro', 'YScale', '5/9*(x-32)');
  59. %     set(get(ax(1), 'ylabel'), 'string', 'Fahrenheit');
  60. %     set(get(ax(2), 'ylabel'), 'string', 'Celcius');
  61. %
  62. % VERSIONS:
  63. %   v1.0 - first version
  64. %   v1.1 - added option to specify X and Y limits
  65. %   v1.2 - remove tick labels for secondary axes if no scaling factors are
  66. %          specified. Also, fixed bug in matching the scaling type (linear
  67. %          or log).
  68. %   v1.3 - added the 'Fix Axes' menu for adjusting the secondary axes limits
  69. %          after zooming.
  70. %   v1.4 - added the option for specifying an equation for XScale and YScale
  71. %          (June 2005)
  72. %   v1.5 - fixed problem plotting on uipanel, where the parent of the axes
  73. %          is not a figure. (Feb 2006)
  74. %
  75. % Jiro Doke (Inspired by ideas from Art Kuo, Univ of Michigan)
  76. % March 2005
  77. %
  78. if nargin < 1
  79.   error('Not enough input arguments');
  80. end
  81. if nargin == 1 && strcmpi(varargin{1}, 'FixAxes')
  82.   figsH = findobj('Type', 'figure');
  83.   if ~isempty(figsH)
  84.     for iFig = 1:length(figsH)
  85.       p2a = getappdata(figsH(iFig), 'p2a');
  86.       FixAxes([], [], p2a);
  87.     end
  88.   end
  89.   return;
  90. end
  91. % Default options
  92. options{1} = 'top';       % XLoc
  93. options{2} = 'right';     % YLoc
  94. options{3} = 1;           % XScale
  95. options{4} = 1;           % YScale
  96. options{5} = [NaN NaN];   % XLim
  97. options{6} = [NaN NaN];   % YLim
  98. opts = {'XLoc', ...
  99.     'YLoc', ...
  100.     'XScale', ...
  101.     'YScale', ...
  102.     'XLim', ...
  103.     'YLim'};
  104. var = varargin;
  105. % Check to see if the first argument is a function handle
  106. if isa(var{1}, 'function_handle');
  107.   func   = var{1};
  108.   var(1) = '';
  109. else
  110.   func = @plot;
  111. end
  112. % Parse through input arguments for options
  113. try
  114.   removeID = [];
  115.   for iVar = 1:length(var)
  116.     if ischar(var{iVar})
  117.       id = strmatch(lower(var{iVar}), lower(opts), 'exact');
  118.       if ~isempty(id)
  119.         options{id} = var{iVar + 1};
  120.         removeID = [removeID, iVar, iVar + 1];
  121.       end
  122.     end
  123.   end
  124. catch
  125.   error('Error parsing options.\n%s\n', lasterr);
  126. end
  127. % Verify options
  128. if ~ismember(lower(options{1}), {'top', 'bottom'}) || ...
  129.     ~ismember(lower(options{2}), {'right', 'left'}) || ...
  130.     ~(isnumeric(options{5}) && length(options{5}) == 2) || ...
  131.     ~(isnumeric(options{6}) && length(options{6}) == 2)
  132.   error('Bad options');
  133. end
  134. var(removeID) = '';
  135. % Determine the axes to plot
  136. ax1 = newplot;
  137. nextplot = get(ax1, 'NextPlot');
  138. figH = gcf;
  139. % Plot data
  140. try
  141.   h = feval(func, var{:});
  142. catch
  143.   error('Failed to plot\n%s\n', lasterr);
  144. end
  145. set(ax1, 'Box', 'off', 'Color', 'none');
  146. % Create secondary axes on top of primary axes
  147. ax2 = axes(...
  148.   'Position', get(ax1, 'Position'), ...
  149.   'Box'     , 'off', ...
  150.   'Parent'  , get(ax1, 'Parent'));
  151. %--------------------------------------------------------------------------
  152. % Apply options
  153. %--------------------------------------------------------------------------
  154. if strcmpi(options{1}, 'top') % XLoc
  155.   set(ax1, 'XAxisLocation', 'bottom');
  156.   set(ax2, 'XAxisLocation', 'top');
  157. else
  158.   set(ax1, 'XAxisLocation', 'top');
  159.   set(ax2, 'XAxisLocation', 'bottom');
  160. end
  161. if strcmpi(options{2}, 'right') % YLoc
  162.   set(ax1, 'YAxisLocation', 'left');
  163.   set(ax2, 'YAxisLocation', 'right');
  164. else
  165.   set(ax1, 'YAxisLocation', 'right');
  166.   set(ax2, 'YAxisLocation', 'left');
  167. end
  168. if ~all(isnan(options{5}))  % XLim
  169.   set(ax1, 'XLim', options{5});
  170. end
  171. if ~all(isnan(options{6}))  % YLim
  172.   set(ax1, 'YLim', options{6});
  173. end
  174. if ischar(options{3}) % for functional relationship
  175.   tmp = inline(options{3});
  176.   set(ax2, 'XLim', fliplr(tmp(get(ax1, 'XLim'))),'Xdir','reverse');
  177. %  set(ax2, 'XLim', tmp(get(ax1, 'XLim')));
  178. else
  179.   set(ax2, 'XLim', get(ax1, 'XLim') * options{3})
  180. end
  181. if ischar(options{4})
  182.   tmp = inline(options{4});
  183.   set(ax2, 'YLim', tmp(get(ax1, 'YLim')));
  184. else
  185.   set(ax2, 'YLim', get(ax1, 'YLim') * options{4})
  186. end
  187. set(ax2, 'XScale', get(ax1, 'XScale'), ...
  188.   'YScale', get(ax1, 'YScale'));
  189. if options{3} == 1 % if there is no scaling, remove tick labels
  190.   set(ax2, 'XTickLabel', '');
  191. end
  192. if options{4} == 1 % if there is no scaling, remove tick labels
  193.   set(ax2, 'YTickLabel', '');
  194. end
  195. %--------------------------------------------------------------------------
  196. % Create DeleteProxy objects (an invisible text object) so that the other
  197. % axes will be deleted properly.  <inspired by PLOTYY>
  198. %--------------------------------------------------------------------------
  199. DeleteProxy(1) = text(...
  200.   'Parent'          , ax1, ...
  201.   'Visible'         , 'off', ...
  202.   'HandleVisibility', 'off');
  203. DeleteProxy(2) = text(...
  204.   'Parent'          , ax2, ...
  205.   'Visible'         , 'off', ...
  206.   'HandleVisibility', 'off', ...
  207.   'UserData'        , DeleteProxy(1));
  208. set(DeleteProxy(1), ...
  209.   'UserData'        , DeleteProxy(2));
  210. set(DeleteProxy, ...
  211.   'DeleteFcn'       , @DelFcn);
  212. %--------------------------------------------------------------------------
  213. % Switch the order of axes, so that the secondary axes are under the
  214. % primary axes, and that the primary axes become the current axes.
  215. %--------------------------------------------------------------------------
  216. % get list of figure children. ax1 and ax2 must exist in this list
  217. ch = get(get(ax1, 'Parent'), 'Children');
  218. i1 = find(ch == ax1);       % find where ax1 is
  219. i2 = find(ch == ax2);       % find where ax2 is
  220. ch([i1, i2]) = [ax2; ax1];  % swap ax1 and ax2
  221. % assign the new list of children and set current axes to primary
  222. set(get(ax1, 'Parent'), 'Children', ch);
  223. set(figH, 'CurrentAxes', ax1);
  224. % Restore NextPlot property (just in case it was modified)
  225. set([ax1, ax2], 'NextPlot', nextplot);
  226. % Store axes information
  227. p2a = getappdata(figH, 'p2a');
  228. if isempty(p2a)
  229.   p2a = {};
  230. end
  231. p2a = [p2a;{ax1, ax2, options{3}, options{4}}];
  232. setappdata(figH, 'p2a', p2a);
  233. % Create 'Fix Axes' button for adjusting the secondary axes limits after
  234. % zooming
  235. hMenu = findobj(figH, 'Type', 'uimenu', 'Label', 'Fix Axes');
  236. if strcmpi(get(figH, 'Menubar'), 'figure') && ...
  237.     (isempty(hMenu) || ~ishandle(hMenu))
  238.   uimenu(...
  239.     'Parent'  , figH, ...
  240.     'Label'   , 'Fix Axes', ...
  241.     'Callback', @FixAxes);
  242. end
  243. if nargout
  244.   ax = [ax1, ax2];
  245. end
  246. %--------------------------------------------------------------------------
  247. % DelFcn - automatically delete both axes
  248. %--------------------------------------------------------------------------
  249. function DelFcn(obj, edata)
  250. try
  251.   set(get(obj, 'UserData'), ...
  252.     'DeleteFcn', 'try;delete(get(gcbo, ''UserData''));end');
  253.   set(obj, 'UserData', ...
  254.     get(get(obj, 'UserData'), 'Parent'));
  255.   delete(get(obj,'UserData'));
  256. end

  257. %--------------------------------------------------------------------------
  258. % FixAxes - fix the scaling of the axes
  259. %--------------------------------------------------------------------------
  260. function FixAxes(obj, edata, p2a)
  261. if nargin < 3
  262.   p2a = getappdata(get(obj, 'Parent'), 'p2a');
  263. end
  264. if ~isempty(p2a)
  265.   for iAx = 1:size(p2a, 1)
  266.     if ishandle(p2a{iAx, 1}) && ishandle(p2a{iAx, 2})
  267.       if ischar(p2a{iAx, 3})
  268.         tmp = inline(p2a{iAx, 3});
  269.         set(p2a{iAx, 2}, 'XLim', tmp(get(p2a{iAx, 1}, 'XLim')));
  270.       else
  271.         set(p2a{iAx, 2}, 'XLim', p2a{iAx, 3} * get(p2a{iAx, 1}, 'XLim'));
  272.       end
  273.       if ischar(p2a{iAx, 4})
  274.         tmp = inline(p2a{iAx, 4});
  275.         set(p2a{iAx, 2}, 'YLim', tmp(get(p2a{iAx, 1}, 'YLim')));
  276.       else
  277.         set(p2a{iAx, 2}, 'YLim', p2a{iAx, 4} * get(p2a{iAx, 1}, 'YLim'));
  278.       end
  279.     end
  280.   end
  281. end
复制代码


[ 本帖最后由 ChaChing 于 2010-8-10 14:49 编辑 ]
发表于 2010-5-15 22:51 | 显示全部楼层
我今天是到处找资料研究这个弄半天了,就是要搞多纵坐标,而且要把坐标重合在一起,这篇帖子太及时了...
由衷感谢!!!!
发表于 2011-4-2 16:07 | 显示全部楼层
太厉害了!!!!!
发表于 2011-4-3 09:52 | 显示全部楼层
这个资料好啊,我正愁怎么弄呢
发表于 2011-4-7 14:13 | 显示全部楼层
很全面啊
发表于 2011-12-19 15:52 | 显示全部楼层
太感谢了
发表于 2012-3-29 17:05 | 显示全部楼层
太强大了!先留着,慢慢学。
我想沿着横坐标延伸的方向连续绘四个高斯脉冲,用的是同一个高斯分布函数来生成,只是四个峰的高矮胖瘦不全相同,因此要设置每个高斯脉冲函数的参数,并且要把各个y与x对应的点一一保存为txt。我的程序如下,为何plot只显示第二个峰还没有第一个峰呢(程序里暂时只设置了两个峰,打算以后再弄四个)?
function []=gs02()
%diary gs2901.txt
a = 1;
b1 = 1513;b2=1535;
c1 = 0.3;c2=0.2;
g1 = @(t)a*exp(-4*log2((t-b1)/c1).^2);
g2 = @(t)a*exp(-4*log2((t-b2)/c2).^2);
x = 1513:0.005:1550;
y1 = g1(x);
y2=g2(x);
plot(x,y1,'-*')
plot(x,y2,'-*')
xlabel('wavelength'),ylabel('guangqiang');
dlmwrite('datademo1.txt',[x',y1'],'newline','pc');
dlmwrite('datademo2.txt',[x',y2'],'newline','pc');

点评

与本主题不相符??  发表于 2012-3-30 01:09
发表于 2012-3-30 16:50 | 显示全部楼层
回复 13 # mingmingtree 的帖子

囧~  就是想请教,当时看到这个页面,竟然不知不觉就到这了。
 楼主| 发表于 2012-3-31 01:27 | 显示全部楼层
回复 14 # mingmingtree 的帖子

不要紧, 我晓得谢老师与LS好像解决了!
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-5-14 14:57 , Processed in 0.076840 second(s), 28 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表