MATLAB advanced drawing

MATLAB advanced drawing

Logarithm Plots

x = logspace(-1,1,100);   % 10的-1~1生成100个等差数列次方,
y = x.^2;    
subplot(2,2,1);
plot(x,y);    % 二次函数
title('Plot');
subplot(2,2,2);
semilogx(x,y);     % plot(log10(x),y)
title('Semilogx');
subplot(2,2,3);
semilogy(x,y);     % plot(x,log10(y))
title('Semilogy');
subplot(2,2,4);
loglog(x, y);      % plot(log10(x),log10(y))   正比关系
title('Loglog'); 

Insert picture description here

plotyy ()

x = 0:0.01:20;
y1 = 200*exp(-0.05*x).*sin(x);
y2 = 0.8*exp(-0.5*x).*sin(10*x);
[AX,H1,H2] = plotyy(x,y1,x,y2);        % 画图并将其属性返回方便后续操作
set(get(AX(1),'Ylabel'),'String','Left Y-axis')   %  将左边的y轴命名
set(get(AX(2),'Ylabel'),'String','Right Y-axis')   %  将右边的y轴命名
title('Labeling plotyy');  %figure的title
set(H1,'LineStyle','--'); set(H2,'LineStyle',':');  %更改line的property

Histogram histogram

y = randn(1,1000);  % 返回一个1*1000的随机项矩阵。
subplot(2,1,1);     % 2行一列第一个图
hist(y,10);         % 分成10分组分
title('Bins = 10');
subplot(2,1,2);
hist(y,50);         % 分成50分等分
title('Bins = 50');

Insert picture description here

Bar Charts

x = [1 2 5 4 8]; y = [x;1:5];   %  y=[1 2 5 4 8;1 2 3 4 5]
subplot(1,3,1); bar(x); title('A bargraph of vector x');  
subplot(1,3,2); bar(y); title('A bargraph of vector y');
subplot(1,3,3); bar3(y); title('A 3D bargraph');

Insert picture description here

Stacked and Horizontal Bar Charts Stacked distribution histogram

x = [1 2 5 4 8];
y = [x;1:5];
subplot(1,2,1);
bar(y,'stacked');    % bar(y)的堆积图
title('Stacked');
subplot(1,2,2);
barh(y);             % 横着的bar(y)
title('Horizontal');

Insert picture description here

Pie Charts

a = [10 5 20 30];
subplot(1,3,1); pie(a);
subplot(1,3,2); pie(a, [0,0,0,1]);   % 0,1拆分
subplot(1,3,3); pie3(a, [0,0,0,1]);  % 3D视图

Insert picture description here

Drawing according to polar coordinates

Annotate subplot(m,n,i) to draw a total of m rows and n columns of graphs, select the i-th graph to draw

x = 1:100; theta = x/10; r = log10(x);
subplot(1,4,1); polar(theta,r);  
theta = linspace(0, 2*pi); r = cos(4*theta);
subplot(1,4,2); polar(theta, r);
theta = linspace(0, 2*pi, 6); r = ones(1,length(theta));
subplot(1,4,3); polar(theta,r);
theta = linspace(0, 2*pi); r = 1-sin(theta);
subplot(1,4,4); polar(theta , r);

Insert picture description here

Stairs and Stem Charts

x = linspace(0, 4*pi, 40); y = sin(x);
subplot(1,2,1); stairs(y);
subplot(1,2,2); stem(y);

Insert picture description here

Exercise

hold on;
t1=linspace(0,10,100)
y1=sin(pi.*t1.^2/4);
plot(t1,y1,'b');
t2=linspace(0,10,100)
y2=sin(pi.*t2.^2/4);
stem(t2,y2,'r');
hold off;

Insert picture description here

fill

t =[0:pi/2:2*pi]; x = cos(t);y=sin(t);
h=fill(x,y,'y'); axis square off;   %将里面填充颜色
set(h,'LineWidth',3);
text(0,0,'WAIT','Color', 'K','FontSize', 66, ...
	'FontWeight','bold','HorizontalAlignment', 'center');

Insert picture description here

Boxplot and Error Bar

x=0:pi/10:pi; y=sin(x);
e=std(y)*ones(size(x));  %sty(y)列的标准差*行和列都是size(x)大小的  1  矩阵
errorbar(x,y,e)   

The function of this function should be to observe the distribution of the data set in a straight line

Insert picture description here

Color Space

G = [46 38 29 24 13]; S = [29 27 17 26 8];
B = [29 23 19 32 7]; h = bar(1:5, [G' S' B']);
set(h(1),'FaceColor',[hex2dec('FF')/255 hex2dec('FF')/255 hex2dec('00')/255])  %设定金牌颜色
set(h(2),'FaceColor',[hex2dec('cc')/255 hex2dec('cc')/255 hex2dec('cc')/255])
set(h(3),'FaceColor',[hex2dec('FF')/255 hex2dec('99')/255 hex2dec('00')/255])
get(gca);
set(gca, 'XTickLabel',{'USA','CHN','GBR','RUS','KOR'})
title('Medal count for top 5 countries in 2012 Olympics');
ylabel('Number of medals'); xlabel('Country');
legend('Gold', 'Silver', 'Bronze')

Insert picture description here

Hexadecimal data table corresponding to various colors

Insert picture description here

Visualizing Data as An Image:

[x, y] = meshgrid(-3:.2:3,-3:.2:3); %x是横着的,y是竖着的
z = x.^2 + x.*y + y.^2; surf( x, y, z); box on;  % surf画空间图
set(gca,'FontSize', 16); zlabel('z');  %设置字号 命名z轴
xlim([-4 4]); xlabel('x'); ylim([-4 4]); ylabel('y'); % 限定xy的范围且命名

Insert picture description here

The role of meshgrid function

axis square/Set the current coordinate system graphics to a square. The ratio of horizontal axis and vertical axis is 1:1
imagesc(z); axis square; xlabel('x'); ylabel('y'); % 从z轴往下看颜色更xy变化

Insert picture description here
Is to show every data with color

Color changes with parameters

Insert picture description here
It can be used in drawing, for example, the color of the 3D view above can use which of the following color changes,
Insert picture description here
for example

 [x, y] = meshgrid(-3:.2:3,-3:.2:3);
z = x.^2 + x.*y + y.^2; surf( x, y, z);colormap(gca,jet);   % 设定上面代码的颜色是jet类型
 box on;  
set(gca,'FontSize', 16); zlabel('z');
xlim([-4 4]); xlabel('x'); ylim([-4 4]); ylabel('y');

Exercise

x = [1:10;3:12;5:14];
imagesc(x); % 显示使用经过标度映射的颜色的图像
colorbar; % 显示色阶的颜色栏
map = zeros(256,3); % 创建一个256*3的矩阵
map(:,2) = (0:255)/255; % 绿色为(0 1 0),所以只取第2colormap(map); % 查看并设置当前颜色图  

Insert picture description here

plot3()

x=0:0.1:3*pi; z1=sin(x); z2=sin(2.*x); z3=sin(3.*x);
y1=zeros(size(x)); y3=ones(size(x)); y2=y3./2;  % 行和列都是size(x)大小的零矩阵
plot3(x,y1,z1,'r',x,y2,z2,'b',x,y3,z3,'g'); grid on;
xlabel('x-axis'); ylabel('y-axis'); zlabel('z-axis');

Insert picture description here
Insert picture description here

Principles for 3D Surface Plots

x = -2:1:2;
y = -2:1:2;
[X,Y] = meshgrid(x,y)  % 将x横着,y竖着
The reason is that when drawing the picture, the selected point is (-2, -2), (-1, -2) (0, -2) If y is not vertical, it will be (-2, -2), (-1 , -1) (0, 0), always a straight line with y=x

Insert picture description here

 x = -3.5:0.2:3.5; y = -3.5:0.2:3.5;
[X,Y] = meshgrid(x,y);
Z = X.*exp(-X.^2-Y.^2);
subplot(1,2,1); mesh(X,Y,Z);
subplot(1,2,2); surf(X,Y,Z);;colormap(gca,jet);

Insert picture description here

What is the difference between mesh and surf in matlab

Summarize the color type and fineness

contour() contour

x = -3.5:0.2:3.5;
y = -3.5:0.2:3.5;
[X,Y] = meshgrid(x,y);
Z = X.*exp(-X.^2-Y.^2);
subplot(2,1,1);
mesh(X,Y,Z);
axis square;
subplot(2,1,2);
contour(X,Y,Z);   %画等高线
axis square;      %x y
% axis square/将当前坐标系图形设置为方形。横轴及纵轴比例是11

Insert picture description here

x = -3.5:0.2:3.5; y = -3.5:0.2:3.5;
[X,Y] = meshgrid(x,y); Z = X.*exp(-X.^2-Y.^2);
subplot(1,3,1); contour(Z,[-.45:.05:.45]); % 等高线的距离切片
axis square;
subplot(1,3,2); [C,h] = contour(Z);
clabel(C,h); axis square;  % 为每条等高线标注
subplot(1,3,3); contourf(Z); %contourf()会填充等高线。
axis square;

Insert picture description here
Insert picture description here

exercise

x= -2:0.05:2;%x共80个点
y= -2:0.05:2;%y共80个点
[X,Y]=meshgrid(x,y);
Z= X.*exp(-X.^2-Y.^2);
[C,h]=contourf(Z,[-.45:.05:.45]);
clabel(C,h);
set(gca,'XTick',1:20:81);%若值要从1开始,否则会不显示第一个数值,81同理
set(gca,'YTick',1:10:81);%因为Y轴间隔较密集,取一半,即10
set(gca,'XTickLabel',-2:1:2);%注意区分 刻度 与 刻度值!
set(gca,'YTicklabel',-2:0.5:2);
colormap(jet);%控制曲面图的颜色
axis square;

Insert picture description here
meshc() and surfc()
is to draw contour lines under the graph

x = -3.5:0.2:3.5; y = -3.5:0.2:3.5;
[X,Y] = meshgrid(x,y); Z = X.*exp(-X.^2-Y.^2);
subplot(1,2,1); meshc(X,Y,Z);
subplot(1,2,2); surfc(X,Y,Z);

Insert picture description here

View Angle: view()

%% 使用view设定不同的视角去看图形
clear; clc; close all;
sphere(50);  % 画球
shading flat;  % 显示风格
material shiny;
axis vis3d off;  % axes画板
set(gcf,'Color',[1 1 1]);  % 设置figure板为白色
view(-45,20);   % 设置固定角度去看图

Insert picture description here

Insert picture description here

sphere(50); % 绘制一个划分成50*50面的单位球体
shading flat; %平面着色,就是一个面就一个颜色,不使用更高级着色
light('Position',[1 3 2]);  %增加光照,设置光照位置
light('Position',[-3 -1 3]);
material shiny;
axis vis3d off;
set(gcf,'Color',[1 1 1]); % 画板,球的背景颜色
view(-45,20); % 从这个角度来看

Insert picture description here
Advanced shading (no shading flat; it's really ugly)
Insert picture description here

Light: light()

clear; clc; close all;
[X, Y, Z] = sphere(64); % 画球,并且获取坐标值
h = surf(X, Y, Z); 
axis square vis3d off; % 坐标尺度相同,并且不显示axes画板
reds = zeros(256, 3);  % 创建一个256*3的零矩阵
reds(:, 1) = (0:256.-1)/255;  % 红色为(0 1 0),所以只取第1colormap(reds);   % 显示颜色
shading interp; 
lighting phong;  % 设置光照
set(h, 'AmbientStrength', 0.75, 'DiffuseStrength', 0.5);
L1 = light('Position', [-1, -1, -1]);  % 获取光照的位置句柄
set(L1, 'Position', [-1, -1, 1]);  % 补光
set(L1, 'Color', 'g');  % 补绿光

shading interp

patch()

%% 显示光的效果
clear; clc; close all;
v = [0 0 0; 1 0 0 ; 1 1 0; 0 1 0; 0.25 0.25 1; ...
0.75 0.25 1; 0.75 0.75 1; 0.25 0.75 1];
f = [1 2 3 4; 5 6 7 8; 1 2 6 5; 2 3 7 6; 3 4 8 7; 4 1 5 8];
subplot(1,2,1); 
patch('Vertices', v, 'Faces', f, ...
'FaceVertexCData', hsv(6), 'FaceColor', 'flat');
view(3);
axis square tight; 
grid on;
subplot(1,2,2); 
patch('Vertices', v, 'Faces', f, ...
'FaceVertexCData', hsv(8), 'FaceColor', 'interp');
view(3); 
axis square tight; %  square xy轴等长,xy轴与图形相切
grid on;
patch(‘PropertyName’,propertyvalue,…)

Use the specified attribute/value parameter pair to specify all the attributes of the patch object. Unless the user explicitly specifies the values ​​of FaceColor and EdgeColor, MATLAB will use the default attribute values. This call format allows users to define patches using Faces and Vertices attribute values

MATLAB-patch to draw polygons

Insert picture description here
matlab——caxis function (set color map range)
MATLAB convolution operation (conv, conv2) explanation

%% 绘制地图
load cape
X=conv2(ones(9,9)/81,cumsum(cumsum(randn(100,100)),2));  % MATLAB卷积运算
surf(X,'EdgeColor','none','EdgeLighting','Phong',...
'FaceColor','interp');
colormap(map); 
caxis([-10,300]);  % 大于小于这个范围的就用一个颜色来画
grid off; 
axis off;

For example: randn(3,4) returns a 3×4 random number matrix.
Insert picture description here
Insert picture description here
Anyway, it will sum and the
Insert picture description here
second column will sum.
Insert picture description here

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_46458164/article/details/110870781