- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to fill a polygon with a custom hatch in Matplotlib?
To fill a polygon with a custom hatch in matplotlib, we can override the matplotlib.hatch.Shapes class.
Steps
- Set the figure size and adjust the padding between and around the subplots.
- Make a hatch shape using polygon class and get the path.
- Override the custom hatch shape class, with shape_vertices, shape_codes, etc.
- Create a new figure or activate an existing figure.
- Add an axes to the figure as part of a subplot arrangement.
- Add an axes patch of shape polygon.
- Set the hatching pattern.
- To display the figure, use show() method.
Example
import matplotlib.hatch import matplotlib.pyplot as plt from matplotlib.patches import Polygon plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True my_hatch_shape = Polygon( [[0., 0.4], [-0.3, 0.1], [0.1, 0.2]], closed=True, fill=False).get_path() class MyCustomHatch(matplotlib.hatch.Shapes): filled = True size = 1.0 path = my_hatch_shape 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(MyCustomHatch) fig = plt.figure() ax = fig.add_subplot(111) pg = ax.add_patch(Polygon( [[0.2, 0.2], [0.2, 0.8], [0.8, 0.1]], closed=True, fill=False)) pg.set_hatch('c') plt.show()
Output
- Related Articles
- How do I fill a region with only hatch (no background colour) in matplotlib 2.0?
- How to fill an area within a polygon in Python using matplotlib?
- How to change the linewidth of a hatch in Matplotlib?
- Plot a circle with an edgecolor and hatch in Matplotlib
- How to decrease the hatch density in Matplotlib?
- How to decouple hatch and edge color in Matplotlib?
- How to create custom markers on a plot in Matplotlib
- How to make a polygon radar (spider) chart in Python Matplotlib?
- How to fill color below a curve in Matplotlib?
- How to use a custom png image marker in a plot (Matplotlib)?
- How to plot data into imshow() with custom colormap in Matplotlib?
- How to give a Pandas/Matplotlib bar graph custom colors?
- How to draw the largest polygon from a set of points in matplotlib?
- How to implement a custom Python Exception with custom message?
- How to fill rainbow color under a curve in Python Matplotlib?

Advertisements