Matlab常用的图像属性

线型、标记和颜色

线型、标记和颜色,指定为包含符号的字符串或字符向量。符号可以按任意顺序显示。不需要同时指定所有三个特征(线型、标记和颜色)。例如,如果忽略线型,只指定标记,则绘图只显示标记,不显示线条。



以下是 MATLAB 在许多类型的绘图中使用的默认颜色的 RGB 三元组和十六进制颜色代码。

示例1 plot

% 定义 x 数据范围
x = linspace(0, 2*pi, 100); % 从 0 到 2*pi 的 100 个点

% 定义 y 数据
y1 = sin(x); % 正弦函数
y2 = cos(x); % 余弦函数

% 绘制第一个数据集:sin(x)
plot(x, y1, 'b-o', 'LineWidth', 1.5, 'MarkerSize', 6, 'MarkerFaceColor', 'yellow', 'DisplayName', 'y = sin(x)');
hold on

% 绘制第二个数据集:cos(x)
plot(x, y2, 'r--s', 'LineWidth', 2, 'MarkerSize', 8, 'MarkerFaceColor', 'cyan', 'DisplayName', 'y = cos(x)');

% 设置标题和轴标签
title('Sin and Cos Functions', 'FontSize', 14, 'FontWeight', 'bold', 'Color', 'blue');
xlabel('x', 'FontSize', 12, 'FontWeight', 'bold');
ylabel('y', 'FontSize', 12, 'FontWeight', 'bold');

% 设置图例
legend('Location', 'northeast'); % 图例位置

% 设置轴范围
xlim([0, 2*pi]); % x 轴范围
ylim([-1.5, 1.5]); % y 轴范围

% 设置网格
grid on;
grid minor; % 显示次网格

% 设置轴刻度
set(gca, 'XTick', 0:pi/2:2*pi, 'XTickLabel', {'0', 'π/2', 'π', '3π/2', '2π'}); % 自定义 x 轴刻度
set(gca, 'YTick', -1:0.5:1); % 自定义 y 轴刻度

% 设置背景颜色
set(gcf, 'Color', [1 1 1]); % 图像背景色为白色
set(gca, 'Color', [0.95 0.95 0.95]); % 图表背景色为浅灰色

% 添加注释
text(pi, 0, '\leftarrow  y = sin(x) crosses y = cos(x)', 'FontSize', 10, 'Color', 'black');

% 关闭 hold
hold off

示例2 subplot

% 定义 x 数据
x = linspace(0, 2*pi, 100);

% 创建一个 2x2 的子图布局,每行 2 个子图,共 4 个子图

% 第一个子图:y = sin(x)
subplot(2, 2, 1);  % 2x2 网格的第 1 个子图
y1 = sin(x);
plot(x, y1, 'b-', 'LineWidth', 1.5);
title('y = sin(x)');
xlabel('x');
ylabel('y');
grid on;

% 第二个子图:y = cos(x)
subplot(2, 2, 2);  % 2x2 网格的第 2 个子图
y2 = cos(x);
plot(x, y2, 'r--', 'LineWidth', 1.5);
title('y = cos(x)');
xlabel('x');
ylabel('y');
grid on;

% 第三个子图:y = tan(x)
subplot(2, 2, 3);  % 2x2 网格的第 3 个子图
y3 = tan(x);
plot(x, y3, 'g-.', 'LineWidth', 1.5);
title('y = tan(x)');
xlabel('x');
ylabel('y');
ylim([-5, 5]);  % 限制 y 轴范围以防止 tan(x) 发散
grid on;

% 第四个子图:y = exp(-x) * sin(3*x)
subplot(2, 2, 4);  % 2x2 网格的第 4 个子图
y4 = exp(-x) .* sin(3*x);
plot(x, y4, 'm:', 'LineWidth', 1.5);
title('y = e^{-x} * sin(3x)');
xlabel('x');
ylabel('y');
grid on;

% 设置整体标题
sgtitle('Different Functions on a 2x2 Subplot Layout');

本文作者:碧水云天4

本文链接:https://www.cnblogs.com/smalldong/p/18540633

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   碧水云天4  阅读(56)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
点击右上角即可分享
微信分享提示
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起
  1. 1 love songs Audio artist
  2. 2 love songs Audio artist
  3. 3 love songs Audio artist
  4. 4 love songs Audio artist
love songs - Audio artist
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.