Matlab drawing tutorial series-Matlab 34 drawing function examples (Part 1)

Matlab Drawing Tutorial Series: Revealing the Drawing and Optimization of High-Quality Scientific Charts

Part One: Getting Started

1.1 Introduction

About the purpose and scope of this tutorial

Welcome to the Matlab plotting guide! The goal of this tutorial is to help you transform from a drawing novice to a Matlab drawing master. Whether you're a scientific researcher, a student, or an enthusiast interested in data visualization, this tutorial will reveal tips and strategies for drawing high-quality scientific diagrams that will make your diagrams stand out in papers and reports.

The importance of Matlab drawing in scientific research

You may ask, why choose Matlab as the drawing tool? good question! Matlab drawing function is powerful and flexible, and is widely used in the field of scientific research. With well-drawn charts and graphs, you can better present your data and results, making your research more convincing and impactful. Therefore, mastering Matlab drawing skills will become your effective assistant on the road of scientific research.

1.2 Preparation

Install Matlab and its toolbox

Before embarking on your drawing journey, we first want to make sure that you have successfully installed Matlab and its necessary toolbox. If you don't have Matlab installed yet, don't worry! The official MathWorks website will provide you with download and installation guides.

1.3 Examples of 34 drawing functions in Matlab (Part 1)

Matlab is a powerful scientific computing software and programming language that excels in data visualization. Matlab provides a variety of drawing functions and tools that enable users to create high-quality two-dimensional and three-dimensional graphics and perform various customizations and interactive operations.

plot function: draw a two-dimensional line chart

% 创建数据
x = 0:pi/10:2*pi;
y = sin(x);

% 绘制线图
plot(x, y, 'b--o', 'LineWidth', 2);

% 添加标题和标签
title('Sin Function');
xlabel('X');
ylabel('Y');

% 添加网格
grid on;

% 添加图例
legend('sin(x)');

insert image description here

scatter function: draw a scatter plot

% 创建数据
x = randn(100, 1);
y = randn(100, 1);
sizes = 30 + 100 * rand(1, 100);
colors = rand(1, 100);

% 绘制散点图
scatter(x, y, sizes, colors, 'filled');

% 添加标题和标签
title('Scatter Plot');
xlabel('X');
ylabel('Y');

% 添加网格
grid on;

insert image description here

bar function: draw a histogram

% 创建数据
x = 1:5;
y = [3 7 2 5 9];

% 绘制柱状图
bar(x, y);

% 添加标题和标签
title('Bar Chart');
xlabel('Category');
ylabel('Value');

% 添加刻度标签
xticks(x);
xticklabels({
    
    'A', 'B', 'C', 'D', 'E'});

% 添加网格
grid on;

p3

pie function: draw a pie chart

% 创建数据
data = [30 15 25 10];

% 绘制饼图
pie(data);

% 添加标题和图例
title('Pie Chart');
legend('A', 'B', 'C', 'D');

p4

histogram function: draw a histogram

% 创建数据
data = randn(1000, 1);

% 绘制直方图
histogram(data, 'BinWidth', 0.5, 'Normalization', 'probability');

% 添加标题和标签
title('Histogram');
xlabel('Value');
ylabel('Probability');

% 添加网格
grid on;

p5

contour function: draw contour plots

% 创建数据
x = -2:0.1:2;
y = -2:0.1:2;
[X, Y] = meshgrid(x, y);
Z = X.^2 + Y.^2;

% 绘制等高线图
contour(X, Y, Z);

% 添加标题和标签
title('Contour Plot');
xlabel('X');
ylabel('Y');

% 添加颜色栏
colorbar;

p6

quiver function: draw vector field graph

% 创建数据
x = -2:0.2:2;
y = -2:0.2:2;
[X, Y] = meshgrid(x, y);
U = -Y;
V = X;

% 绘制矢量场图
quiver(X, Y, U, V);

% 添加标题和标签
title('Quiver Plot');
xlabel('X');
ylabel('Y');

% 设置坐标轴范围
axis([-2.5 2.5 -2.5 2.5]);

p7

surf function: draw a three-dimensional surface graph

% 创建数据
x = -2:0.2:2;
y = -2:0.2:2;
[X, Y] = meshgrid(x, y);
Z = X.^2 + Y.^2;

% 绘制三维曲面图
surf(X, Y, Z);

% 添加标题和标签
title('Surface Plot');
xlabel('X');
ylabel('Y');
zlabel('Z');

% 添加颜色栏
colorbar;

% 设置视角
view(45, 30);

p8

imagesc function: draw heat map

% 创建数据
data = peaks(100);

% 绘制热图
imagesc(data);

% 添加标题和颜色栏
title('Heatmap');
colorbar;

% 调整坐标轴
axis image;

% 添加刻度标签
xticks(0:20:100);
yticks(0:20:100);

% 添加刻度标签标签
xticklabels({
    
    '0', '1', '2', '3', '4', '5'});
yticklabels({
    
    '0', '1', '2', '3', '4', '5'});

p9

errorbar function: draw a line chart with error bars

% 创建数据
x = 1:5;
y = [3 7 2 5 9];
y_error = [0.5 1 0.8 0.3 1.2];

% 绘制带有误差条的折线图
errorbar(x, y, y_error, 'o-', 'LineWidth', 2, 'MarkerSize', 8);

% 添加标题和标签
title('Errorbar Plot');
xlabel('Category');
ylabel('Value');

% 添加刻度标签
xticks(x);
xticklabels({
    
    'A', 'B', 'C', 'D', 'E'});

% 添加网格
grid on;

p10

area function: draw a filled area map

% 创建数据
x = 0:0.1:2*pi;
y1 = sin(x);
y2 = cos(x);

% 绘制填充区域图
area(x, [y1; y2]', 'LineWidth', 1.5);

% 添加标题和标签
title('Area Plot');
xlabel('X');
ylabel('Y');

% 添加图例
legend('sin(x)', 'cos(x)');

% 添加网格
grid on;

p11

stem function: plot discrete signals

% 创建数据
x = 0:0.1:2*pi;
y = sin(x);

% 绘制离散信号图
stem(x, y, 'filled', 'LineWidth', 1.5, 'MarkerSize', 8);

% 添加标题和标签
title('Discrete Signal Plot');
xlabel('X');
ylabel('Y');

% 添加网格
grid on;

p12

polarplot function: plot polar coordinates

% 创建数据
theta = linspace(0, 2*pi, 100);
rho = abs(sin(2*theta));

% 绘制极坐标图
polarplot(theta, rho, 'LineWidth', 2);

% 添加标题
title('Polar Plot');

p13

loglog function: draw logarithmic coordinate plot

% 创建数据
x = logspace(0, 3, 100);
y = 1./(x.^2);

% 绘制双对数坐标图
loglog(x, y, 'r-', 'LineWidth', 2);

% 添加标题和标签
title('Log-Log Plot');
xlabel('X');
ylabel('Y');

p14

contourf function: draw filled contour plot

% 创建数据
x = -2:0.1:2;
y = -2:0.1:2;
[X, Y] = meshgrid(x, y);
Z = X.^2 + Y.^2;

% 绘制填充等高线图
contourf(X, Y, Z);

% 添加标题和标签
title('Filled Contour Plot');
xlabel('X');
ylabel('Y');

% 添加颜色栏
colorbar;

p15

waterfall function: draw waterfall chart

% 创建数据
x = -5:0.5:5;
y = -5:0.5:5;
[X, Y] = meshgrid(x, y);
Z = peaks(length(x));

% 绘制瀑布图
waterfall(X, Y, Z);

% 添加标题和标签
title('Waterfall Plot');
xlabel('X');
ylabel('Y');
zlabel('Z');

% 设置视角
view(-30, 30);

p16

rose function: draw rose diagram

% 创建数据
theta = 0:0.1:2*pi;
data = abs(sin(3*theta));

% 绘制极坐标玫瑰图
rose(theta, data);

% 添加标题
title('Rose Plot');

p17

stem3 function: draw three-dimensional discrete signal graph

% 创建数据
t = 0:0.1:10;
x = sin(t);
y = cos(t);
z = t;

% 绘制三维离散信号图
stem3(x, y, z, 'filled', 'LineWidth', 1.5, 'MarkerSize', 8);

% 添加标题和标签
title('3D Discrete Signal Plot');
xlabel('X');
ylabel('Y');
zlabel('Z');

% 添加网格
grid on;

p18

Guess you like

Origin blog.csdn.net/qq_42818403/article/details/131545466