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

可以看到主要参数有这么些个

  1. quiver绘图指针
  2. 图例位置 X, Y
  3. 标注大小 U
  4. 标注单位字符
  5. 其他参数

    1). 输入坐标 X, Y 单位

    2). (文字)标注在图例哪个位置

    3). 标注与图例相对距离

    4). 标注字体颜色

使用方法:

对应Matlab函数也应该使用这么个流程

  1. 使用quiver绘图
  2. 将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

Matlab矢量图图例函数quiverkey的更多相关文章

  1. matlab 矢量化编程(四)—— 标量函数转化为能够处理矢量的函数

    1. 组合的矢量实现 nchoosek(n, k) 的第二个参数在 matlab 下是不支持矢量化的,必须是标量形式.但 matlab 下的 gamma 函数,却可支持,矢量形式,又因为,gamma ...

  2. matlab中help所有函数功能的英文翻译

    doc funname 在帮助浏览器中打开帮助文档 help funname 在命令窗口打开帮助文档 helpbrowser 直接打开帮助浏览器 lookfor funname 搜索某个关键字相关函数 ...

  3. C# 导出一个控件的矢量图

    调用Control.DrawToBitmap(Bitmap) 方法是很容易得到控件的图形的. 但是bitmap是栅格化图形.栅格化图形有很多缺点,比如文件体积比较大. 放大后失真. 不易编辑等等. 这 ...

  4. matlab 全部的随机数函数

    matlab 全部的随机数函数 (一)Matlab内部函数 a. 基本随机数 Matlab中有两个最基本生成随机数的函数. 1.rand() 生成(0,1)区间上均匀分布的随机变量.基本语法: ran ...

  5. Android 开发 VectorDrawable 矢量图 (三)矢量图动画

    VectorDrawable 矢量图 三部曲: Android 开发 VectorDrawable 矢量图 (一)了解Android矢量图与获取矢量图 Android 开发 VectorDrawabl ...

  6. D3.js (v3)+react框架 基础部分之认识选择集和如何绘制一个矢量图

    首先需要下载安装d3.js  :  yarn add d3 然后在组建中引入 :  import * as d3 from 'd3' 然后定义一个方法,在componentDidMount()这个钩子 ...

  7. 矢量图和Word:EPS,PDF,EMF和SVG

    1.EMF和Word 在学校的时候,我思考过一个问题,论文中的插图如何保证清晰度.关键之一就是使用矢量图.参考知乎问题:如何在论文中画出漂亮的插图?.常见的矢量图包括:EPS,EMF和SVG.SVG适 ...

  8. Matlab随笔之画图函数总结

    原文:Matlab随笔之画图函数总结 MATLAB函数画图 MATLAB不但擅长於矩阵相关的数值运算,也适合用在各种科学目视表示(Scientific visualization).本节将介绍MATL ...

  9. Matlab中的eig函数和Opecv中eigen()函数的区别

    奇异值分解的理论参见下面的链接 http://www.cnblogs.com/pinard/p/6251584.html https://blog.csdn.net/shenziheng1/artic ...

随机推荐

  1. 【Java虚拟机3】类加载器

    前言 Java虚拟机设计团队有意把类加载阶段中的"通过一个类的全限定名来获取描述该类的二进制字节流"这个动作放到Java虚拟机外部去实现,以便让应用程序自己决定如何去获取所需的类. ...

  2. 【c++ Prime 学习笔记】第6章 函数

    6.1 函数基础 函数定义包括:返回类型.函数名字.由0个或多个形参组成的列表以及函数体 通过调用运算符()来执行函数,它作用于一个表达式,该表达式是函数或函数指针.圆括号内是一个逗号隔开的实参列表, ...

  3. Java基础之原生JDBC操作数据库

    前言 日常开发中,我们都习惯了使用ORM框架来帮我们操作数据库,本文复习.记录Java如何使用原生JDBC操作数据库 代码编写 封装几个简单方法 find查询方法 findOne查询方法 update ...

  4. 2021.9.7考试总结[NOIP模拟49]

    T1 Reverse $BFS$暴力$O(n^2)$ 过程中重复枚举了很多点,考虑用链表记录当前点后面可到达的第一个未更新点. 搜索时枚举翻转子串的左端点,之后便可以算出翻转后$1$的位置. $cod ...

  5. 快速了解XML

    1. XML 定义 可扩展标记语言,标准通用标记语言的子集,简称XML.是一种用于标记电子文件使其具有结构性的标记语言. 2. XML 展示 如下是一个xml的标记展示,XML 是不作为的XML 被设 ...

  6. 原串反转 牛客网 程序员面试金典 C++ Python

    原串反转 牛客网 程序员面试金典 C++ Python 题目描述 请实现一个算法,在不使用额外数据结构和储存空间的情况下,翻转一个给定的字符串(可以使用单个过程变量). 给定一个string iniS ...

  7. 二,zabbix与php的一些问题

    zabbix 检查先决条件 一.php-bcmath 不支持 php 安装 bcmath 扩展(编译安装) PHP的linux版本需要手动安装BCMath扩展,在PHP的源码包中默认包含BCMath的 ...

  8. Linux 用户&用户组

    用户和用户组的概念 用户 ---> 使用操作系统的人 Linux系统是一个多用户多任务的分时操作系统,任何一个要使用系统资源的用户,都必须首先向系统管理员申请一个账号,然后以这个账号的身份进入系 ...

  9. yrm的安装和使用

    yrm的安装和使用 我们经常下载包的速度很忙有的还会卡住几十分钟,所以我们需要切换镜像,这样我们下载的速度会快很多 而yrm 是一个 yarn源管理器,允许你快速地在源间切换 安装 npm insta ...

  10. Power Platform Center of Excellence (CoE) 部署完成&主要内容说明

    随着目前国内使用Power Platform的企业越来越多,而在跟客户交付项目时,客户经常想了解平台的一些基本情况: Power Platform 有多少环境,分别是谁创建和管理? Power Pla ...