1. Matlab初阶绘图
函数 | 参数 | 示例 |
---|---|---|
plot(x,y),plot(y) | 向量对,不写x默认1:length(y) | |
legend(‘y1’, ‘y2’, ‘y3’) | 函数名字符串 | 图例,和plot的几个向量对 对应 |
title(), x/y/zlabel() | 标题、坐标轴名字符串 | 标题, |
text() | 基于latex做数学公式的text | |
annotation() | 画箭头 |
一些控制
grid on/off | |
box on/off | |
axis on/off | |
axis normal/square/equal/equal tight… | 对当前图的操作 |
hold on/off |
plot的风格
doc LineSpec
plot(x, y, 'str') % str定义不同风格
-
图表
一个图表含有的object
图表、坐标轴、线、文本…
取得这些object
- 坐标轴相关
x = linspace(0, 2*pi, 1000) y = sin(x) h = plot(x,y) % line的handle为h,axes和figure默认的handle分别为gca,gcf get(h) % 获取line的属性 set(h) % 设置line的属性 set(gca, 'XLim', []) % 设置axis的范围 set(gca, 'YLim', []) % 设置ayis的范围 % 可替代的方式 xlim([ ]) ylin([ ]) set(gca, 'FontSize', 25) % 设置字体大小
-
线相关
set(h, 'LineStyle', '-.', 'LineWidth', 7.0, 'Color', 'g') % 可替代的方式 plot(x,y, '-.g', 'LineWidth', 7.0)
-
Marker,标记点
face & edge 的颜色
plot(x,'-md','LineWidth', 2, 'MarkerEdgeColor','k', 'MarkerFaceColor', 'g', 'MarkerSize', 10)
-
图表相关
% 多个plot,多个figure figure('Position', [left, bottom, width, height])% 当存在多图表时候,谨慎使用gcf,gcf表示现在的图表 % 1个figure,多个plot subplot(m, n, 1) %m行,n列, 第一个1位置 plot(x,y) axis normal/square/equal/equal tight % 4种坐标轴形状 subplot(m, n, 2) % 第2个位置
-
保存图片
saveas(gcf, '<filename>', '<formattype>')
formattype jpeg png tiff pdf eps meta
-
Matlab进阶绘图
特殊的plots
logspace(start, end, num) loglog 加上网格,便于观察 semilogx set(gca, ‘XGid’, ‘on’) semilogy plotyy(x,y1, x, y2) 2条y轴 统计图表 hist(y, num of bins) 柱状图,整体,统计图表 bar(x) bar(matrix) bar3(matrix) bar(y, ‘stacked’) barh(y) 柱状图,当为矩阵时候,根据行分为rows组(二维左右,三维并排),添加stacked是将每组数据堆放 pie(x, [0,0,0,1]) pie3 饼图,[ ]里按x小到大的顺序,为1则扇形分开 polar 极坐标 stairs 楼梯 stem 柱子 boxplot
[AX,H1,H2] = plotyy(x, y1,x, y2) % 获取axes,line1,line2的handle
set(get(AX(1),'Ylabel'), 'String', 'Left Y-axis')
set(get(AX(2),'Ylabel'), 'String', 'Left Y-axis')
set(H1, 'LineStype', '--')
set(H2, 'linestyle', ':')
画图 | |
---|---|
fill() | |
配色 | P6-33:00 |
imagesc() | 用颜色代表第三个维度 |
colorbar | |
colormap() | 色系 |
3D 画图 | |
---|---|
plot3(x,y,z) | |
surf | 基本曲面(第三维是若干个平面) |
meshgird(x,y) | |
surfc | |
surface | 基本曲面(第三维是若干个平面) |
mesh | 网格曲面 |
meshc | 曲面+等高线 |
contour | 等高线 |
contourf | |
view() | |
light() | |
patch() |
[X,Y] = meshgrid(x,y)
Z = X.exp(-X.^2-Y.^2)
mesh(X,Y,Z)
surf(X,Y,Z)