Skip to content

Commit

Permalink
Common plots example
Browse files Browse the repository at this point in the history
  • Loading branch information
qchen-fdii-cardc committed Oct 28, 2024
1 parent 974bc96 commit cd7c952
Show file tree
Hide file tree
Showing 4 changed files with 310 additions and 2 deletions.
165 changes: 165 additions & 0 deletions content/posts/matlab/common-plots.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,171 @@ tocBorder = true

详细介绍每个函数感觉必要性不大,还不如看一些通用的概念,能够更好应对实际应用中可能出现的状况。


这有一个小例子,可以展示出了极坐标之外的所有几种常规图形的绘制。可以通过按钮和菜单选择相应的函数,查看相应的图形。

[代码文件下载:plotExample](/matlab-code/common2DPlots.m)

这里把源文件内容也列在这里:

```matlab
function f = common2DPlots
f = uifigure(Name='Common 2D Plots', Visible='off', WindowState='minimized');
movegui(f, 'center');
g = uigridlayout(f, [1, 1]);
ax = uiaxes(g, Visible='off');
ax.Layout.Row = 1;
ax.Layout.Column = 1;
menu = uimenu(f, Text="Demos");
tb = uitoolbar(f);
texts = {...
"Line Plots", ...
"Multiple Lines Plots", ...
"Bar Plots", ...
"Stairstep Plots", ...
"Errorbar Plots", ...
"Stem Plots", ...
"Scatter Plots", ...
"Scatter Colorbar Plots", ...
};
fcns = {...
@linePlotFcn, ...
@multipleLinePlotFcn, ...
@barPlot,...
@stairStepPlot, ...
@errorBarPlot, ...
@stemPlot, ...
@scatterPlot, ...
@scatterColorPlot, ...
};
n = numel(texts);
for idx = 1:n
fn = matlab.lang.makeValidName(texts{idx}) + ".png";
if ~exist(fn, 'file')
feval(fcns{idx}, ax);
exportgraphics(ax, fn, Resolution=10);
end
cb = makeCallback(fcns{idx}, ax, texts{idx});
uimenu(menu, Text=texts{idx}, ...
MenuSelectedFcn=cb);
uipushtool(tb, Tooltip=texts{idx}, ...
Icon=fn, ...
ClickedCallback=cb)
end
uimenu(menu, Text="Quit", ...
Accelerator="Q", ...
Separator='on', ...
MenuSelectedFcn=@(~, ~)close(f));
clearAll(ax);
f.WindowState = "normal";
f.Visible = 'on';
end
function fh = makeCallback(func, ax_handle, textLabel)
function retCb(~, ~)
clearAll(ax_handle);
feval(func, ax_handle);
ax_handle.Title.String = textLabel;
ax_handle.Title.FontWeight = 'bold';
ax_handle.Title.FontSize = 24;
ax_handle.Visible = 'on';
end
fh = @retCb;
end
function clearAll(ax_handle)
colorbar(ax_handle, 'off');
cla(ax_handle);
end
function scatterPlot(ax_handle)
load patients Height Weight Systolic
scatter(ax_handle, Height,Weight)
xlabel(ax_handle,'Height')
ylabel(ax_handle,'Weight')
end
function scatterColorPlot(ax_handle)
load patients Height Weight Systolic
scatter(ax_handle, Height,Weight, 20,Systolic)
xlabel(ax_handle,'Height')
ylabel(ax_handle,'Weight')
colorbar(ax_handle);
end
function stemPlot(ax_handle)
x = 0:0.1:4;
y = sin(x.^2) .* exp(-x);
stem(ax_handle, x, y);
end
function polarPlot(ax_handle)
clearAll(ax_handle);
theta = 0:0.01:2*pi;
rho = abs(sin(2*theta) .* cos(2*theta));
polarplot(ax_handle, theta, rho);
end
function errorBarPlot(ax_handle)
x = -2:0.1:2;
y = erf(x);
eb = rand(size(x)) / 7;
errorbar(ax_handle, x, y, eb);
end
function stairStepPlot(ax_handle)
x = 0:0.25:10;
y = sin(x);
stairs(ax_handle, x, y);
end
function barPlot(ax_handle)
x = -2.9:0.2:2.9;
y = exp(-x .* x);
bar(ax_handle, x, y);
end
function linePlotFcn(ax_handle)
x = 0:0.05:5;
y = sin(x.^2);
plot(ax_handle, x, y);
end
function multipleLinePlotFcn(ax_handle)
x = 0:0.05:5;
y1 = sin(x.^2);
y2 = cos(x.^2);
plot(ax_handle, x, y1, x, y2);
end
```

而极坐标图,可以通过`polarplot`函数来绘制。

```matlab
theta = 0:0.01:2*pi;
rho = abs(sin(2*theta).*cos(2*theta));
polarplot(theta,rho)
```

因为极坐标图对应的坐标是`PolarAxes`对象,跟上面这个小小的例子不太容易搞到一起,所以这里就不放在这个例子里面了。

![poloarplot](/matlab-img/polarplot.png)

## 图形窗口和坐标系

首先对于一个图形,最基本的两个概念就是:
Expand Down
4 changes: 2 additions & 2 deletions content/posts/matlab/multiple_subplot.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ title('4');



简单的说,子图的编号采取了行先的方式,下面给出一个循环的例子,可以看到,图号按照`n * i + j`的方式编号,其中`n`是行数,`i`是行号,`j`是列号。
简单的说,子图的编号采取了行先的方式,下面给出一个循环的例子,可以看到,图号按照`n * (i-1) + j`的方式编号,其中`n`是行数,`i`是行号,`j`是列号。


```matlab
Expand Down Expand Up @@ -181,7 +181,7 @@ title('3');

### 直接调整子图坐标系

那这样一想,我们能不能先分快创建子图坐标系,然后在调整`Position`属性来调整子图的位置呢?当然可以,下面的例子就是这样做的。
那这样一想,我们能不能先分块创建子图坐标系,然后在调整`Position`属性来调整子图的位置呢?当然可以,下面的例子就是这样做的。


```matlab
Expand Down
143 changes: 143 additions & 0 deletions static/matlab-code/common2DPlots.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
function f = common2DPlots
f = uifigure(Name='Common 2D Plots', Visible='off', WindowState='minimized');
movegui(f, 'center');
g = uigridlayout(f, [1, 1]);
ax = uiaxes(g, Visible='off');
ax.Layout.Row = 1;
ax.Layout.Column = 1;

menu = uimenu(f, Text="Demos");
tb = uitoolbar(f);

texts = {...
"Line Plots", ...
"Multiple Lines Plots", ...
"Bar Plots", ...
"Stairstep Plots", ...
"Errorbar Plots", ...
"Stem Plots", ...
"Scatter Plots", ...
"Scatter Colorbar Plots", ...
};
fcns = {...
@linePlotFcn, ...
@multipleLinePlotFcn, ...
@barPlot,...
@stairStepPlot, ...
@errorBarPlot, ...
@stemPlot, ...
@scatterPlot, ...
@scatterColorPlot, ...
};
n = numel(texts);

for idx = 1:n
fn = matlab.lang.makeValidName(texts{idx}) + ".png";

if ~exist(fn, 'file')
feval(fcns{idx}, ax);
exportgraphics(ax, fn, Resolution=10);
end

cb = makeCallback(fcns{idx}, ax, texts{idx});

uimenu(menu, Text=texts{idx}, ...
MenuSelectedFcn=cb);
uipushtool(tb, Tooltip=texts{idx}, ...
Icon=fn, ...
ClickedCallback=cb)
end

uimenu(menu, Text="Quit", ...
Accelerator="Q", ...
Separator='on', ...
MenuSelectedFcn=@(~, ~)close(f));


clearAll(ax);

f.WindowState = "normal";
f.Visible = 'on';

end

function fh = makeCallback(func, ax_handle, textLabel)
function retCb(~, ~)
clearAll(ax_handle);
feval(func, ax_handle);
ax_handle.Title.String = textLabel;
ax_handle.Title.FontWeight = 'bold';
ax_handle.Title.FontSize = 24;
ax_handle.Visible = 'on';
end
fh = @retCb;
end


function clearAll(ax_handle)
colorbar(ax_handle, 'off');
cla(ax_handle);
end

function scatterPlot(ax_handle)
load patients Height Weight Systolic
scatter(ax_handle, Height,Weight)
xlabel(ax_handle,'Height')
ylabel(ax_handle,'Weight')
end

function scatterColorPlot(ax_handle)
load patients Height Weight Systolic
scatter(ax_handle, Height,Weight, 20,Systolic)
xlabel(ax_handle,'Height')
ylabel(ax_handle,'Weight')
colorbar(ax_handle);
end


function stemPlot(ax_handle)
x = 0:0.1:4;
y = sin(x.^2) .* exp(-x);
stem(ax_handle, x, y);
end

function polarPlot(ax_handle)
clearAll(ax_handle);
theta = 0:0.01:2*pi;
rho = abs(sin(2*theta) .* cos(2*theta));
polarplot(ax_handle, theta, rho);
end

function errorBarPlot(ax_handle)
x = -2:0.1:2;
y = erf(x);
eb = rand(size(x)) / 7;
errorbar(ax_handle, x, y, eb);
end


function stairStepPlot(ax_handle)
x = 0:0.25:10;
y = sin(x);
stairs(ax_handle, x, y);
end

function barPlot(ax_handle)
x = -2.9:0.2:2.9;
y = exp(-x .* x);
bar(ax_handle, x, y);
end


function linePlotFcn(ax_handle)
x = 0:0.05:5;
y = sin(x.^2);
plot(ax_handle, x, y);
end

function multipleLinePlotFcn(ax_handle)
x = 0:0.05:5;
y1 = sin(x.^2);
y2 = cos(x.^2);
plot(ax_handle, x, y1, x, y2);
end
Binary file added static/matlab-img/polarplot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit cd7c952

Please sign in to comment.