IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    [原]用Matlab画直方图

    bendanban发表于 2016-03-25 19:38:54
    love 0

    简介

    本文介绍如何使用matlab定制自己的直方图。


    关键

    1. 使用Matlab的 bar() 函数创建柱状图
    2. bar() 画的bin的高度跟数据相关
    3. bar() 数据每一列一个group,有几列数据就画出几组柱状图
    4. 坐标轴显示的刻度叫 XTick 或 YTick
    5. 注意gca和gcf的区别,可以doc他们分别查看相关说明

    示例代码

    close all;
    clc
    clear;
    
    % bins data 
    num_bins = 5;
    num_methods = 3;
    data = rand(num_bins, num_methods);
    
    % draw the graph
    figure;
    handle_bar = bar(data);
    ax = gca;
    
    % tight the graph
    axis tight;
    
    % title and labels
    title('\bf\color[rgb]{0,0,0}histgram');
    xlabel('$\mathbf{\Gamma(x)}$ \textbf{value}', 'Interpreter', 'latex');
    %xlabel('\bf\color[rgb]{0,0,0}\Gamma(x)', 'Interpreter', 'tex');
    ylabel('\bf\color[rgb]{0,0,0}percentage');
    
    % ytick : add % and use Bold Font with the TeX Interpreter
    ytick = ax.YTick;
    ytick_label = cell(length(ytick), 1);
    for i = 1:length(ytick)
      ytick_label(i) = {['\bf', num2str(ytick(i)*100), '%']};
    end
    ax.YTickLabel = ytick_label;
    
    % Grid 
    ax.YGrid = 'on';
    
    % legend
    leg_handle = legend('\bf{}method 1', '\bf{}method 2', '\bf{}method 2', 'Location', 'northeast');
    
    % figure position and size : [left bottom width height]
    set(gcf, 'Position', [100, 500, 400, 260]);

    效果

    实验效果



沪ICP备19023445号-2号
友情链接