您的位置:首页 > 编程语言 > MATLAB

MatLab绘图命令

2015-10-13 09:44 525 查看
a=linspace(1,2,10)
plot(a,'--pr','linewidth',1.5,'MarkerEdgeColor','r','MarkerFaceColor','m','MarkerSize',10)
legend('a','Location','best')
title('a','FontName','Times New Roman','FontWeight','Bold','FontSize',16)
xlabel('T','FontName','Times New Roman','FontSize',14)
ylabel('a','FontName','Times New Roman','FontSize',14,'Rotation',0)
axis auto equal
set(gca,'FontName','Times New Roman','FontSize',14)


1.曲线线型、颜色和标记点类型

plot(X1,Y1,LineSpec, …) 通过字符串LineSpec指定曲线的线型、颜色及数据点的标记类型。

实线 r 红色 + 加号

-. 点划线 g 绿色 o 圆圈
– 虚线 b 蓝色 *星号
点线 c 蓝绿色 . 点

m 洋红色 x 交叉符号

y 黄色 square(或s) 方格

k 黑色 diamond(或d) 菱形

w 白色 ^向上的三角形

v向下的三角形 >向左的三角形

<向右的三角形 pentagram(或p) 五边形

hexagram(或h) 六边形

线的颜色是由红绿蓝(RGB)三原色组成的,通过设定三原色的权重可以改变线的颜色,命令如下(x为横坐标,y为纵坐标):

color的域值为0—1

plot(x,y,'Color',[1 0 0]);代表红色
plot(x,y,'Color',[0 1 0]);代表绿色
plot(x,y,'Color',[0 0 1]);代表蓝色
plot(x,y,'Color',[0.3 0.8 0.9]);则是三种颜色组成的新颜色。


当需要以上常用的八种以外的颜色时,只要设置颜色中RGB的值就可以得到不同的颜色。

2.设置曲线线宽、标记点大小,标记点边框颜色和标记点填充颜色等。

plot(…,’Property Name’, Property Value, …)

Property Name 意义 选项

LineWidth 线宽 数值,如0.5,1等,单位为points

MarkerEdgeColor 标记点边框线条颜色 颜色字符,如’g’, ’b’等

MarkerFaceColor 标记点内部区域填充颜色 颜色字符

MarkerSize 标记点大小 数值,单位为points

例: 设置图线的线形、颜色、宽度、标记点的颜色及大小。

t=0:pi/20:pi; y=sin(4*t).*sin(t)/2;
plot(t,y,'-bs','LineWidth',2,'MarkerEdgeColor','k', 'MarkerFaceColor', 'y','MarkerSize',10);


3.坐标轴设置

范围设置:

axis([xmin xmax ymin ymax])设置坐标轴在指定的区间

axis auto 将当前绘图区的坐标轴范围设置为MATLAB自动调整的区间

axis manual 冻结当前坐标轴范围,以后叠加绘图都在当前坐标轴范围内显示

axis tight 采用紧密模式设置当前坐标轴范围,即一用户数据范围为坐标轴范围

比例:

axis equal 等比例坐标轴

axis square 以当前坐标轴范围为基础,将坐标轴区域调整为方格形

axis normal 自动调整纵横轴比例,使当前坐标轴范围内的图形显示达到最佳效果

范围选项和比例设置可以联合使用

默认的设置为axis auto normal

axis ij :设置坐标轴的原点在左上角,i为纵坐标,j为横坐标

axis xy :使坐标轴回到直角坐标系

axis off :使坐标轴消隐

axis on :显现坐标轴

4.坐标轴刻度设置

set(gca,'XTick', [0 1 2])               X坐标轴刻度数据点位置
set(gca,'XTickLabel',{'a','b','c'})     X坐标轴刻度处显示的字符
set(gca,'FontName','Times New Roman','FontSize',14)     设置坐标轴刻度字体名称,大小


‘FontWeight’,’bold’ 加粗

‘FontAngle’,’italic’ 斜体

对字体的设置也可以用在title, xlabel, ylabel等中。

5.图例

legend(‘a’,’Location’,’best’) 图例位置放在最佳位置

代码解释
‘North’图例标识放在图顶端
‘South’图例标识放在图底端
‘East’图例标识放在图右方
‘West’图例标识放在图左方
‘NorthEast’图例标识放在图右上方(默认)
‘NorthWest图例标识放在图左上方
‘SouthEast’图例标识放在图右下角
‘SouthWest’图例标识放在图左下角
‘NorthOutside’图例标识放在图框外侧上方
‘SouthOutside’图例标识放在图框外侧下方
‘EastOutside’图例标识放在图框外侧右方
‘WestOutside’图例标识放在图框外侧左方
‘NorthEastOutside’图例标识放在图框外侧右上方
‘NorthWestOutside’图例标识放在图框外侧左上方
‘SouthEastOutside’图例标识放在图框外侧右下方
‘SouthWestOutside’图例标识放在图框外侧左下方
‘Best’图标标识放在图框内不与图冲突的最佳位置
‘BestOutside’图标标识放在图框外使用最小空间的最佳位置
还是用上面的例子

legend(‘sin’,’cos’,’location’,’northwest’)可以将标识框放置在图的左上角。

Examples:

x = 0:.2:12;
plot(x,bessel(1,x),x,bessel(2,x),x,bessel(3,x));
legend('First','Second','Third');
legend('First','Second','Third','Location','NorthEastOutside')
b = bar(rand(10,5),'stacked'); colormap(summer); hold on
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')


6.图形窗口(figure window)

(1)图形窗口的创建和选择(Creating and selecting of figure window)

figure(n):用于为当前的绘图创建图形窗口,每运行一次figure就会创建一个新的图形窗口,n表示第n个窗口,如果窗口定义了句柄,也可以用figure(h)将句柄h的窗口作为当前窗口。

clf :用于清除当前图形窗口中的内容。

shg :用于显示当前图形窗口。

(2)在一个图形窗口中绘制多个子图形(Drawing several subfigures in a single window)

subplot(m,n,p):把窗口分成m×n个小窗口,并把第p个窗口当作当前窗口。

例:将4 个图形显示在同一个图形窗口中。

t=0:pi/20:2*pi; [x,y]=meshgrid(t);

subplot(2,2,1); plot(sin(t),cos(t)); axis equal

subplot(2,2,2); z=sin(x)+cos(y); plot(t,z); axis([0 2*pi –2 2])

subplot(2,2,3); z=sin(x).*cos(y); plot(t,z); axis([0 2*pi –1 1])

subplot(2,2,4); z=sin(x).^2-cos(y).^2; plot(t,z); axis([0 2*pi –1 1])

(3)在一个已有的图形上绘图(Drawing a figure on the figure was existed)

hold on :在一个已有的图形上继续绘图;

hold off: 命令结束继续绘图。

例:将peaks函数的等高线图与伪彩色画在一起。

[x,y,z]=peaks; %产生双变量数组
contour(x,y,z,20,'k') %绘制等高线
hold on
pcolor(x,y,z) %绘制伪彩色图
shading interp %表面色彩渲染
hold off


7.加注坐标轴标识和图形标题(Add axis labels and title of figure)

加注坐标轴标识:xlabel(‘s’), ylabel(‘s’)

图形标题: title(‘s’)

例:加注坐标轴标示和图形标题。

t=0:pi/100:2*pi;y=sin(t);
plot(t,y)
axis([0 2*pi,-1 1])
xlabel('0 \leq \itt \rm \leq \pi','FontSize',16)
ylabel('sin(t)','FontSize',20)
title('正弦函数图形','FontName','隶书','FontSize',20)


8. 图中加注文本(Add text in the figure)

text(x,y,’字符串’)

例:在上图中加语句。

t=0:pi/100:2*pi;
y=sin(t);
plot(t,y)
axis([0 2*pi,-1 1])
xlabel('0 \leq \itt \rm \leq \pi','FontSize',16)
ylabel('sin(t)','FontSize',20)
title('正弦函数图形','FontName','隶书','FontSize',20)
text(3*pi/4,sin(3*pi/4),'\leftarrowsin(t)=0.707', 'FontSize',16)
text(pi,sin(pi),'\leftarrowsin(t)=0', 'FontSize',16)
text(5*pi/4,sin(5*pi/4),'sin(t)=-0.707\rightarrow','FontSize',16,'HorizontalAlignment','right')


句中:

leftarrow 表示加一个向左的箭头

rightarrow 表示加一个向右的箭头

HorizontalAlignment 表示右对齐水平排列

gtext(‘字符串’): 在图形窗口上用鼠标直接在指定的位置上加注文本。

例:

t=0:pi/100:2*pi;
y=sin(t);
plot(t,y)
axis([0 2*pi,-1 1])
xlabel('0 \leq \itt \rm \leq \pi','FontSize',16)
ylabel('sin(t)','FontSize',20)
title('正弦函数图形','FontName','隶书','FontSize',20)
gtext('MATLAB')


9. 指定TeX字符

例:在标题中指定TeX字符

t=0:pi/100:2*pi;
alpha=-0.8;
beta=15;
y=sin(beta*t).*exp(alpha*t);
plot(t,y)
title('{\itAe}^{-\it\alpha\itt}sin\it\beta{\itt}\it\alpha<<\it\beta')
xlabel('时间\mus.'),
ylabel('幅值')


在title中的字符串表现的是 Aeαt sinβt α<<β,斜体Ae、上标斜体αt、斜体βt、斜体α、斜体β

希腊字母MatLab表示
α\alpha
β\beta
γ\gamma
θ\theta
Θ\Theta
Г\Gamma
δ\delta
Δ\Delta
ξ\xi
Ξ\Xi
η\elta
ε\epsilong
ζ\zeta
μ\miu
υ\nu
τ\tau
λ\lamda
Λ\Lamda
π\pi
Π\Pi
σ\sigma
Σ\Sigma
φ\phi
Φ\Phi
ψ\psi
Ψ\Psi
χ\chi
ω\ommiga
Ω\Ommiga
不等于\neq
<>\gg
正负\pm
左箭头\leftarrow
右箭头\rightarrow
上箭头\uparrow
上圆圈(度数)\circ
摄氏度\circC

10 hold on/off

hold on/off用后导致plot后最右侧和最上侧边框消失的问题:

解决:hold on后继续写box on
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  matlab plot