📜  Python中的 Matplotlib.patches.RegularPolygon 类

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

Python中的 Matplotlib.patches.RegularPolygon 类

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

matplotlib.patches.RegularPolygon

matplotlib.patches.RegularPolygon 类用于添加正多边形补丁。

下表列出了有效的 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:

Python3
import matplotlib.pyplot as plt
from matplotlib.patches import RegularPolygon
import numpy as np
 
 
coord = [[0, 0, 0],
         [0, 1, -1],
         [-1, 1, 0],
         [-1, 0, 1],
         [0, -1, 1],
         [1, -1, 0],
         [1, 0, -1]]
 
colors = [["Green"],
          ["Green"],
          ["Green"],
          ["Green"],
          ["Green"],
          ["Green"],
          ["Green"]]
 
labels = [['1'], ['2'],
          ['3'], ['4'],
          ['5'], ['6'],
          ['7']]
 
# Horizontal cartesian coords
hcoord = for c in coord]
 
# Vertical cartersian coords
vcoord = [2. * np.sin(np.radians(60)) * (c[1] - c[2]) /3.
          for c in coord]
 
fig, ax = plt.subplots(1)
ax.set_aspect('equal')
 
# Add some coloured hexagons
for x, y, c, l in zip(hcoord, vcoord, colors, labels):
     
    # matplotlib understands lower
    # case words for colours
    color = c[0].lower()
    hex = RegularPolygon((x, y),
                         numVertices = 6,
                         radius = 2. / 3.,
                         orientation = np.radians(30),
                         facecolor = color,
                         alpha = 0.2,
                         edgecolor ='k')
     
    ax.add_patch(hex)
     
    # Also add a text label
    ax.text(x, y + 0.2, l[0], ha ='center',
            va ='center', size = 20)
 
# add scatter points in hexagon centers
ax.scatter(hcoord, vcoord, c =.lower()
                               for c in colors],
           alpha = 0.5)
 
plt.show()


Python3
import matplotlib.pyplot as plt
from matplotlib.patches import RegularPolygon
from matplotlib.collections import PatchCollection
import numpy as np
 
 
xy = np.random.random((10, 2))
z = np.random.random(10)
 
patches = [RegularPolygon((x, y),
                          5, 0.1)
           for x, y in xy]
 
collection = PatchCollection(patches,
                             array = z,
                             edgecolors ='brown',
                             lw = 2)
 
fig, ax = plt.subplots()
 
ax.patch.set(facecolor ='green')
ax.add_collection(collection)
ax.autoscale()
 
plt.show()


输出:

示例 2:

Python3

import matplotlib.pyplot as plt
from matplotlib.patches import RegularPolygon
from matplotlib.collections import PatchCollection
import numpy as np
 
 
xy = np.random.random((10, 2))
z = np.random.random(10)
 
patches = [RegularPolygon((x, y),
                          5, 0.1)
           for x, y in xy]
 
collection = PatchCollection(patches,
                             array = z,
                             edgecolors ='brown',
                             lw = 2)
 
fig, ax = plt.subplots()
 
ax.patch.set(facecolor ='green')
ax.add_collection(collection)
ax.autoscale()
 
plt.show()

输出: