快速入門matlab,系統地整理一遍,如何你和我一樣是一個新手,那么此文很適合你;
文章目錄
1 前言
如果你是和我一樣的小白,強烈推薦看看這里,需要合理地利用官方的文檔,通常我覺得官方文檔是最好的,沒有之一,在命令終端輸入help plot
,可以看到詳細的幫助文檔;具體如下;
>> help plot
plot Linear plot.
plot(X,Y) plots vector Y versus vector X. If X or Y is a matrix,
then the vector is plotted versus the rows or columns of the matrix,
whichever line up. If X is a scalar and Y is a vector, disconnected
line objects are created and plotted as discrete points vertically at
X.
plot(Y) plots the columns of Y versus their index.
If Y is complex, plot(Y) is equivalent to plot(real(Y),imag(Y)).
In all other uses of plot, the imaginary part is ignored.
Various line types, plot symbols and colors may be obtained with
plot(X,Y,S) where S is a character string made from one element
from any or all the following 3 columns:
b blue . point - solid
g green o circle : dotted
r red x x-mark -. dashdot
c cyan + plus -- dashed
m magenta * star (none) no line
y yellow s square
k black d diamond
w white v triangle (down)
^ triangle (up)
< triangle (left)
> triangle (right)
p pentagram
h hexagram
For example, plot(X,Y,'c+:') plots a cyan dotted line with a plus
at each data point; plot(X,Y,'bd') plots blue diamond at each data
point but does not draw any line.
plot(X1,Y1,S1,X2,Y2,S2,X3,Y3,S3,...) combines the plots defined by
the (X,Y,S) triples, where the X's and Y's are vectors or matrices
and the S's are strings. For example, plot(X,Y,'y-',X,Y,'go') plots the data twice, with a solid yellow line interpolating green circles at the data points. The plot command, if no color is specified, makes automatic use of the colors specified by the axes ColorOrder property. By default, plot cycles through the colors in the ColorOrder property. For monochrome systems, plot cycles over the axes LineStyleOrder property. Note that RGB colors in the ColorOrder property may differ from similarly-named colors in the (X,Y,S) triples. For example, the second axes ColorOrder property is medium green with RGB [0 .5 0], while plot(X,Y,'g') plots a green line with RGB [0 1 0]. If you do not specify a marker type, plot uses no marker. If you do not specify a line style, plot uses a solid line. plot(AX,...) plots into the axes with handle AX. plot returns a column vector of handles to lineseries objects, one handle per plotted line. The X,Y pairs, or X,Y,S triples, can be followed by parameter/value pairs to specify additional properties of the lines. For example, plot(X,Y,'LineWidth',2,'Color',[.6 0 0]) will create a plot with a dark red line width of 2 points. Example x = -pi:pi/10:pi; y = tan(sin(x)) - sin(tan(x)); plot(x,y,'--rs','LineWidth',2,... 'MarkerEdgeColor','k',... 'MarkerFaceColor','g',... 'MarkerSize',10)
與
plot
相關的函數還有plottools
,semilogx
,semilogy
,loglog
,plotyy
,plot3
,grid
,title
,xlabel
,ylabel
,axis
,axes
,hold
,legend
,subplot
,scatter
.
2 plot
2.1 顯示正弦波
顯示一個簡單的正弦函數;
x=0:2*pi/100:2*pi;
y=sin(x);
plot(x,y);
2.2 修改顏色
參數 | 顏色 |
---|---|
b | blue |
g | green |
r | red |
c | cyan |
m | magenta |
y | yellow |
k | black |
w | white |
下面修改為紅色:
x=0:2*pi/100:2*pi;
y=sin(x);
plot(x,y,'r');
結果如下:
2.3 修改點的形狀
參數 | 形狀 | 圖標 |
---|---|---|
- | solid | |
o |
circle | ![]() |
x |
x-mark | ![]() |
+ |
plus | ![]() |
* |
star | ![]() |
s |
square | ![]() |
d |
diamond | ![]() |
v |
triangle (down) | ![]() |
^ |
triangle (up) | ![]() |
< |
triangle (left) | ![]() |
> |
triangle (right) | ![]() |
p |
pentagram | ![]() |
h |
hexagram | ![]() |
將點形狀顯示為六邊形;
x=0:2*pi/20:2*pi;
y=sin(x);
plot(x,y,'h','MarkerSize',10);
結果如下:
相關參數:
MarkerEdgeColor
:點邊框顏色;MarkerFaceColor
:點表面顏色;MarkerSize
:點的大小;
2.4 修改線的形狀
符號 | 形狀 |
---|---|
: |
dotted |
-. |
dashdot |
-- |
dashed |
x=0:2*pi/20:2*pi;
y=sin(x);
plot(x,y,':','LineWidth',3);
LineWidth
的參數為線寬;
x=0:2*pi/20:2*pi;
y=sin(x);
plot(x,y,'-.','LineWidth',3);
x=0:2*pi/20:2*pi;
y=sin(x);
plot(x,y,'--','LineWidth',3);
2.5 多個參數修改
下面修改多個參數屬性顯示一下正弦波;
x = 0:2*pi/100:2*pi;
y = sin(x);
plot(x,y,'--rs','LineWidth',2,...
'MarkerEdgeColor','k',...
'MarkerFaceColor','g',...
'MarkerSize',10);
結果如下:
3 subplot
subplot
的使用方法如下:
subplot Create axes in tiled positions.
H = subplot(m,n,p), or subplot(mnp), breaks the Figure window
into an m-by-n matrix of small axes, selects the p-th axes for
the current plot, and returns the axes handle. The axes are
counted along the top row of the Figure window, then the second
row, etc. For example,
subplot(2,1,1), PLOT(income)
subplot(2,1,2), PLOT(outgo)
通俗的講:
subplot(行,列,index)
注意:plot
函數要在subplot
表明位置之后再調用。
3.1 2行1列
x=0:2*pi/20:2*pi;
y=sin(x);
subplot(2,1,1);
plot(x,y,'y','LineWidth',3);
subplot(2,1,2);
plot(x,y,'g','LineWidth',3);
需要將多個波形顯示在同一張圖中;
3.2 1行2列
x=0:2*pi/20:2*pi;
y=sin(x);
subplot(1,2,1);
plot(x,y,'y','LineWidth',3);
subplot(1,2,2);
plot(x,y,'g','LineWidth',3);
4 plot3
t = 0:pi/50:10*pi;
plot3(sin(t),cos(t),t);
5 title
title
:圖的標題;xlabel
:x軸標題;ylabel
:y軸標題;
指定plot
的標題,需要在plot調用之后在調用title
,xlabel
或ylabel
;
x=0:2*pi/20:2*pi;
y=sin(x);
subplot(1,2,1);
plot(x,y,'y','LineWidth',3);
title('yellow');
xlabel('yellow-x');
ylabel('yellow-y');
subplot(1,2,2);
plot(x,y,'g','LineWidth',3);
title('green');
xlabel('green-x');
ylabel('green-y');
6 legend
x = 0:.2:12;
plot(x,besselj(1,x),x,besselj(2,x),x,besselj(3,x));
legend('First','Second','Third','Location','NorthEastOutside')
b = bar(rand(10,5),'stacked'); colormap(summer); hold on
x = plot(1:10,5*rand(10,1),'marker','square','markersize',12,...
'markeredgecolor','y','markerfacecolor',[.6 0 .6],...
'linestyle','-','color','r','linewidth',2); hold off
legend([b,x],'Carrots','Peas','Peppers','Green Beans',...
'Cucumbers','Eggplant')
這是官方的demo
,比較復雜;
x=0:2*pi/20:2*pi;
y=sin(x);
plot(x,y,':','LineWidth',3);
legend('test1');
legend
需要在plot之后調用,用於依次解釋第一個plot的波形,如果一個plot里顯示了兩個波形,那legen中字符串也需要設置兩個,分別依次對應plot中的波形;
7 at last
比較簡單,matplotlib
的功能和這個比較類似,總體來說,作為一個和博主一樣的新手,要多看官方的help文檔,然后平時使用的過程中慢慢就熟悉了,最后要多總結。
作者能力有限,文中難免有錯誤和紕漏之處,請大佬們不吝賜教
創作不易,如果本文幫到了您;
請幫忙點個贊 👍👍👍;
請幫忙點個贊 👍👍👍;
請幫忙點個贊 👍👍👍;