天天看點

python實驗五圖形繪制_matlab(五)圖形繪制

圖形繪制基礎

步驟

準備資料和函數, 常用指令如下:

x = 0: 0.1: 10;

y1 = bessel(1, x);

y2 = bessel(2, x);

選擇圖形輸出的視窗及位置, 常用指令如下:

figure(1); %打開第一個圖形

subplot(m, n, k); %繪制m×n個圖形, k表示目前圖形的位置

調用基本的繪圖函數, 常用指令如下:

plot(x, y1, x, y2, x, y3); %繪制y1, y2, y3三條曲線

plot3(x, y, z, ‘r:’);%繪制三維曲線, r表示紅色, : 表示曲線

設定坐标軸的範圍, 标記号和網格線, 常用指令如下:

axis([0, 10, -3, 3]);%x軸範圍0到10, y軸範圍-3到3

grid on; %加網格

用名稱, 圖例, 坐标名, 文本等對圖形進行注釋, 常用指令如下:

xlabel(‘x’); %對x軸進行标注, 标注内容為單引号内的内容

titlel(‘圖1’);

text(1, 1, ‘y = f(x)’); %在(1, 1)處标注文本

列印輸出圖形, 常用指令如下:

print-dps2;

基本繪圖指令

plot(y, 's'); %y是一個向量, 其x坐标是元素序号

plot(x, y, 's');

plot(x1, y1, 's1', x2, y2, 's2');

h = plot(...);

plot3(x, y, z, 's');

loglog; %橫縱坐标都是對數形式

semilogx; %x坐标是對數

semilogy; %y坐标是對數

plotyy(x1, y1, x2, y2, '參數1', '參數2'); %同一個圖中兩套坐标軸, 參數1和參數2可以設定坐标形式, 如'loglog'

二維圖形的繪制

同一張圖上畫兩條曲線:

x = 0:0.01:10;

y = sin(x);

y1 = x.*sin(x);

plot(x,y,x,y1);

grid on;

在同一視窗中畫三張圖:

y2 = exp(2*cos(x));

subplot(2,2,1); plot(x, y);

subplot(2,2,2); plot(x, y1);

subplot(2,2,3); plot(x, y2);

自定義曲線樣式:

顔色: 紅色r, 藍色b, 綠色g, 黑色k, 白色w

線型: ":"表示虛線, "-"表示實線, "–"表示劃線, "-."表示點劃線

特殊點标注: “+, ×, ”

plot(x, y, 'r:', x, y1, 'g--', x, y2, 'b-.');

x = 0:0.2:10;

y = sin(x);

y1 = x.*sin(x);

y2 = exp(2*cos(x));

plot(x,y,'r:+',x,y1,'g--d',x,y2,'b-.o')

二維圖的标注:

x= -10:0.1:10;

x= x+(x==0)*eps;%使用近似0,防止出現0/0的情況

y= sin(x)./x;

plot(x, y);

xlabel('x軸');

ylabel('y軸');

title('函數的頻譜');

plot(x,y);

xlabel('x');

ylabel('y = sin(x)');

text(0,sin(0),'\leftarrowsin(x)=0');%左箭頭标注

text(3*pi/4,sin(3*pi/4),'\rightarrowsin(x)=0.707');%右箭頭标注

用gtext指令在曲線上取點:

plot(x,y);

xlabel('x');

ylabel('y');

text(0,sin(0),'\rightarrowsin(x)=0');

text(3*pi/4,sin(3*pi/4),'\leftarrowsin(x)=0.707');

legend('y=cos(x)'); %标注函數名, 有幾條曲線就有幾個參數

grid on;

gtext('No.1')

對數和半對數坐标的繪制:

x = linspace(0,10,100);

y=exp(x);

subplot(1,3,1);

plot(x,y);

subplot(1,3,2);

loglog(x,y);

subplot(1,3,3);

semilogy(x,y)

雙y軸圖形的繪制(plotyy):

x = 0:1:1000;

a = 1000;b=0.01;c=0.01;

y1 = a*exp(-b*x);

y2 = cos(c*x);

plotyy(x,y1,x,y2,'semilogy','plot');

legend('y1=a×exp(-b×x)','y2 = cos(c×x)')

極坐标繪制:

polar(theta, rho, linespec); 其中theta是極角, rho是極徑, linespec用來指定曲線線型, 标記符号和顔色等.

x =0:0.01:2*pi;

polar(x,sin(2*x).*cos(2*x),'r:');

title('八瓣梅花圖')

二維條形圖:

python實驗五圖形繪制_matlab(五)圖形繪制

width是條形圖的寬度, 預設為0.1.

x=[1 2 3];

y = [3 5 2; 4 6 8; 7 5 3];

bar(x,y)

%bar(x,y,'stack'); %畫成堆棧形式

x=[1 2 3];

y = [3 5 2; 4 6 8; 7 5 3];

barh(x,y)

二維區域圖:

A = [1 2 3 4

2 4 6 8

3 5 7 6

7 5 3 2

6 3 2 1];

area(A);

set(gca,'xtick',1:5);%設定x軸

grid on; %顯示網格

set(gca,'layer','top'); %将網格顯示在圖形之上

python實驗五圖形繪制_matlab(五)圖形繪制

矩陣A的每一列構成一條折線, 第一條折線(1, 2, 3, 7, 6)與第二列的值相加到底第二條折線, 以此類推.

二維餅圖:

python實驗五圖形繪制_matlab(五)圖形繪制
python實驗五圖形繪制_matlab(五)圖形繪制
python實驗五圖形繪制_matlab(五)圖形繪制

離散圖:

枝幹圖:

python實驗五圖形繪制_matlab(五)圖形繪制

可以設定樣式, 如:

stem(t, y,’:dr’, ‘fill’); %虛線, 菱形, 紅色, 填充

階梯圖:

python實驗五圖形繪制_matlab(五)圖形繪制

二維輪廓圖:

将相對于某一平面具有同一高度的點連成一條線, 高度由高度矩陣來反映, 即等高線.

python實驗五圖形繪制_matlab(五)圖形繪制

(peaks産生尖峰, 總共畫30條線)

原文連結:https://blog.csdn.net/Surely_Zhu/article/details/105248420