Matlab-绘图学习(Matlab-R2017b))

绘图类型

散点图,曲线图,条形图,饼状图,三维图等

绘图方式

读取数据–> 命令绘图–>修改,标注–>导出高质量图片

单图

点线图

plot_simple.m

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
% 关闭已有图,清理参数,清楚命令行
clf
clear
clc
% 获取数据
x=0:0.2:2*pi;
y=sin(x);
% 绘制点线图,设置点线样式
plot(x,y,'bo-','LineWidth',1.25,'MarkerFaceColor','r','MarkerSize',8)
% 坐标轴以及标题
xlabel('Xaxes')
ylabel('Yaxes')
title('Simple-Point-Line-Figure')
%背景颜色以及整体的字体样式,大小属性
set(gcf,'color','w') %background color
set(gca,'Fontname','times new Roman','FontSize',12,'FontWeight','bold') %set font name,

条形图

bar.m

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
x=categorical({'A','B','C','D','E','F'});
y=[0.21, 0.25, 0.26, 0.36, 0.5, 0.6];
err=[0.01, 0.02, 0.01, 0.03, 0.02, 0.015];
b = bar(x,y);
ylim([0 0.65])
b.FaceColor = 'flat';
b.CData(1,:) = [.5 0 .5];
b.CData(2,:) = [0 0 .5];
b.CData(3,:) = [.5 0 0];
b.CData(4,:) = [0 0 0];
b.CData(5,:) = [.5 .5 .5];
b.CData(6,:) = [.5 .5 0];
xlabel('Group')
ylabel('Value')
title('Colormap-bar')
hold on
b = errorbar(x,y,err,'b','Linestyle','none','LineWidth',2);
set(gcf,'color','w') %background color
set(gca,'Fontname','times new Roman','FontSize',12,'FontWeight','bold') %set font name,

直方图

histogram.m

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
clf % 清除图形
clear
clc
load rmsd.txt % 读取数据文件
data=rmsd(:,2); % 读取数据第二列
%quizzes=transpose(data)
h=histogram(data); % 统计数据
h.FaceColor = 'r';
% xy轴及标题
xlabel('X')
ylabel('Y')
title('Histogram')

set(gcf,'color','w') %background color
set(gca,'Fontname','times new Roman','FontSize',12,'FontWeight','bold') %set font name, size and bold

多组条形带误差棒图

方法一
energy.m

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
% 读取数据
load energy.dat
LP1=energy(:,1);
error1=energy(:,2);
LP2=energy(:,3);
error2=energy(:,4);
LP3=energy(:,5);
error3=energy(:,6);
% x=[1 2 3 4 5 ];
% x轴标签
x=[LP1,LP2,LP3]
e=[error1,error2,error3]
leg={'LP1','LP2','LP3'}
gpname = {'Van der waal','Electrostatic','Polar solvation','SASA','Binding energy'}
handles = barweb(x, e, 1, gpname, 'Energy', 'Group','Value/(kJ/mol)', jet, 'none', leg, 2, 'plot')

set(gcf,'color','w') %background color
set(gca,'Fontname','times new Roman','FontSize',12,'FontWeight','bold') %set font name, size and bold

这里用到了barweb.m函数文件
更改其152-154行处为

1
2
3
% x =get(get(handles.bars(i),'children'), 'xdata');
% x = mean(x([1 3],:));
x = handles.bars(i).XData + handles.bars(i).XOffset;

energy.dat

1
2
3
4
5
54.798	0.422	35.912	0.575	22.862	1.075
20.836 0.443 16.184 1.075 30.519 1.086
48.431 0.578 46.565 1.424 49.984 2.504
8.206 0.04 6.334 0.07 4.854 0.171
35.402 0.47 11.809 0.745 8.247 1.417

问题:无法绘制y轴为负值。

方法二
energy2.m

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
clear
clc
% 绘制条形图的数据
model_series = [4, 7; -5, -6; 3, 5; -1, -6];
% 绘制条形图的误差
model_error = [1, 0.5; 0.5, 0.8; 1, 0.5; 0.6, 0.8];
% Creating axes and the bar graph
ax = axes;
h = bar(model_series,'BarWidth',1);

% Set color for each bar face
% h(1).FaceColor = 'blue';
% h(2).FaceColor = 'yellow';
% Properties of the bar graph as required
ax.YGrid = 'on';
ax.GridLineStyle = '-';
xticks(ax,[1 2 3 4]);
% Naming each of the bar groups
xticklabels(ax,{ 'A', 'B', 'C', 'D'});
% X , Y, and title labels
xlabel ('X');
ylabel ('Y');
title ('Title')
% ylim([-10 10])

% Creating a legend and placing it outside the bar plot
lg = legend('group1','group2','AutoUpdate','off');
lg.Location = 'Best';
lg.Orientation = 'Horizontal';
hold on;

% Finding the number of groups and the number of bars in each group
ngroups = size(model_series, 1);
nbars = size(model_series, 2);
% Calculating the width for each bar group
groupwidth = min(0.8, nbars/(nbars + 1.5));

% 设置每个误差的位置
% Based on barweb.m by Bolu Ajiboye from MATLAB File Exchange
for i = 1:nbars
% Calculate center of each bar
x = (1:ngroups) - groupwidth/2 + (2*i-1) * groupwidth / (2*nbars);
errorbar(x, model_series(:,i), model_error(:,i), 'g', 'linestyle', 'none', 'LineWidth', 2);
end

set(gca,'Fontname','times new Roman','FontSize',12,'FontWeight','bold') %set font name, size and bold

数据点二次拟合

fit-curve.m

1
2
3
4
5
6
7
%读取数据
x=1:1:10;
y=[1 4 9 14 24 36 50 64 81 102];
%拟合曲线
curve=fit(x.',y.','poly2')
%绘制点和拟合曲线
plot(curve,'-r',x,y,'b*')

自定义函数拟合

模板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
clear;
clc;
% 读取数据
load('hbac.txt')
x = hbac(:,1);
y = hbac(:,2);
%自定义拟合函数
f=fittype('A*exp(-(x/b))','independent','x','coefficients',{'A','b'})
cfun=fit(x, y, f)
%产生数据
xi=0:0.1:800;
yi=cfun(xi);
%绘图
plot(x, y, 'r*', xi, yi, 'b-');
legend box off
% ylim([-0.01 1.0])
xlabel('t')
ylabel('C(t)')
% 设置字体,背景
set(gcf,'color','w') %background color
set(gca,'Fontname','times new Roman','FontSize',12,'FontWeight','bold')

饼状图

pie.m

1
2
3
4
5
6
clf
load test2.dat
data=test2(:,2);
%pie(data)
pie3([4 8 9 5 7 8],{'A','B','C','D','E','F'})
%pie3(data)

散点热图

R.m

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
clear;
clc;
%set font name, size and bold
set(gcf,'color','w') %background color
set(gca,'Fontname','times new Roman','FontSize',12,'FontWeight','bold')
%读取数据
[x,y,z] = textread('xpm~xyz.dat','%f%f%f','headerlines',1);
%绘图
figure
scatter(x,y,10,z,'filled')
%颜色
colormap ('jet')
colorbar
%坐标轴
xlabel ('Residue Index');
ylabel ('Residue Index');
xlim ([0 129]);
ylim ([0 129]);
%输出png,600dpi
print('-dpng','-r600','a');

三维点图

3d-plot.m

1
2
3
4
5
6
7
8
9
10
x=1:5;
y=[1 4 8 -5 3];
z=2:2:10;
plot3(x,y,z,'bo','MarkerFaceColor','k')
grid on
box on
xlabel('X')
ylabel('Y')
zlabel('Z')
title('3D-plot')

三维sphere,mesh和comet

surf-3d.m

1
2
3
4
5
[x,y,z]=sphere(15);
size(x)
surf(x,y,z)
title('surf of sphere')
colorbar

mesh-3d.m

1
2
3
4
5
6
7
8
9
10
11
[X,Y] = meshgrid(-8:.5:8);
R = sqrt(X.^2 + Y.^2) + eps;
Z = sin(R)./R;
C = del2(Z);

figure
mesh(X,Y,Z,C,'FaceLighting','gouraud','LineWidth',0.3)
xlabel('X')
ylabel('Y')
zlabel('Z')
title('mesh-3d')

comet.m

1
2
t =0:0.001:12*pi;
comet3(cos(t),sin(t),t)

3d.m

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
clear;
clc;
%读取数据
[x,y,z] = textread('free-energy-landscape.txt','%f%f%f');
%拟合曲面
[X,Y,Z] = griddata(x,y,z,linspace(min(x),max(x))',linspace(min(y),max(y)),'v4');
%绘图
figure
surf(X,Y,Z)
%插值
shading interp
%颜色
colormap ('jet')
colorbar
%坐标轴
xlabel ('PC1');
ylabel ('PC2');
zlabel ('Energy (kJ/mol)');

多图

双y轴图

yy.m

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
set(gcf,'color','w') %background color
set(gca,'Fontname','times new Roman','FontSize',12,'FontWeight','bold') %set font name, size and bold
%读取数据
x=1:0.25:10;
y1=sin(x);

yyaxis left %left yaxis
plot(x,y1,'p--','LineWidth',2)
hold on
y11=0.1*x;
plot(x,y11,'-.','LineWidth',2)
%xy轴及标签
title('Double-Yaxis')
xlabel('X-axis')
ylabel('Y-left')

y2=(2*x-1).*(x.^2);
yyaxis right %right yaxis
plot(x,y2,'^:','LineWidth',2)
hold on
y22=x.^3;
plot(x,y22,'o-','LineWidth',2)
ylabel('Y-right')
grid on
legend('y=sin(x)','y=0.1*x','y=(2x-1)*(x^2)','y=x^3','Location','northeastoutside')
%axis auto

不同图组合

merge.m

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
clf
load test.dat
load test2.dat
figure(1)
x=test(:,1);
y1=test(:,2);
y2=test2(:,2);

ax1=subplot(1,2,1); %1x2图,子图1
plot(ax1,x,y1,'kp-','LineWidth',1,'MarkerSize',6,'MarkerEdgeColor','r','MarkerFaceColor','green')
xlabel(ax1,'X')
ylabel(ax1,'Y1')
legend(ax1,'data1')
title(ax1,'test1')
%set font
set(gca,'Fontname','times new Roman');


ax2=subplot(1,2,2); %子图2
plot(ax2,x,y2,'g^-.','LineWidth',2,'MarkerSize',7,'MarkerEdgeColor','b','MarkerFaceColor','black')
xlabel(ax2,'X')
ylabel(ax2,'Y2')
legend(ax2,'data2')
title(ax2,'test2')
%set font
set(gca,'Fontname','times new Roman');

局部放大组合图

1.先画整体图,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
load rdf_Cl_Hw.xvg
x = rdf_Cl_Hw(:,1);
y = rdf_Cl_Hw(:,2);

plot(x, y, '-r', 'LineWidth', 1)
legend('Cl-Hw')
legend boxoff
xlabel('r (nm)')
ylabel('g(r)')
title('RDF')
% axis equal
axis([0 2 0 2])
set(gcf,'color','w') %background color
set(gca,'Fontname','times new Roman','FontSize',16,'FontWeight','bold') %set font name, size and bold

2.在通过工具栏中的局部放大和复制命令进行整合,最终效果见下图: