当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


Python Matplotlib.patches.RegularPolygon用法及代码示例

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

matplotlib.patches.RegularPolygon

这个matplotlib.patches.RegularPolygon类用于添加常规多边形面片。

用法: class matplotlib.patches.RegularPolygon(xy, numVertices, radius=5, orientation=0, **kwargs)

参数:

  • xy:中心的长度2个元组(x,y)。
  • numVertices:它表示顶点数。
  • radius:从中心到每个顶点的距离。
  • orientation:用于旋转多边形(以弧度为单位)。

下表列出了有效的kwarg;



PROPERTY DESCRIPTION
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 
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 centres 
ax.scatter(hcoord, vcoord, c =.lower()  
                               for c in colors],  
           alpha = 0.5) 
  
plt.show()

输出:

范例2:

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

输出:




相关用法


注:本文由纯净天空筛选整理自RajuKumar19大神的英文原创作品 Matplotlib.patches.RegularPolygon class in Python。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。