📜  Python中的 Matplotlib.patches.Arrow 类

📅  最后修改于: 2022-05-13 01:54:35.273000             🧑  作者: Mango

Python中的 Matplotlib.patches.Arrow 类

Matplotlib是Python中用于数组二维图的惊人可视化库。 Matplotlib 是一个基于 NumPy 数组的多平台数据可视化库,旨在与更广泛的 SciPy 堆栈一起使用。

Matplotlib.patches.Arrow

matplotlib.patches.Arrow类用于修补图中的箭头。它绘制从(x, y)(x + dx, y + dy)的箭头,并使用 width 参数缩放其宽度。

PropertyDescription
agg_filtera filter function that takes a (m, n, 3) float array and a dpi value that returns a (m, n, 3) array
alphafloat or None
animatedbool
antialiased or aaunknown
capstyle{‘butt’, ’round’, ‘projecting’}
clip_boxBbox
clip_onbool
clip_path[(Path, Transform)|Patch|None]
colorcolor or sequence of rgba tuples
containscallable
edgecolor or ec or edgecolorscolor or None or ‘auto’
facecolor or fc or facecolorscolor or None
figurefigure
fillbool
gidstr
hatch{‘/’, ‘\’, ‘|’, ‘-‘, ‘+’, ‘x’, ‘o’, ‘O’, ‘.’, ‘*’}
in_layoutbool
joinstyle{‘miter’, ’round’, ‘bevel’}
linestyle or ls{‘-‘, ‘–‘, ‘-.’, ‘:’, ”, (offset, on-off-seq), …}
linewidth or linewidths or lwfloat or None
path_effectsAbstractPathEffect
pickerNone or bool or float or callable
path_effectsAbstractPathEffect
pickerfloat or callable[[Artist, Event], Tuple[bool, dict]]
rasterizedbool or None
sketch_params(scale: float, length: float, randomness: float)
snapbool or None
transformmatplotlib.transforms.Transform
urlstrvisiblebool
zorderfloat

示例 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()

输出: