The turtle module in Python is a powerful tool for drawing graphics. However, Python also offers several other libraries and techniques to create more complex or customized graphics.
Python offers various libraries for creating graphics besides the turtle module. Some popular alternatives include Tkinter, Pygame, Matplotlib, and the Python Imaging Library (PIL). These libraries provide numerous functions and features for drawing and manipulating graphics, from 2D shapes to 3D models, making them suitable for game development, data visualization, and image processing.
In this article, we will explore some of these alternatives for drawing without the turtle module in Python.
Overview of Python Graphics Libraries
matplotlib
: Used for creating static, animated, and interactive visualizations in Python.tkinter
: Used for creating GUI applications for creating GUI applications.Pygame
: Used for game development and multimedia applications.PIL
: Used for creating and manipulating images.
Creating Graphics with Different Libraries
Let’s now check the different ways in which we can create graphics with Python libraries without using the turtle module.
Creating Line Plots with Matplotlib
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
plt.plot(x, y)
plt.show()
- We import
pyplot
module frommatplotlib
library as its aliasplt
, an alias can be any conventional variable you wish to use. - Two lists x and y are defined which store the values of x coordinates and y coordinates respectively, these values will be used later for plotting.plot function of pyplot module is called to draw a line plot with given x and y coordinates.
- After all the functions we need to display the result for this we use
show
function which will open a separate window with the plot.
Output:

The line will connect the points (1, 1)
, (2, 4)
, (3, 9)
, (4, 16)
, and (5, 25)
.
Drawing Rectangles with Pygame
import pygame
pygame.init()
win = pygame.display.set_mode((500, 500))
rect = pygame.Rect(100, 100, 200, 200)
pygame.draw.rect(win, (255, 0, 0), rect)
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
- Import the pygame module for creating video games and multimedia applications.
- Initialize the pygame modules by calling pygame.init().
- Set up the main window of size 500×500 pixels using pygame.display.set_mode((500, 500)).
- Create a rectangle of size 200×200 pixels with position (100, 100) using pygame.Rect(100, 100, 200, 200), and store it in the variable rect.
- Set the color of the rectangle to red using (win, (255, 0, 0), rect).
- Update the display to show the rectangle using pygame.display.update().
- Enter a while loop to continuously check for events.
- Get a list of all the pygame events that have been executed.
- Check if the user has clicked the close button to terminate the program.
- Use the exit() function to terminate the entire program.
Output:

Creating Images with PIL
from PIL import Image, ImageDraw
img = Image.new('RGB', (500, 500), color = (255, 255, 255))
draw = ImageDraw.Draw(img)
draw.rectangle((100, 100, 300, 300), fill = (255, 0, 0), outline = (0, 0, 0))
img.show()
From Python Imaging Library
(PIL) we import Image
and ImageDraw
modules. To create an image we use Image.new('RGB', (500, 500), color = (255, 255, 255))
To draw a rectangle of the color red we use draw.rectangle((100, 100, 300, 300), fill = (255, 0, 0), outline = (0, 0, 0))
and later display it using img.show()
function.
Output:

Drawing Shapes with Tkinter
import tkinter as tk
CANVAS_WIDTH = 400
CANVAS_HEIGHT = 400
root = tk.Tk()
canvas = tk.Canvas(root, width=CANVAS_WIDTH, height=CANVAS_HEIGHT)
canvas.pack()
canvas.create_rectangle(100, 100, 300, 300, fill="blue")
root.mainloop()
This code uses the Tkinter library to create a simple GUI with a blue rectangle on a canvas. We import tkinter
as tk
and define the constants for the canvas size in CANVAS_WIDTH
and CANVAS_HEIGHT
.
To create a new window object using the tk()
constructor from Tkinter we use root=tk.Tk()
.The canvas
the variable contains the dimensions of the canvas and packs it into the window.
Next, we mention the dimensions for drawing a rectangle.root.mainloop()
starts the main event loop for the GUI.
Output:

Conclusion
Python offers various libraries for creating graphics beyond the turtle module, such as Tkinter, Pygame, Matplotlib, and the Python Imaging Library (PIL). Additional libraries like OpenCV and Pillow are also widely used. Each library has its strengths and weaknesses, catering to different needs in game development, data visualization, and image processing. By exploring these libraries, you can create a wide range of graphics and visualizations in Python.
Which library would be best suited for your next project?
Browse more articles at Askpython.