Skip to main content
TopMiniSite

Back to all posts

How to Fill Polygons With Unique Color In Python Matplotlib?

Published on
3 min read
How to Fill Polygons With Unique Color In Python Matplotlib? image

Best Tools for Coloring Polygons in Python Matplotlib to Buy in October 2025

1 Python Tools for Scientists: An Introduction to Using Anaconda, JupyterLab, and Python's Scientific Libraries

Python Tools for Scientists: An Introduction to Using Anaconda, JupyterLab, and Python's Scientific Libraries

BUY & SAVE
$39.81 $49.99
Save 20%
Python Tools for Scientists: An Introduction to Using Anaconda, JupyterLab, and Python's Scientific Libraries
2 Python Data Science Handbook: Essential Tools for Working with Data

Python Data Science Handbook: Essential Tools for Working with Data

BUY & SAVE
$44.18 $79.99
Save 45%
Python Data Science Handbook: Essential Tools for Working with Data
3 Deep Learning with PyTorch: Build, train, and tune neural networks using Python tools

Deep Learning with PyTorch: Build, train, and tune neural networks using Python tools

BUY & SAVE
$34.40 $49.99
Save 31%
Deep Learning with PyTorch: Build, train, and tune neural networks using Python tools
4 Ultimate Python Libraries for Data Analysis and Visualization: Leverage Pandas, NumPy, Matplotlib, Seaborn, Julius AI and No-Code Tools for Data ... and Statistical Analysis (English Edition)

Ultimate Python Libraries for Data Analysis and Visualization: Leverage Pandas, NumPy, Matplotlib, Seaborn, Julius AI and No-Code Tools for Data ... and Statistical Analysis (English Edition)

BUY & SAVE
$37.95
Ultimate Python Libraries for Data Analysis and Visualization: Leverage Pandas, NumPy, Matplotlib, Seaborn, Julius AI and No-Code Tools for Data ... and Statistical Analysis (English Edition)
5 Modern Python Cookbook: 130+ updated recipes for modern Python 3.12 with new techniques and tools

Modern Python Cookbook: 130+ updated recipes for modern Python 3.12 with new techniques and tools

BUY & SAVE
$25.11 $54.99
Save 54%
Modern Python Cookbook: 130+ updated recipes for modern Python 3.12 with new techniques and tools
6 Advanced Analytics with Power BI and Excel: Learn powerful visualization and data analysis techniques using Microsoft BI tools along with Python and R (English Edition)

Advanced Analytics with Power BI and Excel: Learn powerful visualization and data analysis techniques using Microsoft BI tools along with Python and R (English Edition)

BUY & SAVE
$37.95
Advanced Analytics with Power BI and Excel: Learn powerful visualization and data analysis techniques using Microsoft BI tools along with Python and R (English Edition)
+
ONE MORE?

To fill polygons with unique colors in Python using Matplotlib, you need to create a list of colors and then use the fill method in Matplotlib to fill each polygon with a different color. You can specify the colors you want to use for each polygon by passing a color argument to the fill method. Additionally, you can use a loop to iterate through each polygon and assign a different color to it. This will allow you to fill each polygon with a unique color in your Matplotlib plot.

How to customize the color scheme of polygons in Matplotlib?

In Matplotlib, you can customize the color scheme of polygons by using the facecolor parameter when plotting the polygons. Here's an example code snippet to demonstrate how to customize the color scheme of polygons:

import matplotlib.pyplot as plt import numpy as np

Generate some random data for the coordinates of the polygons

np.random.seed(0) n = 10 x = np.random.rand(n) y = np.random.rand(n)

Create a list of colors for the polygons

colors = plt.cm.viridis(np.linspace(0, 1, n)) # Use the 'viridis' colormap

Plot the polygons with customized color scheme

plt.figure() for i in range(n): polygon = plt.Polygon([(x[i], y[i]), (x[i]+0.1, y[i]+0.2), (x[i]+0.3, y[i])], facecolor=colors[i]) plt.gca().add_patch(polygon)

plt.axis('scaled') plt.show()

In this code snippet, we first generate some random data for the coordinates of the polygons. We then create a list of colors using the viridis colormap from Matplotlib's colormap library. Finally, we plot the polygons using the customized color scheme by setting the facecolor parameter to the corresponding color from the colors list.

You can replace the viridis colormap with any other colormap available in Matplotlib to create different color schemes for the polygons. Additionally, you can also manually specify the color for each polygon by providing a single color or a list of colors to the facecolor parameter.

How to fill polygons with random colors in Matplotlib?

import matplotlib.pyplot as plt import numpy as np

Create a random array of colors

colors = np.random.rand(10,4)

Create a figure and axis

fig, ax = plt.subplots()

Create a polygon with random colors

polygon = plt.Polygon([(0,0), (0,1), (1,1), (1,0)], edgecolor='black', facecolor=colors[0]) ax.add_patch(polygon)

Set axis limits

ax.set_xlim(0, 1) ax.set_ylim(0, 1)

plt.show()

How to combine multiple color techniques for polygon filling in Matplotlib?

In Matplotlib, you can combine multiple color techniques for polygon filling by using the fill method in combination with different color specifications. Here is an example of how you can achieve this:

  1. Create a figure and axis object:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

  1. Define the vertices of the polygon:

polygon = [(0, 0), (1, 0), (1, 1), (0, 1)]

  1. Use the fill method to fill the polygon with a single color:

ax.fill([x for x, y in polygon], [y for x, y in polygon], color='blue')

  1. Use the fill method again to fill the polygon with a gradient color:

ax.fill([x for x, y in polygon], [y for x, y in polygon], color='blue', alpha=0.5)

  1. Use the fill method to fill the polygon with a patterned color:

ax.fill([x for x, y in polygon], [y for x, y in polygon], hatch='//', edgecolor='black')

  1. Use the fill method to fill the polygon with a custom colormap color:

import numpy as np

colors = np.random.rand(len(polygon), 3) ax.fill([x for x, y in polygon], [y for x, y in polygon], color=colors)

  1. Show the plot:

plt.show()

By combining these different color techniques, you can create visually appealing polygon fillings in Matplotlib.