您的位置:首页 > 编程语言 > MATLAB

用Matlab画直方图

2016-03-25 19:38 453 查看

简介

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

关键

使用Matlab的
bar()
函数创建柱状图

bar()
画的bin的高度跟数据相关

bar()
数据每一列一个group,有几列数据就画出几组柱状图

坐标轴显示的刻度叫 XTick 或 YTick

注意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]);


效果

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: