找回密码
 注册
关于网站域名变更的通知
查看: 270|回复: 1
打印 上一主题 下一主题

MATLAB 用“function handles”&nbsp

[复制链接]
  • TA的每日心情

    2019-11-20 15:22
  • 签到天数: 2 天

    [LV.1]初来乍到

    跳转到指定楼层
    1#
    发表于 2020-4-20 09:45 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式

    EDA365欢迎您登录!

    您需要 登录 才可以下载或查看,没有帐号?注册

    x
    Code Sample: Stacked Bars and Lines in Matlab
    I needed to make a plot that superimposes a “stacked” bar graph with a line in Matlab.  I got started by referencing this post on Matlab central, which used “function handles” to pass plotting commands into plotyy.  This got me thinking about some other Matlab features that are explained in the following code sample.  Hope this helps in your work too!
    The data is from the USGS’s estimate of U.S. Water Use for 2005.

    " ~( j& v2 u$ B$ o: B* o4 T+ N& {2 Z2 h' i  h7 ]
    ==================================================================
    %How to plot data from USGS, Estimated Water Use in the U.S. in 2005
    %as a stacked bar/line graph in Matlab.
    %Specifically, we want the US population as a line, and the different
    %water use types as stacked bars.
    %The data, where each data value is for a different 5 year period
    population = [151, 164, 180, 194, 206, 216, 230, 242, 252, 267, 285, 301];
    totalwithdrawal = [180, 240, 270, 310, 370, 420, 430, 397, 404, 399, 413, 410];
    public = [14, 17, 21, 24, 27, 29, 33, 36.4, 38.8, 40.2, 43.2, 44.2];
    irrigation = [89, 110, 110, 120, 130, 140, 150, 135, 134, 130, 139, 128];
    thermo = [40, 72, 100, 130, 170, 200, 210, 187, 194, 190, 195, 201];
    %Place the data in a matrix, where each row is a
    %different data type (i.e., population)
    data = [thermo; irrigation; public;
    totalwithdrawal-(public+irrigation+thermo)];
    %Now, flip the data so each data type has its own column
    data = data';
    %To use different data types in the plotyy environment, you
    �n use Matlab's 'anonymous function' feature. Stackedbar
    %and prettyline below are temporary functions you can only
    %use inside of this script.
    stackedbar = @(x, A) bar(x, A, 'stack');
    prettyline = @(x, y) plot(x, y, 'k', 'LineWidth', 5);
    %There's a version of plotyy that accepts function handles as an
    %argument. We'll pass stackedbar and prettyline in there as the
    %last arguments to this function.
    [ax, h1, h2] = plotyy(1950:5:2005, data, 1950:5:2005, population, stackedbar, prettyline);
    %You'll notice the line and bar axes are not agreeing with each other.
    %We can fix this using the ax variable created above.
    set(ax(1), 'XLim', [1945 2010]);
    set(ax(2), 'XLim', [1945 2010]);
    %Get rid of one of the sets of ticks, to make sure
    %the figure looks nice.
    set(ax(2), 'XTick', []);
    %Changing the colormap will change the colors of the
    %stacked bars. For example:
    colormap summer;
    %Finally write the legend. Note that, for whatever reason,
    %the line is first in the list of legend entries.

    legend('Population', 'Thermoelectric Power', 'Irrigation', 'Public Supply', 'Other', 'Location', 'NorthWest');

    " ]$ K2 ~; i3 z% n- H, c/ C5 J! X
    9 {$ V' [7 l: Z" {0 j- ^6 M* ]

    该用户从未签到

    2#
    发表于 2020-4-20 13:40 | 只看该作者
    MATLAB 用“function handles”&nbsp
    您需要登录后才可以回帖 登录 | 注册

    本版积分规则

    关闭

    推荐内容上一条 /1 下一条

    EDA365公众号

    关于我们|手机版|EDA365电子论坛网 ( 粤ICP备18020198号-1 )

    GMT+8, 2025-7-23 04:58 , Processed in 0.125000 second(s), 26 queries , Gzip On.

    深圳市墨知创新科技有限公司

    地址:深圳市南山区科技生态园2栋A座805 电话:19926409050

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