How to Draw Shapes in Matplotlib with Python
Last Updated :
22 Jul, 2024
Matplotlib provides a collection of classes and functions that allow you to draw and manipulate various shapes on your plots. Whether you're adding annotations, creating diagrams, or visualizing data, understanding how to use these tools effectively will enhance your ability to create compelling visualizations in Python.
Before we start drawing shapes, let’s review some basic concepts:
- Matplotlib: A comprehensive library for creating static, animated, and interactive visualizations in Python.
- Axes: The area on which you plot your data. Shapes are drawn on these axes.
- Patches: A collection of classes in Matplotlib used to draw shapes like rectangles, circles, and polygons.
Draw Shapes in Matplotlib
1. Install Matplotlib
First, ensure we have Matplotlib installed. we can install it using pip if you haven’t already:
pip install matplotlib
2. Import Required Libraries
Start by importing the necessary libraries:
Python
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle, Circle, Polygon
3. Create a Basic Plot
Set up a basic plot where you can draw your shapes:
Python
4. Drawing Shapes
A. Rectangle
To draw a rectangle, use the Rectangle class:
Python
rect = Rectangle((0.2, 0.2), width=0.5, height=0.3,
edgecolor='blue', facecolor='lightblue')
ax.add_patch(rect)
B. Circle
To draw a circle, use the Circle class:
Python
circle = Circle((0.5, 0.5), radius=0.2, edgecolor='red', facecolor='lightcoral')
ax.add_patch(circle)
C. Polygon
To draw a polygon, use the Polygon class:
Python
polygon = Polygon([(0.2, 0.2), (0.8, 0.2), (0.5, 0.8)],
edgecolor='green', facecolor='lightgreen')
ax.add_patch(polygon)
5. Customize and Display the Plot
Set the limits and aspect ratio to ensure your shapes are visible:
Python
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.set_aspect('equal')
plt.grid(True)
plt.title('Drawing Shapes in Matplotlib')
plt.show()
Examples
Example 1: Rectangle
Python
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
fig, ax = plt.subplots()
rect = Rectangle((0.2, 0.2), width=0.5, height=0.3, edgecolor='blue', facecolor='lightblue')
ax.add_patch(rect)
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.set_aspect('equal')
plt.grid(True)
plt.title('Rectangle')
plt.show()
Output:
Example 2: Circle
Python
import matplotlib.pyplot as plt
from matplotlib.patches import Circle
fig, ax = plt.subplots()
circle = Circle((0.5, 0.5), radius=0.2, edgecolor='red', facecolor='lightcoral')
ax.add_patch(circle)
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.set_aspect('equal')
plt.grid(True)
plt.title('Circle')
plt.show()
Output:
Example 3: Triangle
Python
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
fig, ax = plt.subplots()
polygon = Polygon([(0.2, 0.2), (0.8, 0.2), (0.5, 0.8)], edgecolor='green', facecolor='lightgreen')
ax.add_patch(polygon)
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.set_aspect('equal')
plt.grid(True)
plt.title('Polygon')
plt.show()
Output:
Conclusion
Drawing shapes in Matplotlib is simple and provides a wide range of options for creating and customizing displays. Using the Rectangle, Circle, and Polygon classes, you can add different shapes to your plots to help with data representation or visualization style. Experiment with various shapes and modification choices to see how they affect your data representations.
Similar Reads
How to Create Subplots in Matplotlib with Python? Matplotlib is a widely used data visualization library in Python that provides powerful tools for creating a variety of plots. One of the most useful features of Matplotlib is its ability to create multiple subplots within a single figure using the plt.subplots() method. This allows users to display
6 min read
How to Draw 3D Cube using Matplotlib in Python? In this article, we will deal with the 3d plots of cubes using matplotlib and Numpy. Cubes are one of the most basic of 3D shapes. A cube is a 3-dimensional solid object bounded by 6 identical square faces. The cube has 6-faces, 12-edges, and 8-corners. All faces are squares of the same size. The to
6 min read
How to Draw a Circle Using Matplotlib in Python? A Circle is a mathematical figure formed by joining all points lying on the same plane and are at equal distance from a given point. We can plot a circle in python using Matplotlib. There are multiple ways to plot a Circle in python using Matplotlib. Method 1: Using matplotlib.patches.Circle() func
3 min read
How to Set the X and the Y Limit in Matplotlib with Python? In this article, we will learn how to set the X limit and Y limit in Matplotlib with Python. Matplotlib is a visualization library supported by Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy s
2 min read
How to Add Axes to a Figure in Matplotlib with Python? Matplotlib is a library in Python used to create figures and provide tools for customizing it. It allows plotting different types of data, geometrical figures. In this article, we will see how to add axes to a figure in matplotlib. We can add axes to a figure in matplotlib by passing a list argument
2 min read
How to draw 2D Heatmap using Matplotlib in python? A heatmap is a great tool for visualizing data across the surface. It highlights data that have a higher or lower concentration in the data distribution. A 2-D Heatmap is a data visualization tool that helps to represent the magnitude of the matrix in form of a colored table. In Python, we can plot
5 min read
How to Add Markers to a Graph Plot in Matplotlib with Python? In this article, we will learn how to add markers to a Graph Plot in Matplotlib with Python. For that just see some concepts that we will use in our work. Graph Plot: A plot is a graphical technique for representing a data set, usually as a graph showing the relationship between two or more variable
2 min read
How to Draw Rectangle on Image in Matplotlib? Prerequisites: Matplotlib Given an image the task here is to draft a python program using matplotlib to draw a rectangle on it. Matplotlib comes handy with rectangle() function which can be used for our requirement. Syntax: Rectangle(xy, width, height, angle=0.0, **kwargs) Parameters: xy: Lower left
2 min read
How to import matplotlib in Python? Matplotlib is a Python library used to create different types of charts and graphs. It helps to turn data into visual formats like line charts, bar graphs and histograms. This makes it easier to understand and present your data. In this guide youâll learn how to install and import Matplotlib in Pyth
1 min read
Matplotlib.pyplot.draw() in Python Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. matplotlib.pyplot.draw() Function The draw() function in pyplot module of matplotlib library is used to r
1 min read