• matlab实现3个y轴绘图


    通过改写plotyy,可实现3个y轴的绘图,效果还不错,分享下实现。忘了以前哪个大佬写的代码了,贴上来记录下,侵权请告知。

    实现代码如下:

    1. function [ax,hlines] = multiplotyyy(set1,set2,set3,ylabels)
    2. % MULTIPLOTYYY - Extends plotyy to include a third y-axis and allows the
    3. % user to plot multiple lines on each set of axes.
    4. %
    5. % Syntax: [ax,hlines] = plotyyy(set1,set2,set3,ylabels)
    6. %
    7. % Inputs: set1 is a cell array with the xdata and ydata for the first axes
    8. % set2 is a cell array with the xdata and ydata for the second axes
    9. % set3 is a cell array with the xdata and ydata for the third axes
    10. % ylabels is a 3x1 cell array containing the ylabel strings
    11. %
    12. % Outputs: ax - 3x1 double array containing the axes' handles
    13. % hlines - 3x1 cell array containing the lines' handles
    14. %
    15. % Example:
    16. % x1 = (0:0.01:1)';
    17. % x2 = (0:0.1:1)';
    18. % x3 = (0:0.05:1)';
    19. % y1 = x1;
    20. % y2 = x2.^2;
    21. % y3 = x3.^3;
    22. % y4 = sin(x1);
    23. % y5 = fliplr(2*x1.^2);
    24. % y6 = 7*cos(x1);
    25. % y7 = 7*log(x1+1.2);
    26. % ylabels{1}='First y-label';
    27. % ylabels{2}='Second y-label';
    28. % ylabels{3}='Third y-label';
    29. % [ax,hlines] = multiplotyyy({x1,y1,x2,y2,x3,y3,x1,y4},{x1,y5},{x1,[y6,y7]},ylabels);
    30. % legend(cat(1,hlines{:}),'a','b','c','d','e','f','g','location','w')
    31. %
    32. % Based on plotyyy.m (available at www.matlabcentral.com) by :
    33. % Denis Gilbert, Ph.D.
    34. %
    35. % Author: Laura L. Proctor
    36. % December 23, 2012
    37. %
    38. narginchk(3,4)
    39. if nargin==3
    40. % Use empty strings for the ylabels
    41. ylabels{1}=' '; ylabels{2}=' '; ylabels{3}=' ';
    42. end
    43. validateattributes(set1,{'cell'},{})
    44. validateattributes(set2,{'cell'},{})
    45. validateattributes(set3,{'cell'},{})
    46. fh = figure('units','normalized');
    47. cfig = get(fh,'Color');
    48. % Preallocate the outputs
    49. ax = zeros(3,1);
    50. hlines = cell(3,1);
    51. % Plot the first set of lines
    52. ax(1) = axes('Parent',fh);
    53. hlines{1} = plot(set1{:},'Color','b');
    54. set(ax(1),'YColor','b')
    55. lines = set(hlines{1}(1),'LineStyle');
    56. lines(end) = [];
    57. nlines = numel(lines);
    58. markers = set(hlines{1}(1),'Marker');
    59. markers(end) = [];
    60. nmarkers = numel(markers);
    61. if numel(hlines{1}) > 1
    62. for idx = 1:numel(hlines{1})
    63. set(hlines{1}(idx),'LineStyle',lines{rem(idx,nlines)+1})
    64. if numel(hlines{1}) > 4
    65. set(hlines{1}(idx),'Marker',markers{rem(idx,nmarkers)+1});
    66. end
    67. end
    68. end
    69. % Plot the second set of lines
    70. ax(2) = axes('Parent',fh);
    71. hlines{2} = plot(set2{:},'Color',[0 0.5 0]);
    72. set(ax(2),'YAxisLocation','right','Color','none','YColor',[0 0.5 0],...
    73. 'xlim',get(ax(1),'xlim'),'xtick',[],'box','off','XColor','k');
    74. if numel(hlines{2}) > 1
    75. for idx = 1:numel(hlines{2})
    76. set(hlines{2}(idx),'LineStyle',lines{rem(idx,nlines)+1})
    77. if numel(hlines{2}) > 4
    78. set(hlines{2}(idx),'Marker',markers{rem(idx,nmarkers)+1});
    79. end
    80. end
    81. end
    82. % Set the axes position and size
    83. pos = [0.1 0.1 0.7 0.8];
    84. offset = pos(3)/5.5;
    85. pos(3) = pos(3) - offset/2;
    86. set(ax(1),'Position',pos);
    87. set(ax(2),'Position',pos);
    88. % Determine the position of the third axes
    89. pos3=[pos(1) pos(2) pos(3)+offset pos(4)];
    90. % Determine the proper x-limits for the third axes
    91. limx1=get(ax(1),'xlim');
    92. limx3=[limx1(1) limx1(1) + 1.2*(limx1(2)-limx1(1))];
    93. ax(3) = axes('Parent',fh);
    94. hlines{3} = plot(set3{:},'Color','r');
    95. set(ax(3),'Position',pos3,'box','off',...
    96. 'Color','none','XColor','k','YColor','r',...
    97. 'xtick',[],'xlim',limx3,'yaxislocation','right');
    98. if numel(hlines{3}) > 1
    99. for idx = 1:numel(hlines{3})
    100. set(hlines{3}(idx),'LineStyle',lines{rem(idx,nlines)+1})
    101. if numel(hlines{3}) > 4
    102. set(hlines{3}(idx),'Marker',markers{rem(idx,nmarkers)+1});
    103. end
    104. end
    105. end
    106. limy3=get(ax(3),'YLim');
    107. % Hide unwanted portion of the x-axis line that lies between the end of the
    108. % second and third axes
    109. line([limx1(2) limx3(2)],[limy3(1) limy3(1)],...
    110. 'Color',cfig,'Parent',ax(3),'Clipping','off');
    111. axes(ax(2))
    112. % Label all three y-axes
    113. set(get(ax(1),'ylabel'),'string',ylabels{1})
    114. set(get(ax(2),'ylabel'),'string',ylabels{2})
    115. set(get(ax(3),'ylabel'),'string',ylabels{3})

    调用:

    1. x1 = (0:0.01:1)';
    2. x2 = (0:0.1:1)';
    3. x3 = (0:0.05:1)';
    4. y1 = x1;
    5. y2 = x2.^2;
    6. y3 = x3.^3;
    7. y4 = sin(x1);
    8. y5 = fliplr(2*x1.^2);
    9. y6 = 7*cos(x1);
    10. y7 = 7*log(x1+1.2);
    11. ylabels{1}='First y-label';
    12. ylabels{2}='Second y-label';
    13. ylabels{3}='Third y-label';
    14. [ax,hlines] = multiplotyyy({x1,y1,x2,y2,x3,y3,x1,y4},{x1,y5},{x1,[y6,y7]},ylabels);
    15. legend(cat(1,hlines{:}),'a','b','c','d','e','f','g','location','w')

    绘图效果:

     

  • 相关阅读:
    【深度学习21天学习挑战赛】2、复杂样本分类识别——卷积神经网络(CNN)服装图像分类
    mysql的max_allowed_packet配置
    华为机试真题 C++ 实现【打印机队列】【2022.11 Q4 新题】
    Mapbox 与 Babylon.js 可视化 添加地形
    options.html 页面设计成聊天框,左侧是功能列表,右侧是根据左侧的功能切换成不同的内容。--chatGpt
    计算机毕设 opencv 图像识别 指纹识别 - python
    CH9101国产USB转异步串口芯片兼容替代PL2303GC/PL2303HXD/FT230X/FT232RQ/CY7C65213
    debian安装Elasticsearch 8单节点
    品牌百度百科应该怎样创建?编辑品牌百度百科词条的秘籍!
    第一百三十七回 WebView
  • 原文地址:https://blog.csdn.net/wxyczhyza/article/details/126599505