Creating Shapes with Python Matplotlib

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

This tutorial is designed to guide users on how to create circles, wedges and polygons using Python Matplotlib. Users will also be able to use .collections.PatchCollection to visualize the created shapes.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/using_packages("`Using Packages`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/PythonStandardLibraryGroup -.-> python/math_random("`Math and Random`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-48852{{"`Creating Shapes with Python Matplotlib`"}} matplotlib/importing_matplotlib -.-> lab-48852{{"`Creating Shapes with Python Matplotlib`"}} matplotlib/figures_axes -.-> lab-48852{{"`Creating Shapes with Python Matplotlib`"}} python/booleans -.-> lab-48852{{"`Creating Shapes with Python Matplotlib`"}} python/for_loops -.-> lab-48852{{"`Creating Shapes with Python Matplotlib`"}} python/lists -.-> lab-48852{{"`Creating Shapes with Python Matplotlib`"}} python/tuples -.-> lab-48852{{"`Creating Shapes with Python Matplotlib`"}} python/importing_modules -.-> lab-48852{{"`Creating Shapes with Python Matplotlib`"}} python/using_packages -.-> lab-48852{{"`Creating Shapes with Python Matplotlib`"}} python/standard_libraries -.-> lab-48852{{"`Creating Shapes with Python Matplotlib`"}} python/math_random -.-> lab-48852{{"`Creating Shapes with Python Matplotlib`"}} python/numerical_computing -.-> lab-48852{{"`Creating Shapes with Python Matplotlib`"}} python/data_visualization -.-> lab-48852{{"`Creating Shapes with Python Matplotlib`"}} python/build_in_functions -.-> lab-48852{{"`Creating Shapes with Python Matplotlib`"}} end

Import the necessary libraries

First, we need to import the necessary libraries.

import matplotlib.pyplot as plt
import numpy as np

from matplotlib.collections import PatchCollection
from matplotlib.patches import Circle, Polygon, Wedge

Create a figure and an axis

We create a figure and an axis to plot the shapes.

fig, ax = plt.subplots()

Define the number of vertices and the number of shapes

We define the number of vertices and the number of shapes to create.

resolution = 50  ## the number of vertices
N = 3

Create circles

We create circles using Circle() and append them to a list of patches.

x = np.random.rand(N)
y = np.random.rand(N)
radii = 0.1*np.random.rand(N)
patches = []
for x1, y1, r in zip(x, y, radii):
    circle = Circle((x1, y1), r)
    patches.append(circle)

Create wedges

We create wedges using Wedge() and append them to the list of patches.

x = np.random.rand(N)
y = np.random.rand(N)
radii = 0.1*np.random.rand(N)
theta1 = 360.0*np.random.rand(N)
theta2 = 360.0*np.random.rand(N)
for x1, y1, r, t1, t2 in zip(x, y, radii, theta1, theta2):
    wedge = Wedge((x1, y1), r, t1, t2)
    patches.append(wedge)

Add limiting conditions to wedges

We add limiting conditions to wedges.

patches += [
    Wedge((.3, .7), .1, 0, 360),             ## Full circle
    Wedge((.7, .8), .2, 0, 360, width=0.05),  ## Full ring
    Wedge((.8, .3), .2, 0, 45),              ## Full sector
    Wedge((.8, .3), .2, 45, 90, width=0.10),  ## Ring sector
]

Create polygons

We create polygons using Polygon() and append them to the list of patches.

for i in range(N):
    polygon = Polygon(np.random.rand(N, 2), closed=True)
    patches.append(polygon)

Set colors and create PatchCollection

We set the colors of the shapes and create a PatchCollection().

colors = 100 * np.random.rand(len(patches))
p = PatchCollection(patches, alpha=0.4)
p.set_array(colors)
ax.add_collection(p)
fig.colorbar(p, ax=ax)

Display the plot

We display the plot.

plt.show()

Summary

This tutorial has shown how to create circles, wedges, and polygons using Python Matplotlib. We have also learned how to use .collections.PatchCollection to visualize the created shapes.

Other Python Tutorials you may like