爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
查看: 3774|回复: 5

[讨论] bar图高级调整

[复制链接]

新浪微博达人勋

发表于 2018-3-8 17:51:14 | 显示全部楼层 |阅读模式

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

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

x
% Created by Han yuyang
% version 1.0
% March 8,2018
% Description:
%     Plot precipitation
%     liner regress to show the tendency(not very useful)
% note:
%     If you have any question about this code,
%     please do not hesitate to contact me via E-mail:
%     fadedflowers#foxmail.com
function han_plot(years,prec)
figure;
    handle=bar(years,prec);
    set(gca, 'xlim', [min(years)-1, max(years)+1],'ylim',[600,1800]);
    xlabel('{\itYear/a}', 'fontname', 'Times New Roman', 'fontsize', 20);
    ylabel('{\itAnnual Precipitation/mm}', 'fontname', 'Times New Roman', 'fontsize', 20);
    set(gcf,'outerposition',get(0,'screensize'));

    set(handle,'facecolor',[0,255,255]./255);
    %change the color of the bar to Cyan
    hold on

X=[ones(size(years)), years];
[b, ~, ~, ~, stats]=regress(prec, X);
    b1=sprintf('%.3f', b(1));
    b2=sprintf('%.3f', b(2));
    str1=['{\ity}=', b2, '{\itx}+', b1];
    pval=stats(3);
    pval=sprintf('%.3f', pval);
    str2=['{\itp}=', pval];
    instr={str1, str2};
    instratts.font='Arial';
    instratts.xscale=0.05;
    instratts.yscale=0.95;
    instratts.fontsize=0.015;
    instratts.interval=0.05;
   
% see note in PlotaddText
    PlotaddText(gca, instr, instratts);
    yy=b(1)+b(2)*xlim;
    linehand=plot(xlim, yy, 'k-');
    set(linehand, 'linewidth', 2);
    hold off;
end

% instr is a cell contain the text
    PlotAddText(gca, instr, instratts);
    yy=b(1)+b(2)*xlim;
    linehand=plot(xlim, yy, 'k-');
    set(linehand, 'linewidth', 2);
    hold off;
end
untitled.jpg

pro1.rar

760.2 KB, 下载次数: 4, 下载积分: 金钱 -5

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

新浪微博达人勋

 楼主| 发表于 2018-3-8 17:54:03 | 显示全部楼层
% Created by Han yuyang
% version 1.0
% March 8,2018
% Description:
%     instr- a cell of text,length is number of line
%     instratts- the attributes of instr,say:position,font
% Note:
%     If you have any question about this code,
%     please do not hesitate to contact me via E-mail:
%     fadedflowers#foxmail.com
% Example:
%     handle=gca;
%     instr={str1, str2};
%     instratts.font='Arial';
%     instratts.xscale=0.05;
%     instratts.yscale=0.95;
%     instratts.fontsize=0.015;
%     instratts.interval=0.05;
function PlotaddText(handle, instr, instratts)
    % Get xlim ylim
    xlim=get(handle, 'xlim');
    ylim=get(handle, 'ylim');
   
    fontsize=instratts.fontsize;
    if fontsize<1
        fontsize=(ylim(2)-ylim(1))*fontsize;
    end
   
    % Loop
    for ii=1:length(instr)
        strVal=instr{ii};
        x=(xlim(2)-xlim(1))*instratts.xscale+xlim(1);
        y=(ylim(2)-ylim(1))*instratts.yscale+ylim(1);
        y=y-(ylim(2)-ylim(1))*instratts.interval*(ii-1);
        text(x, y, strVal, 'fontname', instratts.font, ...
            'fontsize', fontsize);
    end
end
密码修改失败请联系微信:mofangbao

新浪微博达人勋

 楼主| 发表于 2018-3-8 17:54:50 | 显示全部楼层
原代码来自:http://blog.sciencenet.cn/blog-1148346-794768.html
进行了调整和添加注释,使得可读性更好
密码修改失败请联系微信:mofangbao

新浪微博达人勋

 楼主| 发表于 2018-3-8 17:57:02 | 显示全部楼层
https://baike.baidu.com/item/%E5%8D%81%E5%85%AD%E8%BF%9B%E5%88%B6%E9%A2%9C%E8%89%B2%E7%A0%81/10894232?fr=aladdin
十六进制颜色码
TIM图片20180308175639.png
密码修改失败请联系微信:mofangbao

新浪微博达人勋

 楼主| 发表于 2018-3-8 17:57:33 | 显示全部楼层

    set(handle,'facecolor',[0,255,255]./255);
    %change the color of the bar to Cyan
    hold on

对应该行代码
密码修改失败请联系微信:mofangbao

新浪微博达人勋

 楼主| 发表于 2018-3-8 18:11:00 | 显示全部楼层
关于注释:我从前不能合理利用tab/shift+tab使得注释很麻烦
matlab 请学会使用ctr+r和ctr+t
都是光标全选然后使用
密码修改失败请联系微信:mofangbao
您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

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

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

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