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


猜你喜欢
- 本文实例讲述了Python基于列表list实现的CRUD操作功能。分享给大家供大家参考,具体如下:本篇文章看之前你的先了解python 基础
- 记录遇到的问题;在aliyun上安装MySQL时由于上次错误卸载mysql 导致校验文件出问题;处理方式有几种1到mysql官网下载校验文件
- pytest是python语言中一款强大的单元测试框架,用来管理和组织测试用例,可应用在单元测试、自动化测试工作中。unittest也是py
- 1. RS.OPEN SQL,CONN,A,B,C2. CONN.EXECUTE(SQL,RowsAffected,C)参数含义:SQL的值
- 导言:在前面2节教程,我们探讨了如何使用FileUpload控件从客户端向服务器上传文件,以及如何在数据Web控件里显示二进制数据。在本节,
- 一、前言本来写了脚本用于暴力破解密码,可是1秒钟尝试一个密码2220000个密码我的天,想用多线程可是只会一个for全开,难道开222000
- 以前工作中需要全新的Access数据库,可以复制数据库,也可以把新的数据库放到资源里面,用新数据库的时候释放出来,都感觉不爽,还是动态生成心
- vue-cli 环境变量 process.env使用参考官网: https://cli.vuejs.org/zh/gu
- input()作用:让用户从控制台输入一串字符,按下回车后结束输入,并返回字符串注意:很多初学者以为它可以返回数字,其实是错的!>&g
- 继续pygame实现俄罗斯方块游戏(AI篇1)的代码更新一、消除后才做评价上一篇我们是对方块落下的位置和落下后出来的空洞进行了评价,但是这些
- 1.PyQtGraph简介:pyqtgraph的主要用途:1、为数据、绘图、视频等提供快速、可交互图形显示。2、提供快速开发应用的工具。2.
- 在自动化测试脚本的运行过程中,可以通过设置等待的方式来避免由于网络延迟或浏览器卡顿导致的偶然失败,常用的等待方式有三种:一、固定等待(tim
- 我们用下了asp代码简单统计了下载一个文件需要的时间:<%Function DownloadTime(intFileSize
- Logo是品牌图形区别的点睛之处,我们每天都要接触很多logo - 在高速公路上,在购买商品时,以及浏览各种网站。我们查看很多logo设计,
- 一.简介发展由来:随着信息技术的发展和硬件设备成本的降低,当今的互联网存在海量的数据,要想快速从这些数据中获取更多有效的信息,数据可视化是重
- with/as使用open打开过文件的对with/as都已经非常熟悉,其实with/as是对try/finally的一种替代方案。当某个对象
- 图片缩放会失真是真理,在浏览器里也一样,貌似使用传说中的双三次插值可以让失真看起来比较不明显,但是真的想不通IE7已经实现了,却不默认打开,
- 添加字段的语法:alter table tablename add (column datatype [default value][nul
- 完美的渐变透明效果。支持IE,Firefox渐变,自己写的JS框架中用的东西,发出来了。修正完全隐藏时,偶尔不display = "
- 定义一个变量,直接输出会输出变量的属性,并不能输出变量值。那么怎么输出变量值呢?请看下面得意import tensorflow as tf