欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

matplotlib quiver箭圖繪制案例

 更新時間:2020年04月17日 10:41:23   作者:落葉_小唱  
這篇文章主要介紹了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)] # 使得維數(shù)和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) # 注意參數(shù)的賦值

## 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)] # 使得維數(shù)和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) # 注意參數(shù)的賦值
show()

結果如下:

補充知識:Matlab矢量圖圖例函數(shù)quiverkey

Matlab自帶函數(shù)中不包含構造 quiver 函數(shù)注釋過程,本文參照 matplotlib 中 quiverkey 函數(shù),構造類似函數(shù)為 Matlab 中 quiver 矢量場進行標注。

quiverkey函數(shù)

首先看 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

可以看到主要參數(shù)有這么些個

quiver繪圖指針

圖例位置 X, Y

標注大小 U

標注單位字符

其他參數(shù)

1). 輸入坐標 X, Y 單位
2). (文字)標注在圖例哪個位置
3). 標注與圖例相對距離
4). 標注字體顏色

使用方法:

對應Matlab函數(shù)也應該使用這么個流程

使用quiver繪圖

將quiver返回指針與圖例位置坐標和大小等作為參數(shù)傳入

示例

[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

以上這篇matplotlib quiver箭圖繪制案例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • python 提取html文本的方法

    python 提取html文本的方法

    在解決自然語言處理問題時,有時你需要獲得大量的文本集?;ヂ?lián)網(wǎng)是文本的最大來源,但是從任意HTML頁面提取文本是一項艱巨而痛苦的任務。本文將講述python高效提取html文本的方法
    2021-05-05
  • 詳解Numpy數(shù)組轉置的三種方法T、transpose、swapaxes

    詳解Numpy數(shù)組轉置的三種方法T、transpose、swapaxes

    這篇文章主要介紹了詳解Numpy數(shù)組轉置的三種方法T、transpose、swapaxes,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-05-05
  • linux系統(tǒng)使用python獲取內(nèi)存使用信息腳本分享

    linux系統(tǒng)使用python獲取內(nèi)存使用信息腳本分享

    這篇文章主要介紹了linux系統(tǒng)使用python獲取內(nèi)存使用情況信息,大家參考使用吧
    2014-01-01
  • Python中re模塊:匹配開頭/結尾(^/$)

    Python中re模塊:匹配開頭/結尾(^/$)

    本文主要介紹了Python中re模塊:匹配開頭/結尾(^/$),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-08-08
  • 基于python的列表list和集合set操作

    基于python的列表list和集合set操作

    今天小編就為大家分享一篇基于python的列表list和集合set操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • Python曲線擬合詳解

    Python曲線擬合詳解

    這篇文章主要介紹了關于python曲線擬合,scipy.optimize中,curve_fit函數(shù)可調用非線性最小二乘法進行函數(shù)擬合,文中有詳細的代碼作為參考,需要的朋友可以閱讀參考
    2023-04-04
  • python實現(xiàn)Oracle查詢分組的方法示例

    python實現(xiàn)Oracle查詢分組的方法示例

    這篇文章主要介紹了python實現(xiàn)Oracle查詢分組的方法,結合實例形式分析了python使用group by子句及having子句實現(xiàn)Oracle查詢分組的相關操作技巧,需要的朋友可以參考下
    2020-04-04
  • tensorflow 1.0用CNN進行圖像分類

    tensorflow 1.0用CNN進行圖像分類

    這篇文章主要為大家詳細介紹了tensorflow 1.0用CNN進行圖像分類,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • 使用Python實現(xiàn)毫秒級搶單功能

    使用Python實現(xiàn)毫秒級搶單功能

    年中購物618大狂歡開始了,各大電商又開始了大力度的折扣促銷,我們的小胖又給大家謀了一波福利,淘寶APP直接搜索:小胖發(fā)福利,每天領取三次粉絲專屬現(xiàn)金大紅包。這篇文章主要介紹了用Python完成毫秒級搶單,助你秒殺淘寶大單,需要的朋友可以參考下
    2019-06-06
  • Python IDE Pycharm中的快捷鍵列表用法

    Python IDE Pycharm中的快捷鍵列表用法

    在本篇文章里小編給大家整理的是關于Python IDE Pycharm中的快捷鍵列表用法,需要的朋友們收藏下
    2019-08-08

最新評論