Matlab learning [5] Data Visualization

I. Overview

Generally, in the MATLAB process data visualization have the following main steps:
(1) the need to prepare drawing data of the form pattern in MATLAB.
(2) Create a graphic form, and the plotted data selection area. It may comprise a plurality of MATLAB graphic form drawing area.
(3) using MATLAB draw graphics drawing function or curve.
(4) Set property curves, such as linear, width and the like.
(5) set the properties of the drawing area, and adds the data grid lines.
(6) add graphic drawing title, axis labels, or other text label.
(7) Print or export graphics.

Second, two-dimensional graphics

1. Draw function plot

The vector or matrix data plotted in graphic form, and specify a different line and color. function can also plot a plurality of curves plotted.

Example: basic drawing instructions using MATLAB -
X = 0: PI / 1000: 2 PI; prepare to render data%
Y = SiN (2
X + PI /. 4); prepare to render% data
plot (x, y)% setting the default data x and y are plotted in the graph form

Alt
Note: The default drawn blue curve.

plot function while drawing a plurality of curves:
plot (X, Y, X, + Y. 1, X, Y + 2)

Alt

2. The direct drawing data matrix

Plot function using the matrix data may be directly plotted in graphic form, each column of the data matrices plotted as a curve form.

例 :绘制矩阵数据——
A = pascal(5)
A =
1 1 1 1 1
1 2 3 4 5
1 3 6 10 15
1 4 10 20 35
1 5 15 35 70
plot(A)

Alt

3.设置曲线的样式属性

MATLAB预先设置了不同的曲线样式属性值,分别控制曲线的色彩、线型和标识符,见下表——
Alt
Alt
上图例子中的plot函数——

‘-.rv’, ‘r-.v’, ‘vr-. ’都是“红色、点划线、下三角”曲线。

绘制出的曲线是这样的——
Alt
操作补充——
为了能够更加直观地观察数据曲线,可以使用grid命令将轴的坐标线绘制出来,具体的做法为grid on
执行该命令后,图形窗体的轴将显示坐标网格线!如下图——
(若不希望出现坐标网格线,则可以执行grid off命令)
Alt
更精细的“修图”操作见PPT

4.使用子图

MATLAB的图形窗体可以划分为多个图形显示区域,每个图形显示区域彼此独立,用户可以根据自己的需要把数据绘制在指定的区域中,这种特性就是MATLAB图形窗体的子图功能。
使用子图的方法是使用subplot函数选择绘制区域。

subplot(m,n,p)

其中,m和n为将图形窗体分割成的行数和列数,p为选定的窗体区域的序号,以行元素优先顺序排列。
Alt

5.图形编辑器

MATLAB不仅能够显示数据和图形,而且还可以利用图形窗体中的工具对图形对象进行编辑,这时的图形窗体需要进入到编辑模式。
在编辑模式下,可以向图形窗体中的对象添加文本、箭头、直线等,还可以利用编辑工具完成图形对象的编辑工作。

进入图形编辑模式有以下几种方法——
①执行图形窗体中“Tool”菜单下的“Edit Plot”命令。
②单击图形窗体工具栏中选择对象按钮 。

三、格式化绘图

1.添加标题(title)

添加图形的标题(图名)需要使用title函数,基本用法为:

title(‘string’)
%字符串string为图形窗体的标题,该标题将被自动地设置在轴的正中顶部。

2.添加图例(legend)

图例默认绘制在轴的右上角处,其中包括绘制在轴内曲线的 色彩、样式和时标 ,同时在绘制图例的地方为每一个曲线添加简要的说明文字,便于用户了解数据曲线的信息。

添加轴的图例需要使用函数legend,基本语法为

legend(‘string1’, ‘string2’…)

%其中,字符串string1、string2为图例的说明性文本。

例如——

legend(‘y=sin(t)’,‘y=sin(t-pi/2)’,‘y=sin(t-pi)’)

Alt

3.添加坐标轴标签(label)

坐标轴的标签可以用来说明与坐标轴有关的信息,也可以包含各种需要添加的信息,如坐标轴数据的单位、物理意义等。
MATLAB可以为不同的坐标轴添加不同的坐标信息。一般地,可以使用xlabel、ylabel和zlabel函数分别为图形窗体的X轴、Y轴和Z轴添加轴标签

以X轴为例,基本语法如下:

xlabel(‘string’)
%其中,string就是坐标轴的标签。坐标轴的标签自动与坐标轴居中对齐。

e.g.

plot(sin(0:pi/100:pi))
xlabel(‘X轴数据’);ylabel(‘Y轴数据’)

输出——
Alt

4.添加文本注释(text)

文本注释是由创建图形的用户添加的说明行文字,这些文字可以用来说明数据曲线的细节特点,比如,需要特别注意的数据点。

text函数的基本语法为:

text(x, y, ‘string’)
% 其中,x和y是文本注释添加的 起始点坐标值 ,该坐标值使用当前轴系的单位设置。

Alt
Alt

四、各式各样的图形函数

1.条形图

bar 绘制二维垂直条形图
bar3 绘制三维垂直条形图
barh 绘制二维水平条形图
bar3h 绘制三维水平条形图

2.直方图

Alt
Alt

3.饼图

pie(x) —— x中的每一个数据对应饼图中的一个扇区。
pie(a,b) ——从一个饼图中分离出一个或多个饼片,b是与a同尺寸的矩阵,b中非零元素把与a对应位置的饼分离出来。
pie3 —— 绘制三维饼图。
Alt
打印出二维饼图分离饼图如下:
Alt
还有很多花花的图~见PPT

五、基本三维绘图

Three-dimensional graphics performance capabilities better than the two-dimensional graphics, MATLAB capabilities in many cases requires the use of three-dimensional graphics rendering.
Alt
Output -
Alt
function can be plot3 X, Y, Z coordinates of the plotted in three-dimensional space, the basic syntax is as follows:

plot3(xdata, ydata, zdata, ‘clm’, …)

Released nine original articles · won praise 6 · views 2103

Guess you like

Origin blog.csdn.net/RickieLim/article/details/104304475