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

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 2)

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.

ribbon function: draw a strip chart

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

% 绘制带状图
ribbon(x, y);

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

% 添加颜色栏
colorbar;

p19

polarhistogram function: draw a polar histogram

% 创建数据
theta = 0:0.1:2*pi;
data = 1/2*theta;

% 绘制极坐标直方图
polarhistogram(theta, data, 'LineWidth', 1.5);

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

p20

contour3 function: draw three-dimensional contour map

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

% 绘制三维等高线图
contour3(X, Y, Z);

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

% 添加颜色栏
colorbar;

p21

scatter3 function: draw a three-dimensional scatter plot

% 创建数据
x = randn(100, 1);
y = randn(100, 1);
z = randn(100, 1);

% 绘制三维散点图
scatter3(x, y, z, 'filled');

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

% 添加网格
grid on;

p22

pareto function: draw a Pareto chart

% 创建数据
categories = {
    
    'Category A', 'Category B', 'Category C', 'Category D'};
values = [20 30 15 35];

% 绘制帕累托图
pareto(values, categories);

% 添加标题和标签
title('Pareto Chart');
xlabel('Categories');
ylabel('Values');

p23

scatterhist function: draw a combination of scatter plots and histograms

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

% 绘制散点图和直方图组合图
scatterhist(x, y, 'Marker', 'o', 'MarkerSize', 6);

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

p24

bar3 function: draw a three-dimensional histogram

% 创建数据
x = 1:5;
y = 1:6;
data = rand(6, 5);

% 绘制三维柱状图
bar3(x, data);

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

% 添加颜色栏
colorbar;

pie3 function: draw a three-dimensional pie chart

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

% 绘制三维饼图
pie3(data);

% 添加标题
title('3D Pie Chart');

p25

compass function: draw polar arrow graph

% 创建数据
theta = 0:pi/4:2*pi;
r = ones(size(theta));

% 绘制极坐标箭头图
compass(r, theta);

% 设置箭头长度
h = findobj(gca, 'Type', 'line');
set(h, 'MarkerSize', 8);

p26

wordcloud function: draw word cloud graph

% 创建数据
words = {
    
    'apple', 'banana', 'orange', 'grape', 'watermelon', 'pineapple'};
counts = [10, 5, 8, 12, 6, 9];

% 绘制词云图
wordcloud(words, counts);

% 添加标题
title('Word Cloud');

p27

parallelplot function: draw parallel coordinate plot

% 创建数据
data = randn(100, 5);

% 绘制平行坐标图
parallelplot(data, 'LineWidth', 1.5);

% 添加标题和标签
title('Parallel Coordinate Plot');
xlabel('Variables');
ylabel('Values');

p28

spiderplot function: draw spider web diagram

% 创建数据
categories = {
    
    'Category A', 'Category B', 'Category C', 'Category D'};
data = [0.6, 0.8, 0.5, 0.7];

% 绘制蛛网图
spiderplot(categories, data, 'Marker', 'o', 'LineWidth', 1.5);

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

bode function: draw frequency response graph

% 创建系统传递函数
num = [1];
den = [1, 1, 1];
sys = tf(num, den);

% 绘制频率响应图
bode(sys);

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

p29

geoplot function: draw geographical coordinates map

% 创建地理数据
lat = [40.7128, 34.0522, 51.5074];
lon = [-74.0060, -118.2437, -0.1278];

% 绘制地理坐标图
geoplot(lat, lon, 'o', 'MarkerSize', 10);

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

p30

comet3 function: draw a three-dimensional comet trajectory diagram

绘制三维彗星轨迹图
% 创建数据
t = linspace(0, 10*pi, 1000);
x = sin(t);
y = cos(t);
z = t;

% 绘制三维彗星轨迹图
comet3(x, y, z);

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

% 创建动画帧并保存为GIF
filename = 'comet3_animation.gif';
for i = 1:length(t)
    % 在每个时间步骤处绘制当前位置的点
    hold on;
    plot3(x(i), y(i), z(i), 'ro', 'MarkerSize', 5);
    
    % 设置坐标轴范围
    xlim([-1.5 1.5]);
    ylim([-1.5 1.5]);
    zlim([0 max(z)]);
    
    % 每个时间步骤的绘图都会被捕获并写入GIF文件
    frame = getframe(gcf);
    im = frame2im(frame);
    [imind, cm] = rgb2ind(im, 256);
    if i == 1
        % 如果是第一帧,则创建新的GIF文件
        imwrite(imind, cm, filename, 'gif', 'Loopcount', inf, 'DelayTime', 0.05);
    else
        % 如果不是第一帧,则追加到现有的GIF文件中
        imwrite(imind, cm, filename, 'gif', 'WriteMode', 'append', 'DelayTime', 0.05);
    end
    
    % 在下一帧绘制之前清除当前位置的点
    cla;
end

disp('动画保存成功!');

[The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-jJyJRmmS-1688491223594)(https://mweb-1307664364.cos.ap-chengdu.myqcloud.com/2023 /07/05/comet3animation1.gif)]

quiver3 function: draw three-dimensional vector field diagram

% 创建数据
[x, y, z] = meshgrid(-2:0.5:2, -2:0.5:2, -2:0.5:2);
u = cos(x).*sin(y).*cos(z);
v = sin(x).*cos(y).*sin(z);
w = cos(x).*cos(y).*sin(z);

% 绘制三维矢量场图
quiver3(x, y, z, u, v, w);

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

p32

dendrogram function: draw a dendrogram

% 创建数据
Z = linkage(rand(10, 3), 'ward');

% 绘制树状图
dendrogram(Z);

% 添加标题
title('Dendrogram');

p33

spy function: draw sparse matrix graph

% 创建稀疏矩阵
A = sparse(eye(5));

% 绘制稀疏矩阵图
spy(A);

% 添加标题
title('Sparse Matrix Plot');

p34

These are sample codes and related comments for more commonly used basic plotting functions in Matlab. Note that these example codes avoid duplication of previously provided examples. You are free to modify and adapt these codes as needed to suit your specific needs.

1.4 Customization and interoperability

Matlab provides a wealth of options and functions that enable users to customize graphic styles, colors, axis labels, legends, etc. In addition, Matlab's graphics window also provides interactive tools, such as zoom, rotation, and translation functions, to facilitate users to browse and operate graphics.

1.5 Conclusion

Matlab provides rich and flexible drawing functions to meet various needs for scientific research and data visualization. By drawing basic graphs, customizing chart properties, and using more advanced drawing techniques, you can create impressive charts that present your data and results in a clear and elegant way.

In the next section, we'll explore various aspects of Matlab plotting in more depth and cover some advanced techniques and practical tips. Stay tuned!

Guess you like

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