set(gca,'XTick',[0 0.1 0.5 1 2]);//在 X 轴加指定坐标
set(gca,'YTick',[0 1 2]); 给 x 轴标记值
set(gca,'linewidth',1);//坐标轴粗细
set(gca,'FontName','Time New Roman','FontSize',5);//坐标轴字体
set(gca,'Color','g');//坐标轴颜色
set(gcf,'color',[0 0 0]);//背景颜色
set(gca,'ydir','reverse');
set(gca,'ydir','normal')
set(gca,'fontsize',15)%改变坐标字体大小
set(gca,'Fontname','TimesewRoman','FontSize',14,'FontWeight','bold',
'FontAngle','italic')
%设置字体为 Times New Roman,字号为 14 号,加粗,
斜体
set(gca,'XMinorTick','on') %设置 x 轴最小刻度
set(gca,'YMinorTick','on') %设置 y 轴最小刻度
set(gca,'ticklength',[0.025 0.0125]);
%设置小刻度长度
set(gca,'tickdir','out')
%将小刻度放在坐标轴外面
set(gca,'position',[0.15,0.15,0.80,0.80]) %设置图形占画布的比例
set(gcf,'position',[0,0,1200,800]) %设置画布的大小 %
set(gcf,'FontName','Time New Roman','FontSize',25)
set(gca,'xcolor',[1,0,0],'ycolor',[0,1,0])%设置 x、y 轴的颜色
set(gca,'FontSize',50)%设置坐标字体的大小
图形是呈现数据的一种直观方式,在用 Matlab 进行数据处理和计算后,我们一
般都会以图形的形式将结果呈现出来。尤其在论文的撰写中,优雅的图形无疑会
为文章加分。本篇文章非完全原创,我的工作就是把见到的 Matlab 绘图代码收
集起来重新跑一遍,修改局部错误,然后将所有的图贴上来供大家参考。大家可
以先看图,有看中的可以直接把代码 Copy 过去改成自己想要的。
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
%% 直方图图的绘制
%直方图有两种图型:垂直直方图和水平直方图。而每种图型又有两种表现模
式:累计式:分组式。
figure;
z=[3,5,2,4,1;3,4,5,2,1;5,4,3,2,5]; % 各因素的相对贡献份额
colormap(cool);% 控制图的用色
subplot(2,3,1);
bar(z);%二维分组式直方图,默认的为'group'
title('2D default');
subplot(2,3,2);
bar3(z);%三维的分组式直方图
title('3D default');
subplot(2,3,3);
barh(z,1);%分组式水平直方图,宽度设置为 1
title('vert width=1');
subplot(2,3,4);
bar(z,'stack');%累计式直方图,例如:1,1+2,1+2+3 构成了第一个 bar
title('stack')
subplot(2,3,5);
bar3h(z,0.5,'stacked');%三维累计式水平直方图
title('vert width=1 stack');
subplot(2,3,6);
bar3(z,0.8,'grouped');%对相关数据的颜色进行分组,默认的位'group'
title('width=0.8 grouped');
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
%% =========柱状图的进阶==========
figure;
y=[300 311;390 425; 312 321; 250 185; 550 535; 420 432; 410 520;];
subplot(1,3,1);
b=bar(y);
grid on;
set(gca,'XTickLabel',{'0','1','2','3','4','5','6'})
legend('算法 1','算法 2');
xlabel('x axis');
ylabel('y axis');
%使仅有的一组柱状图呈现不同颜色,默认的位相同颜色
data = [1.0, 1.0, 0.565, 0.508, 0.481, 0.745];
subplot(1,3,2);
b = bar(data);
ch = get(b,'children');
set(ch,'FaceVertexCData',[4;2;3;1;5;6]);%使用 Indexed 形式指定每组
bar 的颜色
set(gca,'XTickLabel',{'C0','C1','C2','C3','C4','C5'})
axis([0 7 0.0 1.0]);
ylabel('micro F-measure');
%使每个 bar 颜色不同,默认的是每个元素在不同组的颜色相同
data = [3, 7, 5, 2;4, 3, 2, 9;6, 6, 1, 4];
23
24
25
26
27
28
subplot(1,3,3);
b = bar(data);
ch = get(b,'children');
set(ch{1},'FaceVertexCData',[1;2;3]);%设置第一个元素在不同组的颜色
set(ch{2},'FaceVertexCData',[1;2;3]);%设置第二个元素在不同组的颜色
set(ch{3},'FaceVertexCData',[1;2;3]);
set(ch{4},'FaceVertexCData',[1;2;3]);
?
1
2
3
4
5
6
7
8
9
1
0
1
1
1
2
1
3
1
4
%% 彩色柱状图
%用到的数据
n = 8;
Z = rand(n,1);
figure;
%默认图片
subplot(1,3,1);
bar(Z);
%简单的作图
% 这个图根据数据列中值的大小着色。每列中的值越大,颜色越突出
subplot(1,3,2);
h=bar(Z);
colormap(summer(n));
ch = get(h,'Children');
fvd = get(ch,'Faces');%针对矩阵时,只能用 fvd=get(ch{col},'Faces'),
下同
fvcd = get(ch,'FaceVertexCData');
[~, izs] = sortrows(Z,1);
for i = 1:n
row = izs(i);
fvcd(fvd(row,:)) = i;
end
set(ch,'FaceVertexCData',fvcd)
%图片会以渐变的方式着色,效果非常不错
subplot(1,3,3);
h=bar(Z);
ch = get(h,'Children');
fvd = get(ch,'Faces');
fvcd = get(ch,'FaceVertexCData');
[zs, izs] = sortrows(Z,1);
k = 128; % 准备生成 128 *3 行的 colormap
colormap(summer(k)); % 这样会产生一个 128 * 3 的矩阵,分别代表[R G B]
的值
% 检视数据
whos ch fvd fvcd zs izs
%
Bytes
Class
Size
Name
Attributes
ch
1x1
66x1
%
%
ble
%
%
%
%
%
shading interp % Needed to graduate colors
for i = 1:n
fvcd
fvd
izs
zs
13x4
13x1
13x1
8 dou
528 double
416 double
104 double
104 double
color = floor(k*i/n); % 这里用取整函数获得 color 在 colormap
中行
row = izs(i); % Look up actual row # in data
fvcd(fvd(row,1)) = 1; % Color base vertices 1st index
fvcd(fvd(row,4)) = 1;
fvcd(fvd(row,2)) = color; % Assign top vertices color
fvcd(fvd(row,3)) = color;
end
set(ch,'FaceVertexCData', fvcd); % Apply the vertex coloring
set(ch,'EdgeColor','k');
1
5
1
6
1
7
1
8
1
9
2
0
2
1
2
2
2
3
2
4
2
5
2
6
2
7
2
8
2
9
3
0
3
1
3
2
3
3
3
4
3
5
3
6
3
7
3
8
3
9
4
0
4
1
4
2
4
3
4
4
4
5
4
6
4
7
4
8
4
9
5
0
5
1
5
2
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
%% 绘制统计直方图
%hist(y):如果 y 是向量,则把其中元素放入 10 个条目中,且返回每条中的
元素的个数;如果 y 为矩阵,则分别对每列进行处理,显示多组条形。
%[n,xout]=hist(y,x):非递减向量 x 的指定 bin 的中心。向量 xout 包含频
率计数与条目的位置。
x=-10:.1:10;
y1=randn(2008,1);
y2=randn(2008,3);
figure;
colormap(winter);
subplot(2,2,1);
hist(y1);%把其中元素放入 10 个条目中
title('y1 为向量,default,n=10');
subplot(2,2,2);
hist(y2);%分别对每列进行处理,显示多组条形
title('y2 为矩阵');
subplot(2,2,3);
hist(y1,x);%用户也可以使用[n,xout]=hist(y1,x);bar(xout,n)绘制条形
直方图
title('向量 x 指定条目');
subplot(2,2,4);
hist(y2,1000);%第二个参数为标量时指定 bin 的数目
title('nbins=1000');
?
1
2
3
4
5
6
7
8
9
%% ========均值方差直方图========
a=[8 9 10 7 8 9];%mean
b=[1 1 1 1 1 1];%std
figure();
h=bar(a);
ch=get(h,'children');
set(ch,'FaceVertexCData',[4;2;3;1;5;6]);%使用 Indexed 形式指定每组
bar 的颜色
hold on;
errorbar(a,b,'k','LineStyle','none');