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解析html开发库pyquery使用方法
Feb 07 Python
Python中的引用和拷贝浅析
Nov 22 Python
利用python如何处理nc数据详解
May 23 Python
在NumPy中创建空数组/矩阵的方法
Jun 15 Python
Python使用logging模块实现打印log到指定文件的方法
Sep 05 Python
对python 中class与变量的使用方法详解
Jun 26 Python
Python+Pyqt实现简单GUI电子时钟
Feb 22 Python
python实现批处理文件
Jul 28 Python
python利用线程实现多任务
Sep 18 Python
python实现PolynomialFeatures多项式的方法
Jan 06 Python
python实现Thrift服务端的方法
Apr 20 Python
Python3.8官网文档之类的基础语法阅读
Sep 04 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
PHP学习资料汇总与网址
2007/03/16 PHP
对squid中refresh_pattern的一些理解和建议
2009/04/17 PHP
[原创]PHP正则删除html代码中a标签并保留标签内容的方法
2017/05/23 PHP
PHP实践教程之过滤、验证、转义与密码详解
2017/07/24 PHP
php实现的后台表格分页功能示例
2017/10/23 PHP
PHP crypt()函数的用法讲解
2019/02/15 PHP
javascript+xml技术实现分页浏览
2008/07/27 Javascript
使用documentElement正确取得当前可见区域的大小
2014/07/25 Javascript
JS实现带有3D立体感的银灰色竖排折叠菜单代码
2015/10/20 Javascript
bootstrap为水平排列的表单和内联表单设置可选的图标
2017/02/15 Javascript
ES6新特性之数组、Math和扩展操作符用法示例
2017/04/01 Javascript
详解Angular 4.x NgIf 的用法
2017/05/22 Javascript
vue.js全局API之nextTick全面解析
2017/07/07 Javascript
js中变量的连续赋值(实例讲解)
2017/07/08 Javascript
基于React Native 0.52实现轮播图效果
2020/08/25 Javascript
vue filter 完美时间日期格式的代码
2019/08/14 Javascript
vue.js this.$router.push获取不到params参数问题
2020/03/03 Javascript
[07:57]DOTA2热力大趴狂欢夜 广州站活动回顾
2013/11/27 DOTA
[33:42]LGD vs OG 2018国际邀请赛小组赛BO2 第一场 8.16
2018/08/17 DOTA
详解Python的单元测试
2015/04/28 Python
python使用arcpy.mapping模块批量出图
2017/03/06 Python
Bottle框架中的装饰器类和描述符应用详解
2017/10/28 Python
python 实现数组list 添加、修改、删除的方法
2018/04/04 Python
彻底理解Python中的yield关键字
2019/04/01 Python
python 函数嵌套及多函数共同运行知识点讲解
2020/03/03 Python
Python实现在Windows平台修改文件属性
2020/03/05 Python
Python如何telnet到网络设备
2021/02/18 Python
eVitamins日本:在线购买折扣维生素、补品和草药
2019/04/04 全球购物
GAZMAN官网:澳大利亚领先的男装品牌
2019/12/19 全球购物
怎样在程序里获得一个空指针
2015/01/24 面试题
医校毕业生自我鉴定
2014/01/25 职场文书
中秋寄语大全
2014/04/11 职场文书
大学生求职信范文
2014/05/24 职场文书
计划生育证明书写要求
2014/09/17 职场文书
2016年小学生新年寄语
2015/08/18 职场文书
Win11自动黑屏怎么办 Win11自动黑屏设置教程
2022/07/15 数码科技