Matplotlib是Python中令人惊叹的可视化库,用于数组的二维图。 Matplotlib是一个基于NumPy数组的多平台数据可视化库,旨在与更广泛的SciPy堆栈配合使用。
Matplotlib.patches.Arrow
这个matplotlib.patches.Arrow
类用于修补图中的箭头。它从绘制箭头(x, y)
至(x + dx, y + dy)
并使用width参数缩放其宽度。
用法: class matplotlib.patches.Arrow(x, y, dx, dy, width=1.0, **kwargs)
参数:
- x:它代表箭头尾部的x坐标。
- y:它表示箭头尾部的y坐标。
- dx:它表示x方向上的箭头长度。
- dy:它表示y方向上的箭头长度。
- width:它是一个可选参数,默认值为1。它是箭头宽度的比例因子。在默认值中,尾部宽度为0.2,头部宽度为0.6。
- **kwargs:这些是下表中提到的补丁程序属性。
属性 | 描述 |
---|---|
agg_filter | 一个过滤器函数,它接受一个(m,n,3)浮点数组,一个dpi值返回一个(m,n,3)数组 |
alpha | 浮点数或无 |
animated | bool |
抗锯齿或抗锯齿 | unknown |
capstyle | {‘butt’,“回合”,‘projecting’} |
clip_box | Bbox |
clip_on | bool |
clip_path | [(Path,Transform)|补丁|无] |
color | rgba元组的颜色或顺序 |
contains | callable |
edgecolor或ec或edgecolors | 颜色或无或‘auto’ |
facecolor或fc或facecolors | 颜色或无 |
figure | figure |
fill | bool |
gid | str |
hatch | {‘/’、‘\’、‘|’、‘-’、‘+’、‘x’, ‘o’、‘O’、‘.’、‘*’} |
in_layout | bool |
joinstyle | {‘miter’,“回合”,‘bevel’} |
线型或ls | {“-”,“-”,“-。”,“:”,“,(偏移量,on-off-seq),...} |
线宽或线宽或lw | 浮点数或无 |
path_effects | AbstractPathEffect |
picker | 无或布尔或浮点数或可赎回 |
path_effects | AbstractPathEffect |
picker | float或callable [[Artist,Event],Tuple [bool,dict]] |
rasterized | 布尔还是无 |
sketch_params | (比例:浮点数,长度:浮点数,随机性:浮点数) |
snap | 布尔还是无 |
transform | matplotlib.transforms.Transform |
url | str | visible | bool |
zorder | float |
范例1:
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.path as mpath
import matplotlib.lines as mlines
import matplotlib.patches as mpatches
from matplotlib.collections import PatchCollection
def label(xy, text):
# shift y-value for label so that
# it's below the artist
y = xy[1] - 0.15
plt.text(xy[0], y, text, ha ="center",
family ='sans-serif', size = 14)
fig, ax = plt.subplots()
# create 3x3 grid to plot
# the artists
grid = np.mgrid[0.2:0.8:3j,
0.2:0.8:3j].reshape(2, -1).T
patches = []
# add an arrow
arrow = mpatches.Arrow(grid[5, 0] - 0.05,
grid[5, 1] - 0.05, 0.1, 0.1,
width = 0.1)
patches.append(arrow)
label(grid[5], " Sample Arrow")
colors = np.linspace(0, 1, len(patches))
collection = PatchCollection(patches,
cmap = plt.cm.hsv,
alpha = 0.3)
collection.set_array(np.array(colors))
ax.add_collection(collection)
plt.axis('equal')
plt.axis('off')
plt.tight_layout()
plt.show()
输出:
范例2:
from matplotlib import pyplot as plt
from matplotlib.patches import Rectangle, Arrow
import numpy as np
nmax = 9
xdata = range(nmax)
ydata = np.random.random(nmax)
plt.ion()
fig, ax = plt.subplots()
ax.set_aspect("equal")
ax.plot(xdata, ydata, 'o-')
ax.set_xlim(-1, 10)
ax.set_ylim(-1, 4)
rect = Rectangle((0, 0), nmax, 1, zorder = 10)
ax.add_patch(rect)
x0, y0 = 5, 3
arrow = Arrow(1, 1, x0-1, y0-1, color ="# aa0088")
a = ax.add_patch(arrow)
plt.draw()
for i in range(nmax):
rect.set_x(i)
rect.set_width(nmax - i)
a.remove()
arrow = Arrow(1 + i, 1, x0-i + 1, y0-1,
color ="# aa0088")
a = ax.add_patch(arrow)
fig.canvas.draw_idle()
plt.pause(0.4)
plt.waitforbuttonpress()
plt.show()
输出:
相关用法
- Python Matplotlib.ticker.MultipleLocator用法及代码示例
- Python Matplotlib.gridspec.GridSpec用法及代码示例
- Python Matplotlib.patches.CirclePolygon用法及代码示例
- Python Matplotlib.colors.Normalize用法及代码示例
注:本文由纯净天空筛选整理自RajuKumar19大神的英文原创作品 Matplotlib.patches.Arrow Class in Python。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。