matplotlib quiver箭图绘制案例
作者:落叶_小唱 发布时间:2023-07-13 10:04:11
标签:matplotlib,quiver,箭图
quiver绘制表示梯度变化非常有用,下面是学习过程中给出的两个例子,可以很好理解quiver的用法
from pylab import *
close()
## example 1
x = linspace(0,10,40)
y = x**2*exp(-x)
u = array([x[i+1]-x[i] for i in range(len(x)-1)])
v = array([y[i+1]-y[i] for i in range(len(x)-1)])
x = x[:len(u)] # 使得维数和u,v一致
y = y[:len(v)]
c = randn(len(u)) # arrow颜色
figure()
quiver(x,y,u,v,c, angles='xy', scale_units='xy', scale=1) # 注意参数的赋值
## example 2
x = linspace(0,20,30)
y = sin(x)
u = array([x[i+1]-x[i] for i in range(len(x)-1)])
v = array([y[i+1]-y[i] for i in range(len(x)-1)])
x = x[:len(u)] # 使得维数和u,v一致
y = y[:len(v)]
c = randn(len(u)) # arrow颜色
figure()
quiver(x,y,u,v,c, angles='xy', scale_units='xy', scale=1) # 注意参数的赋值
show()
结果如下:
补充知识:Matlab矢量图图例函数quiverkey
Matlab自带函数中不包含构造 quiver 函数注释过程,本文参照 matplotlib 中 quiverkey 函数,构造类似函数为 Matlab 中 quiver 矢量场进行标注。
quiverkey函数
首先看 matplotlib 中 quiverkey 如何定义的
quiverkey(*args, **kw)
Add a key to a quiver plot.
Call signature::
quiverkey(Q, X, Y, U, label, **kw)
Arguments:
*Q*:
The Quiver instance returned by a call to quiver.
*X*, *Y*:
The location of the key; additional explanation follows.
*U*:
The length of the key
*label*:
A string with the length and units of the key
Keyword arguments:
*coordinates* = [ 'axes' | 'figure' | 'data' | 'inches' ]
Coordinate system and units for *X*, *Y*: 'axes' and 'figure' are
normalized coordinate systems with 0,0 in the lower left and 1,1
in the upper right; 'data' are the axes data coordinates (used for
the locations of the vectors in the quiver plot itself); 'inches'
is position in the figure in inches, with 0,0 at the lower left
corner.
*color*:
overrides face and edge colors from *Q*.
*labelpos* = [ 'N' | 'S' | 'E' | 'W' ]
Position the label above, below, to the right, to the left of the
arrow, respectively.
*labelsep*:
Distance in inches between the arrow and the label. Default is
0.1
*labelcolor*:
defaults to default :class:`~matplotlib.text.Text` color.
*fontproperties*:
A dictionary with keyword arguments accepted by the
:class:`~matplotlib.font_manager.FontProperties` initializer:
*family*, *style*, *variant*, *size*, *weight*
Any additional keyword arguments are used to override vector
properties taken from *Q*.
The positioning of the key depends on *X*, *Y*, *coordinates*, and
*labelpos*. If *labelpos* is 'N' or 'S', *X*, *Y* give the position
of the middle of the key arrow. If *labelpos* is 'E', *X*, *Y*
positions the head, and if *labelpos* is 'W', *X*, *Y* positions the
tail; in either of these two cases, *X*, *Y* is somewhere in the
middle of the arrow+label key object.
Additional kwargs: hold = [True|False] overrides default hold state
可以看到主要参数有这么些个
quiver绘图指针
图例位置 X, Y
标注大小 U
标注单位字符
其他参数
1). 输入坐标 X, Y 单位
2). (文字)标注在图例哪个位置
3). 标注与图例相对距离
4). 标注字体颜色
使用方法:
对应Matlab函数也应该使用这么个流程
使用quiver绘图
将quiver返回指针与图例位置坐标和大小等作为参数传入
示例
[x,y] = meshgrid(0:0.2:2,0:0.2:2);
u = cos(x).*y;
v = sin(x).*y;
figure;
Qh = quiver(x,y,u,v);
quiverkey(Qh, 0.5, 2.5, 1, 'm/s', 'Color', 'r', 'Coordinates', 'data')
最终效果图
代码
function Q = quiverkey(Q, X, Y, U, label, varargin)
%QUIVERKEY legend for quiver
%
% QUIVERKEY(Q, X, Y, U, label)
%
% Arguments:
% Q : The quiver handle returned by a call to quiver
% X,Y : The location of the legend
% U : The unit length. If U<0, the arrow will be reversed
% label : The string with the length and units of the key
%
% Addition arguments:
% Coordinates = [ 'axes' | 'data'(default) ]
%
% 'axes' & 'figure' : 'axes' and 'figure' are normalized
% coordinate systems with 0,0 in the lower left
% and 1,1 in the upper right;
% 'data' : use the axes data coordinates
%
% LabelDistance : Distance in 'coordinates' between the arrow and the
% label. Deauft is 0.1 (units 'axes').
%
% Color : overrides face and edge colors from Q.
%
% LabelPosition = [ 'N' | 'S'(default) | 'E' | 'W' ]
%
% Position the label above, below, to the right,
% to the left of the arrow, respectively.
%
% LabelColor : defaults to black
%
% Examples:
%
% [x,y] = meshgrid(0:0.2:2,0:0.2:2);
% u = cos(x).*y;
% v = sin(x).*y;
% figure; Qh = quiver(x,y,u,v);
% quiverkey(Qh, 0.5, 2.5, 1, 'm/s', 'Color', 'r', 'Coordinates', 'data')
%
% Author:
% li12242 - Department of Civil Engineering in Tianjin University
% Email:
% li12242@tju.edu.cn
%
%% get input argument
if nargin < 5
error('Input arguments" Number incorrect!')
end
if isempty(varargin) && mod(length(varargin), 2) ~= 0
error('Input arguments donot pairs!')
else
[CoorUnit, LabelDist, Color, LabelPosition, LabelColor] = getInput(varargin);
end
%% add legend arrow
% get original data
xData = get(Q, 'XData'); yData = get(Q, 'YData');
uData = get(Q, 'UData'); vData = get(Q, 'VData');
% get axes properties
haxes = get(Q, 'Parent');
xLim = get(haxes, 'XLim'); yLim = get(haxes, 'YLim');
NextPlot = get(haxes, 'NextPlot');
% set axes properties
set(haxes, 'NextPlot', 'add')
if strcmp(CoorUnit, 'axes')
% position of legend arrow
xa = xLim(1) + X*(xLim(2) - xLim(1));
ya = yLim(1) + Y*(yLim(2) - yLim(1));
else
xa = X; ya = Y;
end
% add legend arrow into data vector
xData = [xData(:); xa]; yData = [yData(:); ya];
uData = [uData(:); U]; vData = [vData(:); 0];
% reset data
set(Q, 'XData', xData, 'YData', yData, 'UData', uData, 'VData', vData);
set(Q, 'Color', Color)
%% add text
dx = LabelDist*(xLim(2) - xLim(1));
dy = LabelDist*(yLim(2) - yLim(1));
% set position of label
switch LabelPosition
case 'N'
xl = xa; yl = ya + dy;
case 'S'
xl = xa; yl = ya - dy;
case 'E'
xl = xa + dx; yl = ya;
case 'W'
xl = xa - dx; yl = ya;
end% switch
th = text(xl, yl, [num2str(U), ' ', label]);
set(th, 'Color', LabelColor);
% turn axes properties to original
set(haxes, 'NextPlot', NextPlot)
end% func
%% sub function
function [CoorUnit, LabelDist, Color, LabelPosition, LabelColor] = getInput(varcell)
% Input:
% varcell - cell variable
% Output:
%
nargin = numel(varcell);
%% set default arguments
CoorUnit = 'data';
LabelDist = 0.05; % units 'axes'
Color = 'k';
LabelPosition = 'S';
LabelColor = 'k';
%% get input arguments
contour = 1;
while contour < nargin
switch varcell{contour}
case 'Coordinates'
CoorUnit = varcell{contour+ 1};
case 'LabelDistance'
LabelDist = varcell{contour+ 1};
case 'Color'
Color = varcell{contour+ 1};
case 'LabelPosition'
LabelPosition = varcell{contour+ 1};
case 'LabelColor'
LabelColor = varcell{contour+ 1};
otherwise
error('Unknown input argument.')
end% switch
contour = contour + 2;
end% while
end% fun
来源:https://blog.csdn.net/ouening/article/details/80712111
0
投稿
猜你喜欢
- 一、pexpect模块介绍Pexpect使Python成为控制其他应用程序的更好工具。可以理解为Linux下的expect的Python封装
- Thinkphp6的日志问题日志级别debug, info, notice, warning, error, critical, alert
- 大家做网站,特别是自己写的代码,常常担心被一些黑客入侵服务器,从而导致网站代码被盗,给自己带来一些损失。那么我们怎么样做,就算黑客盗了你的代
- 这是一个由加油站油罐传感器测量的油罐高度数据和出油体积,根据体积和高度的倒数,用截面积来描述油罐形状,求出拟合曲线,再用标准数据,求积分来验
- 如下所示:#!/usr/bin/env pythonimport osimport sysclass CConsole: M_MAP_COL
- 一个日期联动选择器javascript源码,年月日联动显示,准确显示日期(包括闰年日期),可自定义日期范围。 【select】 先说清空一个
- 本文实例讲述了php自定义函数实现二维数组按指定key排序的方法。分享给大家供大家参考,具体如下:二维数组官方的排序方法并不好,该函数可以进
- 步骤:1. 掌握几种对象及其关系2. 了解每类对象的基本操作方法3. 通过转化关系转化涉及对象1. datetime>>>
- 对于更完整的代码可以参考,这个是支持数据库的版本。经过测试Asp+Ajax仿google搜索提示效果 数据库版google搜索提示.rar
- 1.什么是局部视图局部视图是在其他视图中呈现的视图。通过执行局部视图生成的HTML输出呈现在调用视图中。与视图一样,局部视图使用 .csht
- 数据列类型与查询效率选用适当的数据列类型有助于提高查询命令的执行速度,下面是几点关于如何选择合适数据列类型的建议:尽量选用尺寸较小的数据列。
- 在asp中利用excel的一个方法是将excel文件作为一个数据库进行链接,然后的操作和对access数据库操作类似。但是这个方法不是总能有
- 使用cookie来判断来访者身份,是否是首次登陆, asp代码实例如下:< %@ LANGUAGE=&q
- 在CSS森林群里讨论一个margin的问题中无意间发现overflow也可以用来清除浮动,嘿嘿,这个方法不单使用简单,而且FF、OP、IE7
- Python编程语言判断是否是目录在Python编程语言中可以使用os.path.isdir()函数判断某一路径是否为目录。其函数原型如下所
- 如下所示:a, b, c = 1, 2, 3 # 1.常规 if a>b: &nbs
- 这几天做了一个专题,放到服务器后发现从首页链接到专题页面正常,但是从专题页面跳转到首页就会出现乱码。很是蹊跷,专题页面和首页没有共同的文件,
- 在Google Chrome浏览器出来之前,我一直使用FireFox,因为FireFox的插件非常丰富,更因为FireFox有强大的Fire
- SvnPeer.php <?php /** * * This class for execute the external progr
- 创建时间序列函数pd.date_range()根据指定的范围,生成时间序列DatetimeIndex,每隔元素的类型为Timestamp。该