How to Draw Shapes in Python

Python, being one of the most renowned languages due to its simplicity and versatility, has libraries that allow us to create, manipulate, and visualize graphical representations with considerable ease. In this tutorial, we’ll learn how to draw shapes using Python – more specifically, we’ll use a library called Matplotlib.

Step 1: Install the Matplotlib library

Python itself doesn’t have built-in graphics functionality, but it provides several libraries to handle this. One of the most popular libraries for graphics in Python is Matplotlib. You can install it using pip:

Step 2: Import Matplotlib

After installation, the next step is to import the Matplotlib package into your python script.

Step 3: Draw a Circle

Below is an example of how to draw a circle in Python using Matplotlib:

In the code above, the object “circle” is created with the plt.Circle function. The parameters include the center point, the radius, and the color of the circle, as well as whether the circle is filled. In the following lines, we add this object to our plot.

Step 4: Draw a Rectangle

To draw a rectangle, we will use the function plt.Rectangle. This function takes the bottom left point and the width and the height of the rectangle as parameters.

Similar to the circle, we create the “rectangle” object, add it to our plot, and display it.

Step 5: Save the figure

Finally, if you want to save these figures, use the savefig() function.

Your file, named “shapes.png”, will be saved in your present working directory.

Full Code:


In this tutorial, we’ve covered the basics of drawing shapes in Python using the Matplotlib library. We discussed how to draw a circle and a rectangle, which are some of the most basic shapes, but Matplotlib doesn’t stop there. You can draw various other shapes and even complex geometrical figures. You’re encouraged to further explore the Matplotlib documentation and unleash your creative potential on data representation!