图形属性 #

一、图形对象层次 #

1.1 对象层次结构 #

text
Root (根对象)
└── Figure (图形窗口)
    ├── Axes (坐标轴)
    │   ├── Line (线)
    │   ├── Surface (曲面)
    │   ├── Text (文本)
    │   ├── Patch (补片)
    │   └── Image (图像)
    └── Uicontrol (控件)

1.2 获取和设置属性 #

matlab
% 创建图形
x = 0:0.1:2*pi;
h = plot(x, sin(x));

% 获取属性
get(h, 'LineWidth');
get(h);  % 获取所有属性

% 设置属性
set(h, 'LineWidth', 2);
set(h, 'Color', 'r', 'LineStyle', '--');

% 使用点号(推荐)
h.LineWidth = 2;
h.Color = 'r';

二、线属性 #

2.1 线型 #

matlab
x = 0:0.1:2*pi;
h = plot(x, sin(x));

% 线型: - 实线, -- 虚线, : 点线, -. 点划线
h.LineStyle = '--';
h.LineStyle = ':';
h.LineStyle = '-.';

2.2 线宽 #

matlab
x = 0:0.1:2*pi;
h = plot(x, sin(x));

h.LineWidth = 1;    % 默认
h.LineWidth = 2;    % 中等
h.LineWidth = 3;    % 粗线

2.3 颜色 #

matlab
x = 0:0.1:2*pi;
h = plot(x, sin(x));

% 预定义颜色
h.Color = 'r';      % 红
h.Color = 'g';      % 绿
h.Color = 'b';      % 蓝
h.Color = 'c';      % 青
h.Color = 'm';      % 品红
h.Color = 'y';      % 黄
h.Color = 'k';      % 黑
h.Color = 'w';      % 白

% RGB值
h.Color = [1 0 0];          % 红
h.Color = [0.5 0.5 0.5];    % 灰色
h.Color = [0 0.5 0.8];      % 自定义颜色

% 十六进制(R2019b+)
h.Color = '#FF0000';        % 红
h.Color = '#1f77b4';        % 自定义

2.4 标记 #

matlab
x = 0:0.5:2*pi;
h = plot(x, sin(x), '-o');

% 标记类型
h.Marker = 'o';     % 圆
h.Marker = 's';     % 方块
h.Marker = 'd';     % 菱形
h.Marker = '^';     % 上三角
h.Marker = 'v';     % 下三角
h.Marker = '<';     % 左三角
h.Marker = '>';     % 右三角
h.Marker = 'p';     % 五角星
h.Marker = 'h';     % 六边形
h.Marker = '+';     % 加号
h.Marker = 'x';     % 叉号
h.Marker = '*';     % 星号

% 标记大小
h.MarkerSize = 8;

% 标记填充颜色
h.MarkerFaceColor = 'r';

% 标记边框颜色
h.MarkerEdgeColor = 'b';

三、坐标轴属性 #

3.1 范围 #

matlab
x = 0:0.1:10;
y = exp(x);
plot(x, y);

ax = gca;  % 获取当前坐标轴

% 设置范围
ax.XLim = [0 5];
ax.YLim = [0 100];
ax.ZLim = [0 10];

% 或使用函数
xlim([0 5]);
ylim([0 100]);
zlim([0 10]);

% 自动范围
xlim('auto');
ylim('auto');

3.2 刻度 #

matlab
x = 0:0.1:10;
plot(x, sin(x));
ax = gca;

% 设置刻度
ax.XTick = 0:2:10;
ax.YTick = -1:0.5:1;

% 设置刻度标签
ax.XTickLabel = {'零', '二', '四', '六', '八', '十'};
ax.YTickLabel = {'-1', '-0.5', '0', '0.5', '1'};

% 刻度方向
ax.TickDir = 'in';     % 向内
ax.TickDir = 'out';    % 向外
ax.TickDir = 'both';   % 双向

3.3 网格 #

matlab
x = 0:0.1:10;
plot(x, sin(x));
ax = gca;

% 显示网格
ax.XGrid = 'on';
ax.YGrid = 'on';
ax.GridLineStyle = '--';
ax.GridColor = [0.5 0.5 0.5];
ax.GridAlpha = 0.5;

% 或使用函数
grid on;
grid minor;
grid off;

3.4 标签 #

matlab
x = 0:0.1:10;
plot(x, sin(x));
ax = gca;

% 设置标签
ax.XLabel.String = 'x轴';
ax.YLabel.String = 'y轴';
ax.Title.String = '正弦函数';

% 或使用函数
xlabel('x轴');
ylabel('y轴');
title('正弦函数');

% 字体属性
ax.XLabel.FontSize = 12;
ax.XLabel.FontWeight = 'bold';
ax.XLabel.Color = 'r';

3.5 字体 #

matlab
x = 0:0.1:10;
plot(x, sin(x));
ax = gca;

% 字体名称
ax.FontName = 'Arial';
ax.FontName = 'SimHei';  % 黑体(中文)

% 字体大小
ax.FontSize = 12;

% 字体粗细
ax.FontWeight = 'normal';
ax.FontWeight = 'bold';

% 字体角度
ax.FontAngle = 'normal';
ax.FontAngle = 'italic';

四、图例属性 #

4.1 位置 #

matlab
x = 0:0.1:2*pi;
plot(x, sin(x), x, cos(x));
lgd = legend('sin(x)', 'cos(x)');

% 预定义位置
lgd.Location = 'north';
lgd.Location = 'south';
lgd.Location = 'east';
lgd.Location = 'west';
lgd.Location = 'northeast';    % 默认
lgd.Location = 'northwest';
lgd.Location = 'southeast';
lgd.Location = 'southwest';
lgd.Location = 'best';         % 自动选择
lgd.Location = 'bestoutside';

% 自定义位置
lgd.Position = [0.2 0.8 0.1 0.1];  % [left bottom width height]

4.2 样式 #

matlab
x = 0:0.1:2*pi;
plot(x, sin(x), x, cos(x));
lgd = legend('sin(x)', 'cos(x)');

% 方向
lgd.Orientation = 'vertical';    % 垂直(默认)
lgd.Orientation = 'horizontal';  % 水平

% 边框
lgd.Box = 'on';
lgd.EdgeColor = 'k';

% 背景
lgd.Color = 'w';
lgd.FaceAlpha = 0.8;

% 字体
lgd.FontSize = 10;
lgd.FontName = 'Arial';

五、图形窗口属性 #

5.1 窗口大小和位置 #

matlab
fig = figure;

% 位置 [left bottom width height]
fig.Position = [100 100 800 600];

% 单位
fig.Units = 'pixels';
fig.Units = 'inches';
fig.Units = 'centimeters';

% 最大化
fig.WindowState = 'maximized';

5.2 颜色 #

matlab
fig = figure;

% 背景颜色
fig.Color = 'w';
fig.Color = [0.9 0.9 0.9];

5.3 标题 #

matlab
fig = figure;
fig.Name = '我的图形';
fig.NumberTitle = 'off';  % 不显示编号

六、导出图形 #

6.1 保存为文件 #

matlab
x = 0:0.1:2*pi;
plot(x, sin(x));

% 保存为PNG
saveas(gcf, 'myplot.png');

% 保存为PDF
saveas(gcf, 'myplot.pdf');

% 保存为EPS
saveas(gcf, 'myplot.eps');

% 使用print函数
print('myplot', '-dpng', '-r300');  % 300 DPI
print('myplot', '-dpdf', '-bestfit');

6.2 设置分辨率 #

matlab
x = 0:0.1:2*pi;
plot(x, sin(x));

% 设置DPI
print('myplot', '-dpng', '-r300');   % 300 DPI
print('myplot', '-dpng', '-r600');   % 600 DPI

6.3 设置大小 #

matlab
fig = figure;
fig.Position = [100 100 800 600];
x = 0:0.1:2*pi;
plot(x, sin(x));

% 按纸张大小导出
fig.PaperPositionMode = 'auto';
print('myplot', '-dpdf', '-fillpage');

七、实用示例 #

7.1 美化图形模板 #

matlab
function create_beautiful_plot(x, y)
    % 创建图形
    fig = figure;
    fig.Color = 'w';
    fig.Position = [100 100 800 500];
    
    % 绘制
    h = plot(x, y, '-o', 'LineWidth', 1.5, 'MarkerSize', 6);
    h.Color = [0 0.4470 0.7410];  % MATLAB默认蓝
    h.MarkerFaceColor = h.Color;
    
    % 坐标轴
    ax = gca;
    ax.FontName = 'Arial';
    ax.FontSize = 12;
    ax.LineWidth = 1;
    ax.Box = 'on';
    ax.XGrid = 'on';
    ax.YGrid = 'on';
    ax.GridLineStyle = '--';
    ax.GridAlpha = 0.3;
    ax.GridColor = [0.5 0.5 0.5];
    
    % 标签
    xlabel('x', 'FontSize', 14, 'FontWeight', 'bold');
    ylabel('y', 'FontSize', 14, 'FontWeight', 'bold');
    title('Beautiful Plot', 'FontSize', 16, 'FontWeight', 'bold');
end

7.2 设置默认属性 #

matlab
% 设置默认线宽
set(0, 'DefaultLineLineWidth', 2);

% 设置默认字体
set(0, 'DefaultAxesFontName', 'Arial');
set(0, 'DefaultAxesFontSize', 12);

% 设置默认颜色
set(0, 'DefaultFigureColor', 'w');

% 重置默认值
set(0, 'DefaultLineLineWidth', 'factory');

八、总结 #

本章学习了:

  1. 图形对象层次:Figure、Axes、Line等
  2. 线属性:线型、线宽、颜色、标记
  3. 坐标轴属性:范围、刻度、网格、标签
  4. 图例属性:位置、样式
  5. 窗口属性:大小、颜色、标题
  6. 导出图形:saveas、print

下一章将学习子图与布局。

最后更新:2026-03-27