翻译自 MATLAB 官方文档
- 创建二维图形并自定义线条
- LineSpec:自定义颜色、线型和标记
- 指定坐标轴范围
- 更改坐标轴范围
- 使用半自动坐标轴范围
- 还原为默认范围
- 反转坐标轴方向
- 显示通过原点的轴线
- 删除坐标轮廓区
- 向图形添加标题、坐标轴标签和图例(Title, axis & legend)
- 创建包含双 y 轴的图(double Y axis)
- 合并多个绘图
- 使用hold on 在同一坐标系下合并
- 使用 subplot 函数在同一个window 上绘制不同的坐标区
- 添加超级标题
创建二维图形并自定义线条
x = linspace(,*pi,);
y = sin(x);
figure
plot(x,y)
figure % new figure window
stairs(x,y) % 阶梯图
y1 = sin(x);
y2 = sin(x-pi/);
figure
plot(x,y1,x,y2) % 同时绘制多个线条
figure
plot(x,y1,'--g',x,y2,':r') % 给定 LineSpec
使用 figure 命令打开一个新图窗窗口。如果未打开新图窗窗口,则默认情况下,MATLAB 会清除现有图形并在当前图窗中绘制。
LineSpec:自定义颜色、线型和标记
三者组合成为不同样式的线条
-
是带有圆圈标记的红色虚线'--or'
更多细节,参看 plot – MATLAB.
指定坐标轴范围
可以设置 x 轴、y 轴和 z 轴范围,以控制数据在坐标区上的显示位置。您也可以更改 x 轴线和 y 轴线的显示位置(仅适用于二维绘图),或反转值沿每条轴递增的方向。
更改坐标轴范围
使用
xlim
和
ylim
函数指定坐标轴范围。对于三维绘图,加上
zlim
函数。将
[min max]
形式的二元素向量传递给函数。
x = linspace(-,,);
y = sin(*x)./exp(x);
plot(x,y)
xlim([ ])
ylim([- ])
使用半自动坐标轴范围
将 x 轴范围最大值设为 0,y 轴范围最小值设为 -1。其他范围则由 MATLAB 选择。对于自动计算的范围最小值或最大值,分别使用 -inf 或 inf。
[X,Y,Z] = peaks;
surf(X,Y,Z)
xlabel('x-axis')
ylabel('y-axis')
xlim([-inf ])
ylim([- inf])
还原为默认范围
xlim auto
ylim auto
zlim auto
反转坐标轴方向
通过设置 Axes 对象的 XDir 和 YDir 属性,可控制 x 轴和 y 轴值递增的方向。这些属性可以设置为
'reverse'
或
'normal'
(默认值)。使用 gca 命令可访问 Axes 对象。
stem(:)
ax = gca;
ax.XDir = 'reverse';
ax.YDir = 'reverse';
显示通过原点的轴线
默认情况下,x 轴和 y 轴沿坐标区的外边界显示。通过设置 Axes 对象的 XAxisLocation 和 YAxisLocation 属性来更改轴线位置,以使轴线在原点 (0,0) 处交叉。将 XAxisLocation 设置为 ‘top’、’bottom’ 或 ‘origin’。将 YAxisLocation 设置为 ‘left’、’right’ 或 ‘origin’。这些属性仅适用于二维视图中的坐标区。
x = linspace(-,);
y = sin(x);
plot(x,y)
ax = gca;
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';
删除坐标轮廓区
向图形添加标题、坐标轴标签和图例(Title, axis & legend)
title('Graph of Sine and Cosine Between -2\pi and 2\pi') % 添加标题
xlabel('-2\pi < x < 2\pi') % x-axis label
ylabel('sine and cosine values') % y-axis label
legend('y = sin(x)','y = cos(x)') % 图例
legend('y = sin(x)','y = cos(x)','Location','southwest')
legend('y = sin(x)','y = cos(x)','Location','southwestoutside')
文本中用到的希腊字母,用 LATEX L A T E X 书写。
创建包含双 y 轴的图(double Y axis)
x = linspace(,);
y = sin(x/);
yyaxis left
plot(x,y);
r = x.^/; // elementwise square
yyaxis right
plot(x,r);
yyaxis left
title('Plots with Different y-Scales')
xlabel('Values from 0 to 25')
ylabel('Left Side')
yyaxis right
ylabel('Right Side')
hold on
yyaxis left
y2 = sin(x/);
plot(x,y2);
y3 = sin(x/);
plot(x,y3);
yyaxis right
load count.dat;
m = mean(count,);
e = std(count,,);
errorbar(m,e)
hold off
使用
hold on
命令,会在当前figure继续绘制图形。新绘图不会重置标题或轴标签等坐标区属性,但是,坐标区范围和刻度值会进行调整以适应新数据。
对于双Y轴而言,新图采用和对应 y 轴一样的颜色代表同一族曲线,但循环使用线型序列,以示区分。
hold on
同时影响左右两侧。
对于单Y轴而言,新绘图将根据坐标区的 ColorOrder 和 LineStyleOrder 属性循环使用颜色和线型。
之后,下一个新绘图将清除现有绘图,并重置标题等坐标区属性。
hold off
合并多个绘图
使用hold on 在同一坐标系下合并
x = linspace(,,);
y1 = sin(x);
figure
plot(x,y1)
title('Combine Plots')
hold on
y2 = sin(x/);
plot(x,y2)
y3 = *sin(x);
scatter(x,y3)
hold off
使用 subplot 函数在同一个window 上绘制不同的坐标区
figure
ax1 = subplot(,,);
x = linspace(,,);
y1 = sin(*x);
plot(ax1,x,y1)
title(ax1,'Subplot 1')
ylabel(ax1,'Values from -1 to 1')
ax2 = subplot(,,);
y2 = rand(,);
scatter(ax2,x,y2)
title(ax2,'Subplot 2')
ylabel(ax2,'Values from 0 to 1')
添加超级标题
f = figure;
p = uipanel('Parent',f,'BorderType','none');
p.Title = 'My Super Title';
p.TitlePosition = 'centertop';
p.FontSize = ;
p.FontWeight = 'bold';
subplot(,,,'Parent',p)
x = linspace(,,);
y1 = sin(*x);
plot(x,y1)
title('Subplot 1')
subplot(,,,'Parent',p)
y2 = rand(,);
scatter(x,y2)
title('Subplot 2')