使用 newplot 控制绘图
此示例演示如何将图窗和坐标区用于用户编写的绘图函数。使用圆点表示法设置属性。
使用 newplot
管理指定绘图函数输出。myPlot2D
函数:
为特定的出版要求自定义坐标区和图窗外观。
针对多线条图形循环使用线型和单一颜色。
添加具有指定显示名称的图例。
function myPlot2D(x,y) % Call newplot to get the axes handle cax = newplot; % Customize axes cax.FontName = 'Times'; cax.FontAngle = 'italic'; % Customize figure fig = cax.Parent; fig.MenuBar= 'none'; % Call plotting commands to % produce custom graph hLines = line(x,y,... 'Color',[.5,.5,.5],... 'LineWidth',2); lso = ['- ';'--';': ';'-.']; setLineStyle(hLines) grid on legend('show','Location','SouthEast') function setLineStyle(hLines) style = 1; for ii = 1:length(hLines) if style > length(lso) style = 1; end hLines(ii).LineStyle = lso(style,:); hLines(ii).DisplayName = num2str(style); style = style + 1; end end end
该图形显示 myPlot2D
函数的典型输出:
x = 1:10; y = peaks(10); myPlot2D(x,y)
myPlot2D
函数显示用户编写的绘图函数的基本结构:
由于 myPlot2D
使用 newplot
返回的句柄访问目标图窗和坐标区,该函数:
在使用各后续调用清除坐标区时遵循 MATLAB® 绘图函数的行为。
在
hold
设置为on
时,工作正常。
NextPlot
属性的默认设置确保您的绘图函数遵循标准 MATLAB 行为 - 重用图窗窗口,但不清除和重置每个新图形的坐标区。