How to fill a polygon using custom hatching in matplotlib?

I use python and matplotlib to create multiple closed polygons. Then I need to fill them with a hatch, which can be done via set_hatch.

http://matplotlib.org/api/artist_api.html#matplotlib.patches.Patch.set_hatch

http://matplotlib.org/examples/pylab_examples/hatch_demo.html

Unfortunately, I work with images in shades of gray, and I need more hatches than the default - I would prefer to provide a bitmap (or some similar image) that could be tiled instead of using these hatches with different densities.

I am open to other python libraries (pyglet, pygame, PIL, etc.), however I would prefer the solution to be in python.

+8
python matplotlib image polygons hatchstyle
source share
1 answer

You can subclass matplotlib.hatch.Shapes and define a custom stroke based on any reference path drawn inside the unit square [[-0.5, 0.5] x [-0.5, 0.5]].

Tentative:

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

Donation:

enter image description here

+6
source share

All Articles