Matlab如何绘制带箭头图形
- 带箭头坐标轴
- 带箭头曲线
- 实例演示
- 例1 带箭头曲线
- 例2 带箭头曲线+带箭头坐标轴
- 联系作者
带箭头坐标轴
前段时间推出了绘制箭头坐标轴曲线的方法,许多网友联系我索取了源程序,绘制出漂亮的图形,得到大家好评。
带箭头曲线
但是有些时候我们不光要求坐标轴是箭头形式,还要求图形上有箭头标记,前段时间绘制Nyquist图,需要在Nyquist曲线上用箭头标记频率方向,在网上几经搜寻,终于在Matlab中文论坛上找到了解决方案,绘制出的Nyquist图如下所示。
当然,arrowPlot.m还可以和箭头坐标轴结合起来,绘制出更加“高端”的曲线。
解决方案是:Matlab中文论坛网友TimeCoder编写的arrowPlot.m函数。
arrowPlot.m函数的调用格式简单、使用方便。
function H = arrowPlot(X, Y, varargin)
%ARROWPLOT Plot with arrow on the curve.
% ARROWPLOT(X, Y) plots X, Y vectors with 2 arrows directing the trend of data.
%
% You can use some options to edit the properties of arrow or curve.
% The options that you can change are as follow:
% number: The number of arrows, default number is 2;
% color: The color of arrows and curve, default color is [0, 0.447, 0.741];
% LineWidth: The line width of curve, default LineWidth is 0.5;
% scale: To scale the size of arrows, default scale is 1;
% limit: The range to plot, default limit is determined by X, Y data;
% ratio: The ratio of X-axis and Y-axis, default ratio is determined by X, Y data;
% You can use 'equal' for 'ratio', that means 'ratio' value is [1, 1, 1].
%
% Example 1:
% ---------
% t = [0:0.01:20];
% x = t.*cos(t);
% y = t.*sin(t);
% arrowPlot(x, y, 'number', 3)
%
% Example 2:
% ---------
% t = [0:0.01:20];
% x = t.*cos(t);
% y = t.*sin(t);
% arrowPlot(x, y, 'number', 5, 'color', 'r', 'LineWidth', 1, 'scale', 0.8, 'ratio', 'equal')
% Copyright 2017 TimeCoder.
实例演示
例1 带箭头曲线
% 绘制带箭头图形
t = [0:0.01:20];
x = t.*cos(t);
y = t.*sin(t);
arrowPlot(x, y, 'number', 3) % 'number', 3 表示曲线上均匀分布3个箭头标识
% 绘制带箭头图形
t = [0:0.01:10];
x = cos(t);
arrowPlot(t, x, 'number', 6)
例2 带箭头曲线+带箭头坐标轴
当然,arrowPlot.m还可以和箭头坐标轴结合起来,绘制出更加“高端”的曲线。
% 绘制带箭头图形 + 箭头坐标轴
plot_with_arrow(); % 使用plot_with_arrow绘制箭头坐标轴
hold on
t = [0:0.01:20];
x = t.*cos(t);
y = t.*sin(t);
arrowPlot(x, y, 'number', 5, 'color', 'r', 'LineWidth', 1, 'scale', 0.8, 'ratio', 'equal');
Arrow_Xlabel([],'X/m','pp'); % x轴标签
Arrow_Ylabel([],'Y/m','pp'); % y轴标签
% 绘制带箭头图形 + 箭头坐标轴
plot_with_arrow(); % 使用plot_with_arrow绘制箭头坐标轴
hold on
t = [0:0.01:20];
x = t.*cos(t);
y = t.*sin(t);
arrowPlot(x, y, 'number', 5, 'color', 'r', 'LineWidth', 1, 'scale', 0.8, 'ratio', 'equal');
Arrow_Xlabel([],'X/m','pp'); % x轴标签
Arrow_Ylabel([],'Y/m','pp'); % y轴标签
% 绘制带箭头图形 + 箭头坐标轴
plot_with_arrow([],[],[],[],'aa'); % 使用plot_with_arrow绘制箭头坐标轴
hold on
t = linspace(0,2*pi,600);
x = cos(t);
y = sin(t);
plot(x,y,'r') % 绘制直径1圆
plot(3*x,3*y,'r') % 绘制直径3圆
ind = 25 + 0:100:600;
for ii = 1:length(ind) % 循环绘制斜线
hold on
arrowPlot([3*x(ind(ii)) x(ind(ii))], [3*y(ind(ii)) y(ind(ii))], 'number', 2, 'color', 'r');
end
axis equal