﻿ How to Draw a Circle Using Matplotlib in Python ﻿ # How to Draw a Circle Using Matplotlib in Python In this article, we show how to draw a circle using Matplotlib in Python

aPatches in matplotlib allow a programmer to draw geometric shapes, such as circles and triangles. Each shape is referred to as a patch.

To draw a circle using Matplotlib, the line of code below will do so.

So, the first thing we must do is import the matplotlib package.

We then create a function called create_circle().

In this function, we create a variable called circle and set it equal to plt.Circle((0,0), radius=5), gives the circle a center of (0,0) on an X-Y axis, along with a radius of 5 units (for a total diamter of 10 units).

We then return the circle. This returns the Object reference to the circle, not the image of the circle itself.

We then create a function called show_shape(). Inside of this function has a parameter called patch. This patch refers to the shape created.

The line, ax=plt.gca(), gives a reference to the current Axes.

The line, plt.axis('scaled'), gives appropriate scaling that you specify for the shape. Without this line, a default value of 1.0 vertically and 1.0 horizontally will be assigned to the graph. Being that our circle has a radius of 5, it wouldn't show on the graph. Thus, this line is essential to give any type of scaling for the graph.

The line, plt.show(), will show the circle graph.

Remember, that this block simply creates the function, show_shape(), and doesn't actually show the shape yet in the code, because it hasn't been called yet.

The next block of code creates a circle object and then shows the circle.

Running the code above produces the circle shown below. You can see the circle has a center at point (0,0) and a radius of 5.

And this is how a circle can be drawn with matplotlib in Python.

Related Resources