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 January 2026

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
$38.96 $49.99
Save 22%
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 Python for Data Analysis: Data Wrangling with pandas, NumPy, and Jupyter

Python for Data Analysis: Data Wrangling with pandas, NumPy, and Jupyter

BUY & SAVE
$43.99 $79.99
Save 45%
Python for Data Analysis: Data Wrangling with pandas, NumPy, and Jupyter
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
$16.99
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 Python and Data Structures Flashcards for Beginners and Experienced Programmers

Python and Data Structures Flashcards for Beginners and Experienced Programmers

  • COMPREHENSIVE COVERAGE WITH REAL-WORLD EXAMPLES FOR DEEP UNDERSTANDING.

  • INTERACTIVE LEARNING WITH HANDS-ON EXERCISES FOR PRACTICAL CODING SKILLS.

  • PORTABLE RESOURCES FOR FLEXIBLE STUDYING ANYTIME, ANYWHERE, ON ANY DEVICE.

BUY & SAVE
$24.99 $39.99
Save 38%
Python and Data Structures Flashcards for Beginners and Experienced Programmers
6 Mastering Data Visualization with Python: A Complete Guide to Matplotlib, Seaborn, and Plotly for Data Analysis, Dashboards, and Storytelling (Python Series – Learn. Build. Master. Book 4)

Mastering Data Visualization with Python: A Complete Guide to Matplotlib, Seaborn, and Plotly for Data Analysis, Dashboards, and Storytelling (Python Series – Learn. Build. Master. Book 4)

BUY & SAVE
$4.99
Mastering Data Visualization with Python: A Complete Guide to Matplotlib, Seaborn, and Plotly for Data Analysis, Dashboards, and Storytelling (Python Series – Learn. Build. Master. Book 4)
+
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.