matplotlib quiver箭图绘制案例


Posted in Python onApril 17, 2020

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()

结果如下:

matplotlib quiver箭图绘制案例

matplotlib quiver箭图绘制案例

补充知识: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')

最终效果图

matplotlib quiver箭图绘制案例

代码

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

以上这篇matplotlib quiver箭图绘制案例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python实现将pvr格式转换成pvr.ccz的方法
Apr 28 Python
最近Python有点火? 给你7个学习它的理由!
Jun 26 Python
详解Python核心对象类型字符串
Feb 11 Python
Request的中断和ErrorHandler实例解析
Feb 12 Python
PYTHON基础-时间日期处理小结
May 05 Python
Python生成器的使用方法和示例代码
Mar 04 Python
不到40行代码用Python实现一个简单的推荐系统
May 10 Python
Win10环境python3.7安装dlib模块趟过的坑
Aug 01 Python
解决pycharm中导入自己写的.py函数出错问题
Feb 12 Python
Python运行提示缺少模块问题解决方案
Apr 02 Python
python正则表达式re.match()匹配多个字符方法的实现
Jan 27 Python
Python爬取梨视频的示例
Jan 29 Python
更新升级python和pip版本后不生效的问题解决
Apr 17 #Python
浅谈python多线程和多线程变量共享问题介绍
Apr 17 #Python
使用Matplotlib绘制不同颜色的带箭头的线实例
Apr 17 #Python
matplotlib 曲线图 和 折线图 plt.plot()实例
Apr 17 #Python
Python实现自动打开电脑应用的示例代码
Apr 17 #Python
Python matplotlib绘制图形实例(包括点,曲线,注释和箭头)
Apr 17 #Python
Python读取excel文件中带公式的值的实现
Apr 17 #Python
You might like
joomla内置的表单验证功能使用方法
2010/06/11 PHP
php switch语句多个值匹配同一代码块的实现
2014/03/03 PHP
PHP实现读取文件夹及批量重命名文件操作示例
2019/04/15 PHP
JTrackBar水平拖动效果
2007/07/15 Javascript
JS解密入门 最终变量劫持
2008/06/25 Javascript
在JavaScript中,为什么要尽可能使用局部变量?
2009/04/06 Javascript
利用js(jquery)操作Cookie的方法说明
2013/12/19 Javascript
JS和函数式语言的三特性
2014/03/05 Javascript
JavaScript实现生成GUID(全局统一标识符)
2014/09/05 Javascript
AngularJS入门教程(二):AngularJS模板
2014/12/06 Javascript
jQuery实现拖动调整表格单元格大小的代码实例
2015/01/13 Javascript
jQuery中outerHeight()方法用法实例
2015/01/19 Javascript
javascript实现支持移动设备画廊
2015/08/24 Javascript
Javascript实现Array和String互转换的方法
2015/12/21 Javascript
详解Angularjs filter过滤器
2016/02/06 Javascript
相册展示PhotoSwipe.js插件实现
2016/08/25 Javascript
学习Node.js模块机制
2016/10/17 Javascript
js 中获取制定的cook信息实现方法
2016/11/19 Javascript
详解微信第三方小程序代开发
2017/06/23 Javascript
JS实现前端页面的搜索功能
2018/06/12 Javascript
[01:03:36]Ti4 循环赛第三日DK vs Titan
2014/07/12 DOTA
[04:59]DOTA2-DPC中国联赛 正赛 Ehome vs iG 选手采访
2021/03/11 DOTA
Python最基本的输入输出详解
2015/04/25 Python
python将字符串转换成数组的方法
2015/04/29 Python
对python中的*args与**kwgs的含义与作用详解
2019/08/28 Python
opencv3/C++实现视频背景去除建模(BSM)
2019/12/11 Python
python查询MySQL将数据写入Excel
2020/10/29 Python
中国梦团日活动总结
2014/07/07 职场文书
公司总经理岗位职责范本
2014/08/15 职场文书
区域经理岗位职责
2015/02/02 职场文书
简历自我评价模板
2015/03/11 职场文书
快消品行业营销模式与盈利模式分享
2019/09/27 职场文书
pandas求平均数和中位数的方法实例
2021/08/04 Python
Python办公自动化解决world文件批量转换
2021/09/15 Python
一文了解JavaScript用Element Traversal新属性遍历子元素
2021/11/27 Javascript
Win10开机修复磁盘错误怎么跳过?Win10关闭开机磁盘检查的方法
2022/09/23 数码科技