Ora

How do you make a circle on Python?

Published in Python Graphics 5 mins read

The simplest and most common way to draw a circle in Python is by using the built-in turtle graphics module, which is ideal for beginners and visual programming.

Drawing Circles with Python's Turtle Module

The turtle module provides an easy and intuitive way to create graphics and shapes. It simulates a "turtle" moving across a canvas, drawing lines as it goes. Drawing a circle is straightforward with its dedicated function.

Basic Circle Drawing

To draw a circle, you use the turtle.circle() function. This function takes the radius of the circle as its primary argument. The radius is specified in pixels, determining the size of your circle on the screen.

Here’s a basic example:

import turtle

# Create a turtle screen
screen = turtle.Screen()
screen.setup(width=600, height=400) # Optional: set screen dimensions
screen.title("Python Turtle Circle")

# Create a turtle object
my_turtle = turtle.Turtle()
my_turtle.shape("turtle") # Optional: change the turtle's shape

# Draw a circle
# The simplest way to create a circle is by specifying its radius.
# For instance, a radius of 100 pixels will create a moderately sized circle.
radius_in_pixels = 100
my_turtle.circle(radius_in_pixels)

# Keep the window open until it's closed manually
turtle.done()

In this code, my_turtle.circle(100) instructs the turtle to draw a circle with a radius of 100 pixels. This is the most direct method for drawing a circle using Python.

Customizing Your Circle

You can enhance your circles with various customizations, such as changing color, filling them, or adjusting the drawing speed.

Here are some common turtle functions you might use:

Turtle Function Description
turtle.circle(radius) Draws a circle with the specified radius.
turtle.color(color) Sets both the pen (outline) and fill color.
turtle.fillcolor(color) Sets only the fill color.
turtle.begin_fill() Marks the start of an area to be filled.
turtle.end_fill() Fills the area drawn since begin_fill() with the current fillcolor.
turtle.pensize(width) Sets the thickness of the pen line.
turtle.speed(speed) Sets the drawing speed (0-10, or "fastest", "slowest").

Example with Customization:

import turtle

screen = turtle.Screen()
screen.setup(width=600, height=400)
screen.bgcolor("lightblue") # Set background color
screen.title("Customized Python Turtle Circle")

my_turtle = turtle.Turtle()
my_turtle.shape("arrow")
my_turtle.speed(5) # Set drawing speed (1 slowest, 10 fastest, 0 no animation)
my_turtle.pensize(3) # Set pen thickness

# Draw a red circle with a blue outline
my_turtle.color("blue") # Outline color
my_turtle.fillcolor("red") # Fill color

my_turtle.begin_fill()
my_turtle.circle(80) # Draw a circle with 80 pixels radius
my_turtle.end_fill()

# Move the turtle and draw another circle
my_turtle.penup() # Lift the pen to move without drawing
my_turtle.goto(-150, 0) # Move to a new position
my_turtle.pendown() # Put the pen down

# Draw a green circle with a yellow outline
my_turtle.color("yellow")
my_turtle.fillcolor("green")
my_turtle.begin_fill()
my_turtle.circle(60) # Draw another circle
my_turtle.end_fill()

turtle.done()

For more details, refer to the official Python turtle module documentation.

Other Python Libraries for Drawing Circles

While turtle is excellent for simple graphics, other libraries offer more advanced capabilities for drawing circles in specific contexts.

Matplotlib for Data Visualization

Matplotlib is a powerful library for creating static, animated, and interactive visualizations in Python. When you need to plot a circle as part of a graph or chart, Matplotlib is the go-to choice.

import matplotlib.pyplot as plt
import matplotlib.patches as patches

fig, ax = plt.subplots() # Create a figure and an axes object

# Create a circle patch
circle = patches.Circle((0.5, 0.5), radius=0.2, color='blue', alpha=0.6) # (x, y), radius
ax.add_patch(circle)

# Set plot limits and aspect ratio
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.set_aspect('equal', adjustable='box') # Ensures the circle isn't distorted

plt.title("Circle with Matplotlib")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.grid(True)
plt.show()

Learn more at the Matplotlib documentation.

Pygame for Game Development

If you're developing a 2D game or interactive application, Pygame is an excellent framework. It provides functions for drawing various shapes, including circles, directly onto a screen surface.

import pygame

# Initialize Pygame
pygame.init()

# Set up the screen dimensions
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Pygame Circle")

# Define colors
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLUE = (0, 0, 255)

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # Fill the screen with white
    screen.fill(WHITE)

    # Draw a circle
    # pygame.draw.circle(surface, color, center_coordinates, radius, width=0)
    # width=0 means filled circle
    pygame.draw.circle(screen, RED, (400, 300), 100, 0) # Filled red circle
    pygame.draw.circle(screen, BLUE, (100, 100), 50, 5) # Blue outline, 5 pixels thick

    # Update the display
    pygame.display.flip()

pygame.quit()

Explore more at the Pygame documentation.

Pillow (PIL) for Image Manipulation

Pillow, a fork of the PIL (Python Imaging Library), is used for image processing. If you need to draw a circle on an existing image or create a new image with circles, Pillow is the right tool.

from PIL import Image, ImageDraw

# Create a new blank image (white background, 400x400 pixels)
img = Image.new('RGB', (400, 400), color = 'white')
draw = ImageDraw.Draw(img)

# Define circle bounding box (left, top, right, bottom)
# For a circle, width == height of the bounding box
# Example: a circle from (50, 50) to (150, 150)
draw.ellipse((50, 50, 150, 150), fill='green', outline='black', width=2)

# Another circle, yellow with red outline
draw.ellipse((200, 200, 350, 350), fill='yellow', outline='red', width=3)

# Save the image
img.save("circles_pillow.png")
# img.show() # Uncomment to display the image directly

Refer to the Pillow documentation for more information on drawing shapes.