爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
查看: 13276|回复: 12

[秀图] matlab多Y轴作图

[复制链接]

新浪微博达人勋

发表于 2013-11-18 09:58:55 | 显示全部楼层 |阅读模式

登录后查看更多精彩内容~

您需要 登录 才可以下载或查看,没有帐号?立即注册 新浪微博登陆

x
来源于:http://www.matlabsky.com/thread-23732-1-1.html

function [ax,hlines] = ploty4(x1,y1,x2,y2,x3,y3,x4,y4,ylabels)
%PLOTY4     Extends plotyy to include a third and fourth y-axis
%
% Syntax:   [ax,hlines] = ploty4(x1,y1,x2,y2,x3,y3,x4,y4,ylabels)
%
% Inputs:   x1,y1 are the xdata and ydata for the first axes' line
%           x2,y2 are the xdata and ydata for the second axes' line
%           x3,y3 are the xdata and ydata for the third axes' line
%           x4,y4 are the xdata and ydata for the fourth axes' line
%           ylabels is a 4x1 cell array containing the ylabel strings (optional)
%
% Outputs:  ax -        4x1 double array containing the axes' handles
%           hlines -    4x1 double array containing the lines' handles
%
% Example:
%           x = 0:10;
%           y1=x;  y2=x.^2;  y3=x.^3;  y4=x.^4;
%           ylabels{1} = 'First y-label';
%           ylabels{2} = 'Second y-label';
%           ylabels{3} = 'Third y-label';
%           ylabels{4} = 'Fourth y-label';
%           [ax,hlines] = ploty4(x,y1,x,y2,x,y3,x,y4,ylabels);
%           leghandle = legend(hlines, 'y = x','y = x^2','y = x^3','y = x^4',2);
%
% See also Plot, Plotyy

% Based on plotyyy.m (available at www.matlabcentral.com) by :
% Denis Gilbert, Ph.D.


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

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

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

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

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

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

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

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

%Put ax(2) on top;
axes(ax(2));

%Set y-labels;
if nargin==9
set(cell2mat(get(ax,{'ylabel'})),{'String'},{ylabels{:}}');
end




主程序:
         x = 0:10;
            y1=x;  y2=x.^2;  y3=x.^3;  y4=x.^4;
            ylabels{1} = 'First y-label';
            ylabels{2} = 'Second y-label';
            ylabels{3} = 'Third y-label';
            ylabels{4} = 'Fourth y-label';
            [ax,hlines] = ploty4(x,y1,x,y2,x,y3,x,y4,ylabels);
            leghandle = legend(hlines, 'y = x','y = x^2','y = x^3','y = x^4',2);


untitled.jpg

评分

参与人数 1金钱 +10 贡献 +2 收起 理由
Aires + 10 + 2

查看全部评分

密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2013-11-18 10:45:52 | 显示全部楼层
不错啊,以后用到可以参考了~
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2013-11-18 13:13:04 | 显示全部楼层
Matlab出图真心不好看
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2013-11-18 15:11:44 | 显示全部楼层
刚才试了一下,画出来了,挺好的。
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2013-11-18 15:19:43 | 显示全部楼层
不错,不过开始发现origin的双y轴也不错
密码修改失败请联系微信:mofangbao

新浪微博达人勋

 楼主| 发表于 2013-11-18 18:51:34 | 显示全部楼层
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2013-11-18 20:21:39 | 显示全部楼层
学习了!
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2013-11-19 11:16:35 | 显示全部楼层
这种折线图,matlab真心不是专长如此多的代码....
origin做这样的图还是方便些。
不过啦,多一种方法多一种思路啦。
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2013-11-20 14:27:57 | 显示全部楼层
非常感谢楼主,强大的Matlab,但总感觉Matlab做图的线条不够圆滑
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2015-8-19 08:13:36 | 显示全部楼层
get{:eb502:}
密码修改失败请联系微信:mofangbao
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

Copyright ©2011-2014 bbs.06climate.com All Rights Reserved.  Powered by Discuz! (京ICP-10201084)

本站信息均由会员发表,不代表气象家园立场,禁止在本站发表与国家法律相抵触言论

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