文章目录
- 前言
- 一、Basics
- 二、Plot from “Data”
- 三、plot()
- 四、hold on/off
- 五、Plot Style
- 六、legend() 增添图例
- 七、title() and ?label()
- 八、text() and annotation() 注释
- Exercise
- 九、Figure Adjustment
- 十、Graphical Objects
- 十一、Figure Properties
- 十二、Modifying Properties of An Object
- 十三、Identifying the Handle of An Object 标识一个对象的句柄
- 十四、Fetching or Modifying Properties
- 十五、Getting Object Properties
- 十六、Setting Axes Limits
- 十七、Setting Font (字体)and Tick of Axes
- 十八、Line Specification
- 十九、Marker Specification
- Exercise
- 二十、Multiple Figures
- 二十一、Figure Position and Size
- 二十二、Several Plots in One Figure
- 二十三、subplot()
- 二十四、Control of Grid, Box, and Axis
- 二十五、Saving Figures into Files
- 总结
前言
b站课程《MATLAB教程_台大郭彦甫(14课)》学习记录
一、Basics
MATLAB has a powerful plotting engine that can generate a wide variety of plots
二、Plot from “Data”
MATLAB does not understand functions
𝑓(𝑡) = sin(2𝜋𝑡)
Strategies:
- Generate the numeric values of a function over a specific range
在特定范围内生成函数的数值 - Display the data “points” in a graphical way
以图形方式显示数据“点”
三、plot()
plot(y) : x取1,2,3…n,n为y的长度
连续输入两个图的代码,只会显示一个图形
MATLAB在画图时会清空上一次图
四、hold on/off
Use hold on to have both plots in one figure
五、Plot Style
六、legend() 增添图例
Add legend to graph
使用:legend(‘L1’,…)
Position adjustment
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() and ?label()
title()
xlabel()
ylabel()
zlabel()
x = 0:0.1:2*pi; y1 = sin(x); y2 = exp(-x);
plot(x, y1, '--*', x, y2, ':o');
xlabel('t = 0 to 2\pi');
ylabel('values of sin(t) and e^{-x}')
title('Function Plots of sin(t) and e^{-x}');
legend('sin(t)','e^{-x}');
八、text() and annotation() 注释
Text with mathematical expression using LaTex
对代码进行解释:
0.25,2.5为text的start point,就是公式开头放在(0.25,2.5)这个点上
\int是积分符号的意思
[0.32,0.5]是指:x坐标从整个图形32%的地方到50%的地方(从左到右)
在annotation中变量取值都是在0到1之间取,因为是百分比
可以理解为整个箭头占据了一个矩形
line那里实际上是指起点(2,0)到终点(2,2^2sin(2))的线段,是向量X[x1,x2]和向量Y[y1,y2]
x = linspace(0,3); 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 $$';
text(0.25,2.5,str,'Interpreter','latex');
annotation('arrow','X',[0.32,0.5],'Y',[0.6,0.4]);
Exercise
Plot 𝑓 as a black line and 𝑔 as a series of red circles for the range t = 1 to 2 in one figure
Label each axis, and add title and legend
t = linspace(1,2);
f = t.^2;
g = sin(2*pi.*t);
plot(t, f, '-', t, g, 'o');
xlabel('Time(ms)');
ylabel('f(t)');
title('Mini Assignment #1');
legend('t^2','sin(2\pi t)');
九、Figure Adjustment
Several properties:
Font 字体
Font size
Line width
Axis limit
Tick position
Tick label
十、Graphical Objects
十一、Figure Properties
点编辑→图窗属性
十二、Modifying Properties of An Object
Strategy:
- Identify the “handle” of an object
- Fetch or modify the object’s properties
For example, to change the limits of the x-axis: - Find the handle of the x-axis
- Modify the limits
十三、Identifying the Handle of An Object 标识一个对象的句柄
h = plot(x,y)只对line做了定义
十四、Fetching or Modifying Properties
To fetch properties, use
get()
To modify properties, use
set()
十五、Getting Object Properties
Getting properties of a graphical object: get()
What do you see after running?
>> x = linspace(0, 2*pi, 1000);
y = sin(x); 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]
ColorMode: 'auto'
ContextMenu: [0×0 GraphicsPlaceholder]
CreateFcn: ''
DataTipTemplate: [1×1 matlab.graphics.datatip.DataTipTemplate]
DeleteFcn: ''
DisplayName: ''
HandleVisibility: 'on'
HitTest: on
Interruptible: on
LineJoin: 'round'
LineStyle: '-'
LineStyleMode: 'auto'
LineWidth: 0.5000
Marker: 'none'
MarkerEdgeColor: 'auto'
MarkerFaceColor: 'none'
MarkerIndices: [1×1000 uint64]
MarkerMode: 'auto'
MarkerSize: 6
Parent: [1×1 Axes]
PickableParts: 'visible'
Selected: off
SelectionHighlight: on
SeriesIndex: 1
Tag: ''
Type: 'line'
UserData: []
Visible: on
XData: [1×1000 double]
XDataMode: 'manual'
XDataSource: ''
YData: [1×1000 double]
YDataSource: ''
ZData: [1×0 double]
ZDataSource: ''
What do you see?
>> get(gca)
ALim: [0 1]
ALimMode: 'auto'
AlphaScale: 'linear'
Alphamap: [1×64 double]
AmbientLightColor: [1 1 1]
BeingDeleted: off
Box: off
BoxStyle: 'back'
BusyAction: 'queue'
ButtonDownFcn: ''
CLim: [0 1]
CLimMode: 'auto'
CameraPosition: [0.5000 0.5000 9.1603]
CameraPositionMode: 'auto'
CameraTarget: [0.5000 0.5000 0.5000]
CameraTargetMode: 'auto'
CameraUpVector: [0 1 0]
CameraUpVectorMode: 'auto'
CameraViewAngle: 6.6086
CameraViewAngleMode: 'auto'
Children: [0×0 GraphicsPlaceholder]
Clipping: on
ClippingStyle: '3dbox'
Color: [1 1 1]
ColorOrder: [7×3 double]
ColorOrderIndex: 1
ColorScale: 'linear'
Colormap: [256×3 double]
ContextMenu: [0×0 GraphicsPlaceholder]
CreateFcn: ''
CurrentPoint: [2×3 double]
DataAspectRatio: [1 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
InnerPosition: [1×4 double]
Interactions: [1×1 matlab.graphics.interaction.interface.DefaultAxesInteractionSet]
Interruptible: on
LabelFontSizeMultiplier: 1.1000
Layer: 'bottom'
Layout: [0×0 matlab.ui.layout.LayoutOptions]
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'
NextSeriesIndex: 1
OuterPosition: [0 0 1 1]
Parent: [1×1 Figure]
PickableParts: 'visible'
PlotBoxAspectRatio: [1 0.7896 0.7896]
PlotBoxAspectRatioMode: 'auto'
Position: [1×4 double]
PositionConstraint: 'outerposition'
Projection: 'orthographic'
Selected: off
SelectionHighlight: on
SortMethod: 'childorder'
Subtitle: [1×1 Text]
SubtitleFontWeight: 'normal'
Tag: ''
TickDir: 'in'
TickDirMode: 'auto'
TickLabelInterpreter: 'tex'
TickLength: [0.0100 0.0250]
TightInset: [1×4 double]
Title: [1×1 Text]
TitleFontSizeMultiplier: 1.1000
TitleFontWeight: 'normal'
TitleHorizontalAlignment: 'center'
Toolbar: [1×1 AxesToolbar]
Type: 'axes'
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 1]
XLimMode: 'auto'
XLimitMethod: 'tickaligned'
XMinorGrid: off
XMinorTick: off
XScale: 'linear'
XTick: [1×6 double]
XTickLabel: {6×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: [0 1]
YLimMode: 'auto'
YLimitMethod: 'tickaligned'
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: [0 1]
ZLimMode: 'auto'
ZLimitMethod: 'tickaligned'
ZMinorGrid: off
ZMinorTick: off
ZScale: 'linear'
ZTick: [0 0.5000 1]
ZTickLabel: ''
ZTickLabelMode: 'auto'
ZTickLabelRotation: 0
ZTickMode: 'auto'
Where do we modify the limits of the x-axis?
改变axes
十六、Setting Axes Limits
x = linspace(0, 2*pi, 1000);
y = sin(x); h = plot(x,y);
set(gca, 'XLim', [0, 2*pi]);
set(gca, 'YLim', [-1.2, 1.2]);
Alternative:
xlim([0, 2*pi]);
ylim([-1.2, 1.2]);
十七、Setting Font (字体)and Tick of Axes
set(gca, 'FontSize', 25);
set(gca, 'XTick', 0:pi/2:2*pi);
set(gca, 'XTickLabel', 0:90:360);
XTick: 坐标轴刻度间距
XTickLabel: 坐标轴刻度标记
set(gca, 'FontName', 'LaTex');
set(gca, 'XTickLabel', {'0', '\pi/2', '\pi', '3\pi/2', '2\pi'});
十八、Line Specification
Line style and width:
set(h, 'LineStyle', '-.','LineWidth', 7.0, 'Color', 'g');
Alternative:
plot(x,y, '-.g', 'LineWidth', 7.0);
Try:
delete(h);
删除了line
十九、Marker Specification
Face and edge colors of the marker
x=rand(20,1);
set(gca, 'FontSize', 18);
plot(x,'-md', 'LineWidth', 2, 'MarkerEdgeColor' , 'k' ,... 'MarkerFaceColor' , 'g', 'MarkerSize', 10); xlim([1, 20]);
Exercise
t = linspace(1,2);
f = t.^2;
g = sin(2*pi.*t);
hold on
plot(t, f, '-k', 'LineWidth',5);
plot(t, g, 'or', 'MarkerEdgeColor', 'm', 'MarkerFaceColor', '[0.4940 0.1840 0.5560]', 'Markersize', 10);
hold off
xlabel('Time(ms)');
ylabel('f(t)');
title('Mini Assignment #1');
legend('t^2','sin(2\pi t)');
xlim([1,2]);
set(gca, 'LineWidth', 2, 'FontSize', 15);
二十、Multiple Figures
Create a figure window by calling figure
图形会分别出来,而不是画在同一张上面
Be careful when using the gcf handle where there exists multiple figures
因为只会对其中一幅图进行修改,不可以同时进行
二十一、Figure Position and Size
figure('Position', [left, bottom, width, height]);
前面两个是页边距,后面两个是宽高
二十二、Several Plots in One Figure
Several small plots “in a figure”
subplot(m, n,1);
m: row n: column
二十三、subplot()
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
subplot(2, 2, 4); plot(x, y); axis equal tight
axis square 图形框为正方形
axis equal x,y轴刻度值相同
axis square tight 所画的line与坐标轴相切
二十四、Control of Grid, Box, and Axis
axis off
grid on
二十五、Saving Figures into Files
saveas(gcf,'<filename>','<formattype>');
saveas(gcf,‘learn_322’,'pdf,);
总结
继续加油吧。