基础绘图:一次输入两个画图指令,只能画出第二句的图形,除非执行指令之前有一句:hold on
如:
>> plot(cos(0:pi/20:2*pi));plot(sin(0:pi/20:2*pi));
>> hold on;
>> plot(cos(0:pi/20:2*pi));plot(sin(0:pi/20:2*pi));
>> hold off;
小tip:shift + enter ——换行但不执行
>> hold on;
plot(cos(0:pi/20:2*pi),'or--');
% 表示画cos的图像时用圈圈表示绘画点,红色,且用虚线连接
plot(sin(0:pi/20:2*pi),'xg:');
% 表示画sin的图像时用叉叉表示绘画点,绿色,且用点连接。
hold off;
如何给图线标识名称:
>> x = 0:0.5:4*pi;
y = sin(x);
h = cos(x);
w = 1./(1+exp(-x));
g = (1/(2*pi*2)^0.5).*exp((-1.*(x-2*pi).^2)./(2*2^2));
plot(x,y,'bd-',x,h,'gp:',x,w,'ro-',x,g,'c^-');
添加绘图右上角的标识:
legend('sin(x)','cos(x)','Sigmoid','Gauss function');
给整个图像加title 以及给轴加标识:
活学活用如下:
x = 0:0.1:2*pi;
y1 = sin(x);
y2 = exp(-x);
plot(x,y1,'--*',x,y2,':o');
xlabel('t = 0 to 2\pi');
% 注意:反斜杠+pi——表示真正的Π
ylabel('values of sin(t) and e^{-x}');
% e^{-x}——表示e的-x次方
title('Function Plots of sin(t) and e^{-x}');
legend('sin(t)','e^{-x}');
如何用MATLAB表示积分符号,并在图像中显示:
x = linspace(0, 3);
% linespace——生成线性间距向量,
% 此MATLAB函数返回包含x1(0)和x2(3)之间的100个等间距点的行向量。
y = x .^ 2 .* sin(x);
plot(x,y);
line([2,2],[0,2^2*sin(2)]);
% 用来生成图中的那条竖线。
str = '$$ \int_{0}^{2} x^2\sin(x) dx $$';
% "\int"——表示积分符号(就那个不加一横的f);
% "_{0}"——表示下标为0;"^{2}"——表示上标为2
% "x^2\sin(x)"——其中的“\sin(x)”表示转义字符,
% 加上反斜杠后sin的字体会变成正体,不加会变成斜体
text(0.25, 2.5, str, 'Interpreter', 'latex');
annotation('arrow', 'X', [0.32, 0.5], 'Y', [0.6, 0.4]);
% 这句话用来显示箭头,annotation里“arrow”即是此意。
% 后面的用来指示箭头的起止位置。起始位置:X:0.32,Y:0.6;终止位置:X:0.5,Y:0.4
% 这里的坐标是归一化的;普通坐标:(x,y);归一化坐标:(x/w,y/h)。w:图像宽,h:图像高
第四课课后练习1:
t = linspace(1,2);
f = t .^ 2; g = sin(2.*pi.*t);
plot(t, f ,'k-', t, g, 'ro');
legend('t^{2}','sin(2\pi t)');
xlabel('Time(ms)');
ylabel('f(t)');
title('Mini Assignment');
本图有两个问题:1. x轴的标度改变;2. legend 的位置如何设置。
更改:(但x轴标度问题仍然未解决)
t = 1:0.01:2;
% 换一种标度表示法即可
f = t .^ 2; g = sin(2.*pi.*t);
plot(t, f ,'k-', t, g, 'ro');
legend('t^{2}','sin(2\pi t)','Location','northwest');
% 可以加上location表示legend 出现的位置,也可以直接拖动[捂脸笑哭]
xlabel('Time(ms)');
ylabel('f(t)');
title('Mini Assignment #1');
调整图形部分:
主要策略:1.重新定义object 的handle(句柄);2.获得或修改各个object的属性。
x = linspace(0, 2*pi, 1000);
y = sin(x); plot(x, y);
h = plot(x, y);
get(h)
运行结果:
AlignVertexCenters: 'off'
Annotation: [1×1 matlab.graphics.eventdata.Annotation]
BeingDeleted: 'off'
BusyAction: 'queue'
ButtonDownFcn: ''
Children: [0×0 GraphicsPlaceholder]
Clipping: 'on'
Color: [0 0.4470 0.7410] % 颜色规则:[R G B]
CreateFcn: ''
DeleteFcn: ''
DisplayName: ''
HandleVisibility: 'on'
HitTest: 'on'
Interruptible: 'on'
LineJoin: 'round'
LineStyle: '-' % 划线形状
LineWidth: 0.5000 % 线宽度
Marker: 'none'
MarkerEdgeColor: 'auto'
MarkerFaceColor: 'none'
MarkerIndices: [1×1000 uint64]
MarkerSize: 6
Parent: [1×1 Axes]
PickableParts: 'visible'
Selected: 'off'
SelectionHighlight: 'on'
Tag: ''
Type: 'line'
UIContextMenu: [0×0 GraphicsPlaceholder]
UserData: []
Visible: 'on'
XData: [1×1000 double]
XDataMode: 'manual'
XDataSource: ''
YData: [1×1000 double]
YDataSource: ''
ZData: [1×0 double]
ZDataSource: ''
画出的图形:
现在的目的:通过修改图形的某些handle,来使图形右侧更美观。(易知需要修改axes的properity)
>> get(gca)
ALim: [0 1]
ALimMode: 'auto'
ActivePositionProperty: 'outerposition'
AlphaScale: 'linear'
Alphamap: [1×64 double]
AmbientLightColor: [1 1 1]
BeingDeleted: 'off'
Box: 'on'
BoxStyle: 'back'
BusyAction: 'queue'
ButtonDownFcn: ''
CLim: [0 1]
CLimMode: 'auto'
CameraPosition: [3.5000 0 17.3205]
CameraPositionMode: 'auto'
CameraTarget: [3.5000 0 0]
CameraTargetMode: 'auto'
CameraUpVector: [0 1 0]
CameraUpVectorMode: 'auto'
CameraViewAngle: 6.6086
CameraViewAngleMode: 'auto'
Children: [1×1 Line]
Clipping: 'on'
ClippingStyle: '3dbox'
Color: [1 1 1]
ColorOrder: [7×3 double]
ColorOrderIndex: 2
ColorScale: 'linear'
Colormap: [64×3 double]
CreateFcn: ''
CurrentPoint: [2×3 double]
DataAspectRatio: [3.5000 1 1]
DataAspectRatioMode: 'auto'
DeleteFcn: ''
FontAngle: 'normal'
FontName: 'Helvetica'
FontSize: 10
FontSizeMode: 'auto'
FontSmoothing: 'on'
FontUnits: 'points'
FontWeight: 'normal'
GridAlpha: 0.1500
GridAlphaMode: 'auto'
GridColor: [0.1500 0.1500 0.1500]
GridColorMode: 'auto'
GridLineStyle: '-'
HandleVisibility: 'on'
HitTest: 'on'
Interruptible: 'on'
LabelFontSizeMultiplier: 1.1000
Layer: 'bottom'
Legend: [0×0 GraphicsPlaceholder]
LineStyleOrder: '-'
LineStyleOrderIndex: 1
LineWidth: 0.5000
MinorGridAlpha: 0.2500
MinorGridAlphaMode: 'auto'
MinorGridColor: [0.1000 0.1000 0.1000]
MinorGridColorMode: 'auto'
MinorGridLineStyle: ':'
NextPlot: 'replace'
OuterPosition: [0 0 1 1]
Parent: [1×1 Figure]
PickableParts: 'visible'
PlotBoxAspectRatio: [1 0.7882 0.7882]
PlotBoxAspectRatioMode: 'auto'
Position: [0.1300 0.1100 0.7750 0.8150]
Projection: 'orthographic'
Selected: 'off'
SelectionHighlight: 'on'
SortMethod: 'childorder'
Tag: ''
TickDir: 'in'
TickDirMode: 'auto'
TickLabelInterpreter: 'tex'
TickLength: [0.0100 0.0250]
TightInset: [0.0510 0.0527 0.0071 0.0200]
Title: [1×1 Text]
TitleFontSizeMultiplier: 1.1000
TitleFontWeight: 'normal'
Toolbar: [1×1 AxesToolbar]
Type: 'axes'
UIContextMenu: [0×0 GraphicsPlaceholder]
Units: 'normalized'
UserData: []
View: [0 90]
Visible: 'on'
XAxis: [1×1 NumericRuler]
XAxisLocation: 'bottom'
XColor: [0.1500 0.1500 0.1500]
XColorMode: 'auto'
XDir: 'normal'
XGrid: 'off'
XLabel: [1×1 Text]
XLim: [0 7]
XLimMode: 'auto'
XMinorGrid: 'off'
XMinorTick: 'off'
XScale: 'linear'
XTick: [0 1 2 3 4 5 6 7]
XTickLabel: {8×1 cell}
XTickLabelMode: 'auto'
XTickLabelRotation: 0
XTickMode: 'auto'
YAxis: [1×1 NumericRuler]
YAxisLocation: 'left'
YColor: [0.1500 0.1500 0.1500]
YColorMode: 'auto'
YDir: 'normal'
YGrid: 'off'
YLabel: [1×1 Text]
YLim: [-1 1]
YLimMode: 'auto'
YMinorGrid: 'off'
YMinorTick: 'off'
YScale: 'linear'
YTick: [1×11 double]
YTickLabel: {11×1 cell}
YTickLabelMode: 'auto'
YTickLabelRotation: 0
YTickMode: 'auto'
ZAxis: [1×1 NumericRuler]
ZColor: [0.1500 0.1500 0.1500]
ZColorMode: 'auto'
ZDir: 'normal'
ZGrid: 'off'
ZLabel: [1×1 Text]
ZLim: [-1 1]
ZLimMode: 'auto'
ZMinorGrid: 'off'
ZMinorTick: 'off'
ZScale: 'linear'
ZTick: [-1 0 1]
ZTickLabel: ''
ZTickLabelMode: 'auto'
ZTickLabelRotation: 0
ZTickMode: 'auto'
get(h) 和 get(gca) 的异同:
h——handle of sin line; gca ——handle of axis
修改axis 的properity:
set(gca, 'XLim', [0, 2*pi]);
set(gca, 'YLim', [-1.2, 1.2]);
在原图仍然在的情况下:
图片明显被拉宽,更符合图形框,更加美观了。
执行 “xlim([0, 2*pi ]); ylim([-1.2, 1.2])” 也是同样的效果。
set(gca, 'FontSize', 25);% 更改字体
更改横轴标度:
set(gca, 'XTick', 0:pi/2:2*pi);
set(gca, 'XTickLabel', 0:90:360);
(因此练习1中的x轴标度问题可以通过这里的set gca 来解决。
将横轴标度改为用Π表示:
set(gca, 'FontName', 'LaTex');
set(gca, 'XTickLabel', {'0', '\pi/2', '\pi', '3\pi/2', '2\pi'});
更改图形的绘画方式:
set(h, 'LineStyle', '-.',...
'LineWidth', 7.0, 'Color', 'g');
也可以用:
plot(x, y, '-.g',...
'LineWidth', 7.0);
尝试delete语句:
% 尝试:
delete(h);
% 发现除了坐标轴,其他的图形部分都被删除了。
x = rand(20, 1);
set(gca, 'FontSize', 18);
% 设置字体
plot(x, '-md', 'LineWidth', 2, 'MarkerEdgeColor', 'k',...
'MarkerFaceColor', 'g', 'MarkerSize', 10);
% 设置图像的描绘图线,颜色,标注点的描绘等,
% 以及线宽,标注点边缘描色,标注点颜色,标注点大小等。
xlim([1 ,20]);
第四课课后练习2:
t = 1:0.01:2;
f = t .^ 2; g = sin(2.*pi.*t);
hold on
plot(t, f ,'k-', 'LineWidth', 2)
h = plot(t, g, 'ro');
set(h, 'MarkerFaceColor', '0.6 0.0 0.4');
set(gca, 'FontSize', 20);
legend('t^{2}','sin(2\pi t)','Location','northwest');
xlabel('Time(ms)');
ylabel('f(t)');
title('Mini Assignment #1');
hold off
。。。就这样了,做到了能做到的最好。。
Multiple Figures::
由于下例中两个图形的y轴差距非常大,因此需要将其画在两张图上,这时,可以用figure指令“召唤”出两张图。
x = -10:0.1:10;
y1 = x.^2 - 8;
y2 = exp(x);
figure, plot(x, y1);
figure, plot(x, y2);
运行结果出现两个figure:(没有从figure1开始的原因是之前那张图没关掉,figure1还在被占用)
注意:在这种情况下,要小心使用gcf 和gca 指令,因为容易混淆到底是哪一个figure。一般 gcf 和 gca 指令针对的是现在这个(当前)figure!!!(在此例中即figure3)
用figure指令还可以指定图形的位置:
在一个figure上呈现多个图形:
例:
t = 0:0.1:2*pi; x = 3*cos(t); y = sin(t);
subplot(2, 2, 1); plot(x, y); axis normal
subplot(2, 2, 2); plot(x, y); axis square
subplot(2, 2, 3); plot(x, y); axis equal
% 第三个,这个“axis equal”的图形才是真正的x、y图;表示xy轴上的单位长度一样
subplot(2, 2, 4); plot(x, y); axis equal tight
例如:在执行上面那个代码块的基础上,执行:axis off
发现最后一个图形的xy轴不见了。因此,在操纵axis时,只能操纵current axis。(关于current ,如果画完图之后不对页面进行任何操作,则current指的是最后一个line;其他情况下,鼠标选中哪一个,哪个就是current)
(或者图像figure左上角点击保存。。。。)