图形绘制

基本二维绘图

plot 函数

最基本的绘图函数,用于绘制二维线图:

plot(x, y)

简单示例

# 绘制简单的曲线
x = 0:0.1:2*pi
y = sin(x)

plot(x, y)

# 添加标签
xlabel('x')
ylabel('sin(x)')
title('正弦函数')
grid on

绘制多条曲线

# 方法1:多次调用 plot
x = 0:0.1:2*pi
y1 = sin(x)
y2 = cos(x)

plot(x, y1)
hold on
plot(x, y2)
hold off

legend('sin(x)', 'cos(x)')
xlabel('x')
ylabel('y')
title('三角函数')
grid on

# 方法2:一次调用传入多组数据
plot(x, y1, x, y2)

绘制矩阵数据

# 如果 Y 是矩阵,plot 会绘制每一列
x = 0:0.1:2*pi
Y = [sin(x); cos(x); sin(2*x)]'  # 每列一条曲线

plot(x, Y)
legend('sin(x)', 'cos(x)', 'sin(2x)')
grid on

LineSpec 格式

使用 LineSpec 字符串可以快速指定线条的颜色、标记和线型:

plot(x, y, LineSpec)

颜色代码

字符 颜色
'r'红色 (red)
'g'绿色 (green)
'b'蓝色 (blue)
'c'青色 (cyan)
'm'品红 (magenta)
'y'黄色 (yellow)
'k'黑色 (black)
'w'白色 (white)

标记符号

字符 标记
'o'圆圈
'+'加号
'*'星号
'.'
'x'叉号
's'方块
'd'菱形
'^'上三角
'v'下三角

线型

字符 线型
'-'实线(默认)
'--'虚线
':'点线
'-.'点划线

组合使用

x = 0:0.5:2*pi

# 红色圆圈标记,实线
plot(x, sin(x), 'ro-')

# 蓝色星号标记,虚线
plot(x, cos(x), 'b*--')

# 绿色加号标记,点线
plot(x, sin(2*x), 'g+:')

# 品红色方块标记,点划线
plot(x, cos(2*x), 'ms-.')

# 只有标记,没有连线
plot(x, sin(x), 'ro')

Name-Value 参数对

使用 Name-Value 对可以更精细地控制图形属性:

线条属性

x = 0:0.1:2*pi
y = sin(x)

plot(x, y, 'Color', 'red', 'LineWidth', 2, 'LineStyle', '--')

# 常用属性:
# 'Color' - 颜色(RGB 或名称)
# 'LineWidth' - 线宽
# 'LineStyle' - 线型
# 'Marker' - 标记符号
# 'MarkerSize' - 标记大小
# 'MarkerFaceColor' - 标记填充颜色
# 'MarkerEdgeColor' - 标记边缘颜色

示例:自定义样式

x = 0:0.5:2*pi
y = sin(x)

plot(x, y, ...
    'Color', [0.8, 0.2, 0.2], ...      # RGB 颜色
    'LineWidth', 2.5, ...               # 线宽
    'LineStyle', '--', ...              # 虚线
    'Marker', 'o', ...                  # 圆圈标记
    'MarkerSize', 8, ...                # 标记大小
    'MarkerFaceColor', 'blue', ...      # 标记填充为蓝色
    'MarkerEdgeColor', 'black')         # 标记边缘为黑色

xlabel('x')
ylabel('sin(x)')
title('自定义样式的绘图')
grid on

三维绘图

plot3 函数

绘制三维曲线:

plot3(x, y, z)

基本示例

# 螺旋线
t = 0:0.1:10*pi
x = cos(t)
y = sin(t)
z = t

plot3(x, y, z)
xlabel('X')
ylabel('Y')
zlabel('Z')
title('三维螺旋线')
grid on

参数方程曲线

# 绘制参数方程曲线
t = 0:0.01:2*pi

# 圆环曲线
R = 3  # 大半径
r = 1  # 小半径
x = (R + r*cos(10*t)) .* cos(t)
y = (R + r*cos(10*t)) .* sin(t)
z = r * sin(10*t)

plot3(x, y, z, 'LineWidth', 2)
xlabel('X')
ylabel('Y')
zlabel('Z')
title('圆环曲线')
grid on
axis equal

多条三维曲线

t = 0:0.1:10*pi

# 第一条曲线
x1 = cos(t)
y1 = sin(t)
z1 = t

# 第二条曲线
x2 = cos(t)
y2 = sin(t)
z2 = -t

plot3(x1, y1, z1, 'r-', x2, y2, z2, 'b--')
legend('向上螺旋', '向下螺旋')
grid on

散点图

scatter 函数

绘制散点图:

scatter(x, y)
scatter(x, y, size)           # 指定点的大小
scatter(x, y, size, color)    # 指定大小和颜色

基本散点图

# 随机散点
x = randn(50, 1)
y = randn(50, 1)

scatter(x, y)
xlabel('X')
ylabel('Y')
title('散点图')
grid on

自定义大小和颜色

x = randn(100, 1)
y = randn(100, 1)
sizes = randi([20, 100], 100, 1)  # 随机大小

scatter(x, y, sizes)
title('不同大小的散点')
grid on

# 根据数据着色
colors = x + y  # 颜色随 x+y 变化
scatter(x, y, 50, colors)
colorbar
title('按数值着色的散点图')

scatter3 - 三维散点图

x = randn(100, 1)
y = randn(100, 1)
z = randn(100, 1)

scatter3(x, y, z)
xlabel('X')
ylabel('Y')
zlabel('Z')
title('三维散点图')
grid on

图形控制

figure - 创建新图形窗口

# 创建新窗口
figure

# 切换到指定编号的窗口
figure(1)
plot(x, sin(x))

figure(2)
plot(x, cos(x))

# 返回窗口1
figure(1)
title('窗口1中的图形')

hold - 保持当前图形

x = 0:0.1:2*pi

# 在同一图形上绘制多条曲线
plot(x, sin(x), 'r-')
hold on
plot(x, cos(x), 'b--')
plot(x, sin(2*x), 'g-.')
hold off

legend('sin(x)', 'cos(x)', 'sin(2x)')
grid on

clf - 清除当前图形

plot(x, y)
# ... 其他操作 ...
clf  # 清除图形,准备绘制新的

grid - 网格线

plot(x, y)
grid on   # 显示网格
grid off  # 隐藏网格

图形标注

标题和坐标轴标签

x = 0:0.1:2*pi
y = sin(x)

plot(x, y)

# 标题
title('正弦函数图')

# 坐标轴标签
xlabel('时间 (秒)')
ylabel('振幅')

# 可以使用中文
title('正弦波形图')
xlabel('横坐标')
ylabel('纵坐标')

图例

x = 0:0.1:2*pi

plot(x, sin(x), 'r-')
hold on
plot(x, cos(x), 'b--')
plot(x, tan(x), 'g-.')
hold off

# 添加图例
legend('正弦', '余弦', '正切')

# 指定图例位置
legend('sin', 'cos', 'tan', 'Location', 'best')
# 可选位置: 'northeast', 'northwest', 'southeast', 'southwest', 'best'

grid on

坐标轴范围

x = 0:0.1:2*pi
y = sin(x)

plot(x, y)

# 设置 x 轴范围
xlim([0, 2*pi])

# 设置 y 轴范围
ylim([-1.5, 1.5])

# 同时设置
axis([0, 2*pi, -1.5, 1.5])

# 自动调整为相等比例
axis equal

# 紧凑布局
axis tight

主题和样式

theme 函数

设置图形主题:

# 设置为深色主题
theme('dark')

# 设置为亮色主题
theme('light')

# 设置为默认主题
theme('default')

示例

# 使用深色主题绘图
theme('dark')

x = 0:0.1:2*pi
plot(x, sin(x), 'c-', 'LineWidth', 2)
hold on
plot(x, cos(x), 'm--', 'LineWidth', 2)
hold off

xlabel('x')
ylabel('y')
title('深色主题示例')
legend('sin(x)', 'cos(x)')
grid on

动画和交互

drawnow - 更新图形

在循环中绘制动画:

# 简单动画示例
x = 0:0.1:2*pi

for phase = 0:0.1:2*pi
    y = sin(x + phase)
    plot(x, y)
    ylim([-1.5, 1.5])
    title('正弦波动画')
    grid on
    drawnow  # 立即更新显示
    pause(0.05)  # 暂停 50 毫秒
end

pause - 暂停执行

# 暂停指定秒数
pause(1)      # 暂停 1 秒
pause(0.5)    # 暂停 0.5 秒

# 等待用户按键
pause  # 按任意键继续

动画示例:移动的点

# 在圆周上移动的点
theta = 0:0.1:2*pi
x_circle = cos(theta)
y_circle = sin(theta)

for t = 0:0.1:2*pi
    # 绘制圆
    plot(x_circle, y_circle, 'b-')
    hold on
    
    # 绘制移动的点
    x_point = cos(t)
    y_point = sin(t)
    plot(x_point, y_point, 'ro', 'MarkerSize', 15, 'MarkerFaceColor', 'r')
    
    hold off
    axis equal
    xlim([-1.5, 1.5])
    ylim([-1.5, 1.5])
    title('圆周运动')
    grid on
    
    drawnow
    pause(0.05)
end

综合示例

示例1:李萨如图形

# 李萨如曲线(两个正交简谐运动的合成)
t = 0:0.01:2*pi
a = 3  # x 方向频率
b = 2  # y 方向频率
delta = pi/2  # 相位差

x = sin(a*t)
y = sin(b*t + delta)

plot(x, y, 'LineWidth', 2)
axis equal
grid on
xlabel('x')
ylabel('y')
title('李萨如图形 (a=' + num2str(a) + ', b=' + num2str(b) + ')')

示例2:多子图对比

# 如果支持子图,可以这样:
x = 0:0.1:2*pi

# 子图1
subplot(2, 2, 1)
plot(x, sin(x))
title('sin(x)')
grid on

# 子图2
subplot(2, 2, 2)
plot(x, cos(x))
title('cos(x)')
grid on

# 子图3
subplot(2, 2, 3)
plot(x, tan(x))
title('tan(x)')
ylim([-5, 5])
grid on

# 子图4
subplot(2, 2, 4)
plot(x, exp(-x))
title('exp(-x)')
grid on

示例3:数据可视化

# 绘制实验数据和拟合曲线
x_data = [1 2 3 4 5 6 7 8 9 10]
y_data = [2.1 3.9 6.2 7.8 10.1 11.9 14.2 15.8 18.1 19.9]

# 线性拟合
A = [x_data', ones(length(x_data), 1)]
coeffs = A \ y_data'
y_fit = A * coeffs

# 绘制数据点和拟合线
scatter(x_data, y_data, 50, 'filled')
hold on
plot(x_data, y_fit, 'r-', 'LineWidth', 2)
hold off

xlabel('X')
ylabel('Y')
title('实验数据与线性拟合')
legend('实验数据', '拟合直线')
grid on

# 显示拟合方程
slope = coeffs(1)
intercept = coeffs(2)
text(2, 15, "y = " + num2str(slope, '%.2f') + "x + " + num2str(intercept, '%.2f'))

示例4:极坐标图

# 玫瑰线(如果支持极坐标)
theta = 0:0.01:2*pi
r = sin(3*theta)

# 转换为直角坐标
x = r .* cos(theta)
y = r .* sin(theta)

plot(x, y, 'LineWidth', 2)
axis equal
grid on
title('玫瑰线 r = sin(3θ)')

示例5:矢量场

# 如果支持 quiver 函数
[X, Y] = meshgrid(-2:0.3:2, -2:0.3:2)
U = -Y
V = X

quiver(X, Y, U, V)
axis equal
xlabel('x')
ylabel('y')
title('旋转矢量场')
grid on

绘图最佳实践

1. 始终添加标签

# 好的做法
plot(x, y)
xlabel('时间 (s)')
ylabel('速度 (m/s)')
title('速度随时间变化')
grid on

# 避免没有任何标注的图

2. 使用合适的颜色和线型

# 多条曲线时使用不同的样式
plot(x, y1, 'r-', x, y2, 'b--', x, y3, 'g-.')
legend('数据1', '数据2', '数据3')

# 确保在黑白打印时也能区分

3. 控制坐标轴范围

# 确保重要部分可见
plot(x, y)
xlim([x_min, x_max])
ylim([y_min, y_max])

# 或使用 axis tight 自动调整
axis tight

4. 使用网格线

# 网格线帮助读取数值
plot(x, y)
grid on

恭喜!您已完成 TianYuan 核心教程的学习。接下来可以查阅函数参考手册深入了解各个函数的详细用法。