欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > python >内容正文

python

python多边形图案_如何用matplotlib中的自定义图案填充多边形?

发布时间:2025/4/5 python 39 豆豆
生活随笔 收集整理的这篇文章主要介绍了 python多边形图案_如何用matplotlib中的自定义图案填充多边形? 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

您可以子类matplotlib.hatch.Shapes,并根据在unit square[[-0.5,0.5]x[-0.5,0.5]]内绘制的任何引用路径定义自定义图案填充。在

暂定: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()

给予:

总结

以上是生活随笔为你收集整理的python多边形图案_如何用matplotlib中的自定义图案填充多边形?的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。