Assignment Chef icon Assignment Chef
All English tutorials

Programming lesson

Plotting Earthquake Data with Turtle Graphics: A Step-by-Step Guide

Learn how to read earthquake data from a CSV file and visualize it on a world map using Python turtle graphics. Perfect for students working on CSCI 141 lab assignments.

earthquake data visualization turtle graphics Python CSCI 141 lab 8 plot earthquakes on map CSV file parsing Python magnitude circle size latitude longitude conversion Python data plotting turtle circle color magnitude earthquake map turtle Python file I/O tutorial data visualization lab Python turtle graphics project earthquake data analysis student programming lab

Introduction: Why Visualize Earthquake Data?

Earthquakes are one of nature's most powerful phenomena. In this tutorial, you'll learn how to read historical earthquake data from a file and plot each earthquake on a map using Python's turtle graphics. This lab is a great way to practice file I/O, data parsing, and graphical plotting—skills that are essential for any budding programmer. By the end, you'll have a program that draws circles on a world map, with size and color representing magnitude. Let's dive in!

Setup: Getting Your Environment Ready

First, create a lab8 directory. Download the following files from your course webpage: plot_earthquakes.py (skeleton code), earthquakes.csv (data file), and earth.png (background map). Place them in your lab8 folder. The skeleton code already sets up the turtle canvas with the earth image as background, so you can focus on parsing data and plotting.

Reading the CSV File

The earthquakes.csv file contains earthquake records. Each line after the header includes fields like latitude, longitude, magnitude, and date. Your first task is to read this file into a list of dictionaries. Use Python's csv module for simplicity. Remember to skip the first line (header) before reading data. Here's a snippet:

import csv

def read_earthquake_data(filename):
    data = []
    with open(filename, 'r') as f:
        reader = csv.reader(f)
        next(reader)  # skip header
        for row in reader:
            # row: [lat, lon, mag, date, ...]
            record = {
                'latitude': float(row[0]),
                'longitude': float(row[1]),
                'magnitude': float(row[2]),
                'date': row[3]
            }
            data.append(record)
    return data

Parsing Each Row: The parse_row Function

In the skeleton, you'll implement parse_row. This function takes a row (list of strings) and returns a dictionary with keys like lat, lon, mag, and date. Make sure to convert numeric fields to floats. For example:

def parse_row(row):
    return {
        'lat': float(row[0]),
        'lon': float(row[1]),
        'mag': float(row[2]),
        'date': row[3]
    }

Plotting on the Map: Coordinate Conversion

The turtle canvas is 720x360 pixels, with (0,0) at the center. Longitude ranges from -180 to 180, latitude from -90 to 90. To convert (lon, lat) to canvas coordinates, multiply each by 2. For example, (0,0) stays at (0,0); (180,90) becomes (360,180). This scaling maps the entire globe onto the canvas.

Drawing Circles for Earthquakes

For each earthquake, use the turtle's circle method. The radius should vary with magnitude—say, radius = magnitude * 2. You can also color circles: red channel proportional to magnitude, green and blue inversely proportional. Here's how:

import turtle

def draw_earthquake(t, lat, lon, mag):
    x = lon * 2
    y = lat * 2
    t.penup()
    t.goto(x, y)
    t.pendown()
    # color based on magnitude
    red = mag / 10.0
    green = 1 - red
    blue = 1 - red
    t.color(red, green, blue)
    t.begin_fill()
    t.circle(mag * 2)
    t.end_fill()

Putting It All Together in Main

Your main function should set up the turtle, read data, and iterate over records to draw circles. Use the teleport function from Lab 5 to move the turtle without drawing. Here's a skeleton:

def main():
    # Setup turtle
    screen = turtle.Screen()
    screen.setup(720, 360)
    screen.bgpic("earth.png")
    t = turtle.Turtle()
    t.speed(0)
    # Read data
    data = read_earthquake_data("earthquakes.csv")
    # Plot each earthquake
    for quake in data:
        draw_earthquake(t, quake['lat'], quake['lon'], quake['mag'])
    turtle.done()

Testing and Debugging Tips

If circles appear off the map, check your coordinate conversion. Remember that latitude and longitude are in degrees, and the map image is centered at (0,0). Also, ensure you skip the header line. Use print statements to debug if needed.

Going Further: Color by Date

For an extra challenge, color circles based on date instead of magnitude. You could map older earthquakes to blue and recent ones to red. This adds a temporal dimension to your visualization.

Conclusion

You've now built a program that reads real earthquake data and visualizes it on a world map. This lab reinforces file I/O, data structures (list of dictionaries), and graphical plotting. These skills are widely applicable—whether you're analyzing sports statistics, tracking social media trends, or building data visualizations for AI models. Submit your screenshot and code as lab8.zip. Good luck!