Plot Cheatsheet
VScode 运行 Matlab
电脑需要安装 VScode、Matlab 和 python
VScode 安装插件:MATLAB、Matlab Extension Pack、Matlab Interactive Terminal 以及 Matlab Snippets。
管理员权限打开命令行,进入 matlab (14b 以上版本) 目录:matlab安装目录\extern\engines\python
,python setup.py install
vscode 下,ctrl+shift+p
:
Open a Matlab Terminal
which opens an interactive Matlab terminal in the VS Code integrated terminal, similar to the Matlab command line
Run current Matlab Script
which runs the currently opened Matlab script and then allows the user to interact with it through the opened terminal
Run current selection in Matlab
which runs the currently selected text in a Matlab terminal. If no text is selected, the current line is run instead
Matlab 导出高 DPI 图像 可参考文章:https://zhuanlan.zhihu.com/p/65116358
文件 -> 导出设置 点击文件》导出设置》就能打开导出设置窗口
我们需要设置:
大小的单位(有时候需要自行设置宽和高),由于 dpi 是以 inch 为单位,这里将单位设置为英寸。
渲染的分辨率,设置为要求的大小。对于线图来说一般需要 600dpi:
最后点击导出,保存为需要的格式即可(包括 eps/tiff/jpeg 等)
matlab 的 print
函数 可参考:http://ww2.mathworks.cn/help/matlab/ref/print.html
1 2 3 4 img =gcf; print(img, '-dpng' , '-r600' , './img.png' ) print('-r600' ,'-dpdf' , '-fillpage' , 'sphere1.pdf' );
Matlab 3D Plot meshgrid 1 2 3 4 5 x = -2 :0.25 :2 ; y = x; [X,Y] = meshgrid(x); # 使用均匀分布的 x 坐标和 y 坐标在区间 [-2,2] 内创建二维网格。 F = X.*exp (-X.^2 -Y.^2 ); surf(X,Y,F)
slice 函数 slice 函数用于对图像进行切面,调用格式:slice(x,y,z,v,a,b,c)
,x,y,z, 作为坐标定义三维图像 v,a,b,c 作为矩阵,记录切面位置。比如 a=[1 2] 就是用 x=1,x=2 两个面去切 v。
1 2 3 4 5 6 7 8 [x,y,z] = meshgrid (-2 :.2 :2 ,-2 :.25 :2 ,-2 :.16 :2 ); v = x.*exp (-x.^2 -y.^2 -z.^2 ); xslice = [-1.2 ,.8 ,2 ]; yslice = 2 ; zslice = [-2 ,0 ]; slice(x,y,z,v,xslice,yslice,zslice) set(get(gca, 'XLabel' ), 'String' , 'X' ); set(get(gca, 'YLabel' ), 'String' , 'Y' ); set(get(gca, 'ZLabel' ), 'String' , 'Z' ); colormap hsv
3D 元胞自动机 以 2016 年美赛 A 题为例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 clc, clear; global tcell; global next_tcell; global length ; global width; global height; global sum_cell; global tcell_type;tcell_type = zeros (length , width, height); dis = 1 ; time = 1500 ; length = 40 ; width = 60 ; height = 20 ;[x, y, z] = meshgrid (1 :dis:length + 1 , 1 :dis:width + 1 , 1 :dis:height + 1 ); [m, n, p] = size (x); length = m - 1 ; width = n - 1 ; height = p - 1 ;sum_cell = length * width * height; length = m; width = n; height = p;min_temperature = 20 ; max_temperature = 45 ; t = 35 ; avg_t = zeros (t, 1 ); tcell = zeros (length , width, height); for a = 1 :length for b = 1 :width for c = 1 :height tcell(a, b, c) = randi([20 45 ]); end end end next_tcell = tcell; for i = 1 :t fprintf('第%d时刻\n' , i ); slice(x, y, z, tcell, [3 ], [], [], 'cubic' ); axis equal; view(90 , 10 ); shading flat; caxis([min_temperature max_temperature]); colormap jet colorbar drawnow; for x1 = 2 :length - 1 for y1 = 2 :width - 1 for z1 = 2 :height - 1 end end end tcell = next_tcell; avg_t(i ) = 1 ; end