Matlab绘图实例
- 格式:pdf
- 大小:65.23 KB
- 文档页数:3
Matlab绘图实例
概要
每次⽤ Matlab 绘图都要搜⼀堆资料设置⼀些参数,本次将绘图中的⼀些参数设置实例展⽰在这⾥,以备不时之需。暂包括折线
图,⾯积图。
折线图实例
下图是效果图:
图 1:折线图效果图
Matlab 代码如下:
clc;clear;
y1 = 0; y2 = 0;
len = 249;
for i = 2:len
y1(i) = y1(i-1) + unifrnd(-0.8,1);
end
for i = 2:len
y2(i) = y2(i-1) + unifrnd(-0.9,1);
end
fig = figure();
ax = axes;
% fig.Visible = 'off'; % 设置图⽚不可见,即只保存图⽚到本地
plot( y1, 'linewidth', 1.75, 'Color', 'red')
hold on;
plot(y2, 'linewidth', 1.75, 'Color', [0.27451, 0.5098, 0.70588])
ax.YGrid = 'on'; % 把 gca 当成结构体。R2014b 版本之前⽤ set(gca, 'YGrid', 'on'),下同
ax.XColor = 'k'; %set(gca, 'XColor', 'k')
fig.Position = [10 10 630 300]; %set(gcf,'Position',[left,top,width,height]
box off; %去掉坐标的边框
ylabel('random value');
xlabel('random date');
xtickangle(0); % x 轴标签旋转
h = legend('随机曲线 y_1', '随机曲线 y_2', 'Location', 'northwest', 'Orientation', 'horizontal');
h.Box = 'off'; %set(h, 'Box', 'off'); % 去掉图例边框
title('图 1. 随机曲线⽰例图', 'FontSize', 10.5, 'FontWeight', 'normal', 'FontName', 'heiti');
saveas(gcf, 'Fig1.png'); % 保存图⽚到本地,也可以⾃定义路径,路径名+图⽚名即可
⾯积图实例
下图是效果图:
图 2:⾯积图效果图
Matlab 代码如下:
clc;clear;
x = 0:0.1:6*pi;y1 = sin(x); y2 = y1;
y1(y1<0) = 0;
y2(y2>0) = 0;
fig = figure(); % 不可见的话,加参数 'Visible', 'off' 或者设定 fig.Visible = 'off'
ax = axes;
h = area([y1',y2'], 'linewidth', 0.05); % 按列绘图的
h(1).FaceColor = [0.2,0.8,0.2];%[0.27451, 0.5098, 0.70588]; % 指定第⼀列填充颜⾊
h(2).FaceColor = [1, 0.55, 0]; % 指定第⼆列填充颜⾊
h(1).EdgeAlpha = 0; % 将边线设置为透明,0 到 1 之间
h(2).EdgeAlpha = 0; % 将边线设置为透明,0 到 1 之间
ax.YGrid = 'on'; %set(gca, 'YGrid', 'on');
ax.YLim = [-1.2, 1.5]; % set(gca, 'YLim', [-1.2, 1.5]); % 设置 Y 轴显⽰范围
fig.Position = [10 10 630 300]; %set(gcf,'Position',[10 10 630 300]); %[left,top,width,height]
box off; %去掉坐标的边框
ylabel('sin value');
xlabel('x value');
h = legend('余弦曲线(正)', '余弦曲线(负)', 'Location', 'northwest', 'Orientation', 'horizontal');
h.Box = 'off'; %set(h, 'Box', 'off'); % 去掉图例边框
title('图 2. ⾯积图⽰例', 'FontSize', 10.5, 'FontWeight', 'normal', 'FontName', 'heiti');
saveas(gcf, 'Fig2.png'); % 保存图⽚到本地,也可以⾃定义路径,路径名+图⽚名即可
⽇期作 X 轴标签实例
下图是效果图:
图 3:⽇期轴效果图
clc;clear;
y1 = 0; y2 = 0;
len = 205;
for i = 2:len
y1(i) = y1(i-1) + unifrnd(-0.8,1);
end
x_time = today:today+len-1; % datenum 格式
fig = figure('color', [1 1 1]); % 同时设置背景⾊为⽩⾊
fig.Position = [10 10 630 270]; %[left,top,width,height]
ax = axes;
%fig.Visible = 'off'; % 从 R2014b 开始,您可以使⽤圆点表⽰法查询和设置属性
plot(x_time, y1', 'linewidth', 1.75, 'Color', 'red')
%xlim([x_time(1), x_time(end)]);
xDate = linspace(x_time(1), x_time(end), 10); % 指定显⽰个数
ax.XTick = xDate;
datetick('x','yyyymmdd','keepticks');
%xlim([x_time(1), x_time(end)]);
% 这⼀句消除了 ytick -
%ax.YAxis.TickLength = [0,0];
%ax.YAxis.Color = [0.5 0.5 0.5];%'gray';
%ax.XAxis.Color = [0.5 0.5 0.5];
ax.YGrid = 'on';ax.XColor = 'k';
box off; %去掉坐标的边框
ylabel('random value');
xlabel('random date');
xtickangle(30); % x 轴标签旋转
h = legend('随机曲线 y_1', 'Location', 'northwest', 'Orientation', 'horizontal');
h.Box = 'off'; % 去掉图例边框
title('图 1. 随机曲线⽰例图', 'FontSize', 10.5, 'FontWeight', 'normal', 'FontName', 'heiti','Color', [0.5 0.5 0.5]);
%saveas(gcf, 'Fig1.png'); % 保存图⽚到本地