The most dry goods of MATLAB (3)

The dry goods of MATLAB (three)-image rendering

Soon, it came to our image drawing link. In research, image drawing is very commonly used to visualize the data of the research. Below, we will start today's content- image drawing in matlab , elbow~~

First of all, variables are reserved for the following examples

x = 0:0.1:2*pi;
y1 = sin(x);
y2 = cos(x);

1. Drawing of two-dimensional curve

1. Basic functions

(1) plot(y), y is a vector

plot(y1);  % 纵坐标为y的值;横坐标自动为元素序号(角标+1),此处为1~9

(2) plot(y), y is a matrix

figure;   % 开启新绘图窗口,下一次绘图在新窗口
y = [y1',y2'];
plot(y);  % 当y为矩阵,按每一列画出曲线,颜色自动区分

(3) plot(x,y), xy is a vector

plot(x,y1);

(4) plot(x1, y1, x2, y2…)

plot(x,y1,x,y2);  %在同一个窗口绘制两条曲线

Insert picture description here

2. Linear graph format setting

(1) Line color and style

plot (x,y1,'b:o');  %蓝色 点线 圆圈

% b blue g green r red c blue m purple y yellow k black w white% -Solid line: dotted line-dotted line -. stippled line %. solid dot o circle x cross + cross * asterisk
s square d diamond v Triangle^upper triangle<left triangle>right triangle p five-pointed star h six-pointed star


(2) Coordinate axis

plot(x,y1);
axis([-1*pi,3*pi,-1.5,1.5]);  %规定横纵坐标范围

In this example, the axis is x→ [-pi-3pi], y→[-1.5-1.5]

3. Graphic modification

(1) Title tag

title('a title') ;  %图像标题
xlabel ('this is x') % x轴标记,yz 同理

(2) Legend setting

legend('hahaha','location','best');  %str的顺序与绘图顺序一致; 'best'指图例位置最佳化,还有其他位置

Insert picture description here
(3) Graphic retention

plot(x,y1);
hold on ; %在原有的窗口y1曲线上增加绘制下一个图形
plot(x,y2); %y2在同一个窗口被绘制
hold off;

(4) Split drawing

subplot(2, 2, 1);  % 分割成2x2区域,在第一块区域绘制下一个图形
plot(x, y1);  % y1被绘制在4块区域的第一块
subplot(2, 2, 2);  % 分割方法相同,区域改变
plot(x, y2);  % y2在第二块区域

Insert picture description here

2. Two-dimensional feature map drawing

1. Histogram

bar(x, y, width, '参数');

% x horizontal coordinate vector, m elements; when y is a vector, each x draws a vertical bar, a total of m, when the matrix is ​​mxn, each x draws n;
% width defaults to 0.8, more than 1 each will overlap;
% The parameters are grouped and stacked; the default grouped
% bar vertical histogram, barh horizontal histogram, bar3 three-dimensional histogram, barh3 horizontal three-dimensional histogram (three-dimensional one more parameter detached, and is the default)

2. Pie chart

pie(x, explode, 'lable'); 

% x is a vector to display the percentage of each element to the total, and a matrix to display the percentage of each element to the total.
% explode vector is the same length as x, and 1 means that the element is separated and highlighted. By default, all 0s are not separated.
% pie3 draws a three-dimensional pie Figure

3. Histogram

 hist(y, n);  % y为向量,把横坐标分为n段绘制
 hist(y, x);  % x为向量,用于指定每段中间值, 若取N = hist(y, x), N为每段元素个数。

4. Discrete data graph

 stairs(x, y, 'b-o');  % 阶梯图,参数同plot
 stem(x, y, 'fill');  % 火柴杆图,参数fill是填充火柴杆,或定义线形
 candle(HI, LO, CL, OP);  % 蜡烛图:HI为最高价格向量,LO为最低价格向量,CL为收盘价格向量,OP为开盘价格向量

5. Vector graphics

 compass(u, v, 'b-o');  % 罗盘图横坐标u纵坐标v
 compass(Z, 'b-o');  % 罗盘图复向量Z
 feather(u, v, 'b-o');  % 羽毛图横坐标u纵坐标v
 feather(Z, 'b-o');  % 羽毛图复向量Z
 quiver(x, y, u, v);  %(x, y)为起点(u, v)为终点向量场图

6. Polar diagram

% polar(theta, rho,'b-o');% polar angle theta, radius rho

  theta = -pi:0.01:pi;
  rho = sin(theta);
  polar(theta, rho, 'b')

7. Logarithmic graph

  semilogx(x1, y1, 'b-o');  % 把x轴对数刻度表示, semilogy是y轴对数刻度表示,loglog是两个坐标都用对数表示

8. Double ordinate

 plotyy(x1, y1, x2, y2, 'fun1', 'fun2');  % fun规定了两条条线的绘制方式,如plot,semilogx,semilogy,loglog,stem等

9. Function drawing

 f = 'sin(2*x)';
 ezplot(f, [0, 2*pi]);  % 绘制f并规定横坐标范围,也有[xmin, xmax, ymin, ymax]
 x = '2*cos(t)';
 y = '4*sin(t)';
 ezplot(x, y);  % 绘制x(t),y(t)[0, 2*pi]图像, 也可以在最后用[tmin, tmax]规定t的范围

Three, three-dimensional curve and surface drawing

1. Three-dimensional curve

x = 0:0.1:2*pi;
y = sin(x);
z = cos(x);
plo3 (x,y,z,'b-*');

Insert picture description here
2. Three-dimensional surface

(1) Three-dimensional grid

x = -5:0.1:5;  % 规定了x轴采样点,也规定了x轴范围
y = -4:0.1:4;  % 规定了y轴采样点,也规定了y轴范围
[X, Y] = meshgrid(x, y);  % 得到了xoy面网格点
Z = X.^2+Y.^2;
mesh(X, Y, Z)  % XY是meshgrid得到的网格点,Z是网格顶点,c是用色矩阵可省略

Insert picture description here
(3) Three-dimensional surface map

x = -5:0.1:5;  
y = -4:0.1:4;
[X, Y] = meshgrid(x, y);
Z = X.^2+Y.^2;  % 以上部分同上
surf(X, Y, Z)  % 与上一个类似

Insert picture description here
At this point, the content of this section is over. I hope that it can bring you some knowledge that you need. If there is any code word error in the appeal, I hope you can criticize and correct it (the wind and rain, the comment area is waiting for you). Feel free to leave a comment if you understand.
Well, no longer Bibi, see you next time, continue to dry
do not forget to point a praise support Oh, we did not support his brother, my brother is not a dog basket ~

Guess you like

Origin blog.csdn.net/weixin_49005845/article/details/109724044