📜  Python中的 Matplotlib.patches.PathPatch

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

Python中的 Matplotlib.patches.PathPatch

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

matplotlib.patches.PathPatch

matplotlib.patches.PathPatch类用于绘制一般多曲线路径补丁。

下表给出了有效的 kwargs 参数列表:

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
urlstr
visiblebool
zorderfloat

示例 1:

import numpy as np
import matplotlib.cm as cm
import matplotlib.pyplot as plt
import matplotlib.cbook as cbook
from matplotlib.path import Path
from matplotlib.patches import PathPatch
  
  
delta = 0.025
x = y = np.arange(-3.0, 3.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = np.exp(-X**2 - Y**2)
Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)
Z = (Z1 - Z2) * 2
  
path = Path([[0, 1], [1, 0], [0, -1], [-1, 0], [0, 1]])
patch = PathPatch(path, facecolor ='none')
  
fig, ax = plt.subplots()
ax.add_patch(patch)
  
im = ax.imshow(Z, interpolation ='bilinear', cmap = cm.gray,
               origin ='lower', extent =[-3, 3, -3, 3],
               clip_path = patch, clip_on = True)
im.set_clip_path(patch)
  
plt.show()

输出:

示例 2:

import matplotlib.pyplot as plt 
import numpy as np
from matplotlib.path import Path
from matplotlib.patches import PathPatch
  
  
fig = plt.figure() 
  
ax = fig.add_subplot(111, aspect ='equal') 
path = Path([[0, 0], [0, 1], [1, 0], [0, 0]])
patch = PathPatch(path, facecolor ='none')
ax.add_patch(patch) 
  
Z, Z2 = np.meshgrid(np.linspace(0, 1), np.linspace(0, 1))
  
im = plt.imshow(Z-Z2, 
                interpolation ='bilinear', 
                cmap = plt.cm.RdYlGn,
                origin ='lower',
                extent =[0, 1, 0, 1],
                clip_path = patch,
                clip_on = True)
  
im.set_clip_path(patch)
  
ax.set_xlim((0, 1)) 
ax.set_ylim((0, 1)) 
  
plt.show()

输出: