python多边形图案_如何用matplotlib中的自定义图案填充多边形?
您可以子類matplotlib.hatch.Shapes,并根據(jù)在unit square[[-0.5,0.5]x[-0.5,0.5]]內(nèi)繪制的任何引用路徑定義自定義圖案填充。在
暫定:import numpy as np
import matplotlib.hatch
import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse, Polygon
house_path = Polygon(
[[-0.3, -0.4], [0.3, -0.4], [0.3, 0.1], [0., 0.4], [-0.3, 0.1]],
closed=True, fill=False).get_path()
class CustomHatch(matplotlib.hatch.Shapes):
"""
Custom hatches defined by a path drawn inside [-0.5, 0.5] square.
Identifier 'c'.
"""
filled = True
size = 1.0
path = house_path
def __init__(self, hatch, density):
self.num_rows = (hatch.count('c')) * density
self.shape_vertices = self.path.vertices
self.shape_codes = self.path.codes
matplotlib.hatch.Shapes.__init__(self, hatch, density)
matplotlib.hatch._hatch_types.append(CustomHatch)
fig = plt.figure()
ax = fig.add_subplot(111)
ellipse = ax.add_patch(Ellipse((0.5, 0.5), 0.3, 0.5, fill=False))
ellipse.set_hatch('c')
ellipse.set_color('red')
plt.show()
給予:
總結(jié)
以上是生活随笔為你收集整理的python多边形图案_如何用matplotlib中的自定义图案填充多边形?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python怎么自动生成测试报告_pyt
- 下一篇: python hmac_Python实现