Introduction to Python Data Visualization 6 - Exercises

힘센캥거루
2025년 10월 8일(수정됨)
2
12

This time, it's a data visualization exercise.

Wouldn't it be boring to use the same data again?

Let's use new data.

1. File and Problem

The file is annual crime occurrence and arrest statistics obtained from the public data portal.

Let's draw a graph according to the conditions below.

  • Visualize the occurrence and arrest counts for hacking (data breaches)

  • Set the title, x-axis label, y-axis label, and legend

  • Change the line style freely

  • Other styles are free

Introduction to Python Data Visualization 6 - Exercises-1

2. Problem Solution

First, let's copy the initial content from the previous post. Here, the only change is the file path.

import pandas as pd

# Call modules and set Korean font
import matplotlib.pyplot as plt
import matplotlib

# Font settings for MacOS
# matplotlib.rcParams["font.family"] = "AppleGothic"

# Font settings for Windows
matplotlib.rcParams["font.family"] = "Malgun Gothic"

# Set font size
matplotlib.rcParams["font.size"] = 13

# Solve negative output problem
plt.rcParams['axes.unicode_minus'] = False

crime = pd.read_excel("./crime_statistics.xlsx")
crime.head(3)

When you check the data, you can see that the years are duplicated twice according to the category.

Introduction to Python Data Visualization 6 - Exercises-2

Remove duplicate years using unique().

And extract the occurrence and arrest counts for hacking using loc and conditional statements to draw a graph.

x = crime.loc[:,"Year"].unique()
y1 = crime.loc[crime.Category=="Occurrences","Hacking (Data Breach)"]
y2 = crime.loc[crime.Category=="Arrests","Hacking (Data Breach)"]
plt.plot(x,y1)
plt.plot(x,y2)
Introduction to Python Data Visualization 6 - Exercises-3

Let's add a legend and title to the graph.

x = crime.loc[:,"Year"].unique()
y1 = crime.loc[crime.Category=="Occurrences","Hacking (Data Breach)"]
y2 = crime.loc[crime.Category=="Arrests","Hacking (Data Breach)"]
plt.plot(x,y1,label="Occurrences") # <-- added
plt.plot(x,y2,label="Arrests") # <-- added
plt.legend() # <-- added
plt.title("Hacking (Data Breach) Occurrences and Arrests") # <-- added
Introduction to Python Data Visualization 6 - Exercises-4

And also set axis names.

x = crime.loc[:,"Year"].unique()
y1 = crime.loc[crime.Category=="Occurrences","Hacking (Data Breach)"]
y2 = crime.loc[crime.Category=="Arrests","Hacking (Data Breach)"]
plt.plot(x,y1,label="Occurrences")
plt.plot(x,y2,label="Arrests")
plt.legend()
plt.xlabel("Year") # <-- added
plt.ylabel("Count") # <-- added
plt.title("Hacking (Data Breach) Occurrences and Arrests")
Introduction to Python Data Visualization 6 - Exercises-5

Finally, specify the line style and add a grid.

x = crime.loc[:,"Year"].unique()
y1 = crime.loc[crime.Category=="Occurrences","Hacking (Data Breach)"]
y2 = crime.loc[crime.Category=="Arrests","Hacking (Data Breach)"]
plt.plot(x,y1,"^--",label="Occurrences") # <-- added
plt.plot(x,y2,"o-",label="Arrests") # <-- added
plt.legend()
plt.xlabel("Year")
plt.ylabel("Count")
plt.title("Hacking (Data Breach) Occurrences and Arrests")
plt.grid() # <-- added
Introduction to Python Data Visualization 6 - Exercises-6

3. Conclusion

In the next post, I plan to discuss how to draw multiple graphs at once in Matplotlib.

I hope this helps you much in practicing data visualization.

관련 글

Automating School Work – Using AI to Check Subject-Specific Remarks in Student Records
Automating School Work – Using AI to Check Subject-Specific Remarks in Student Records
If I had to pick the most meaningless, exhausting, and boring task at school, I would choose checking student records.In middle school, the student re...
Book Review and Challenge Review of Chapter 7 of *Building an LLM from Scratch*
Book Review and Challenge Review of Chapter 7 of *Building an LLM from Scratch*
Chapter 7 covers the process of fine-tuning a model to follow instructions.In other words, making it give the desired response to a given question.As...
Review of Chapter 6 of *Build an LLM from Scratch*
Review of Chapter 6 of *Build an LLM from Scratch*
Chapter 6 is about fine-tuning for classification.The example used is building a spam classifier.A spam classifier determines whether something is spa...
Review of Chapter 5 of *Building an LLM from Scratch*
Review of Chapter 5 of *Building an LLM from Scratch*
Today is December 14.The challenge period actually ended two weeks ago, but I couldn’t just give up on writing a review.Because these TILs I leave lik...
Impressions After Reading Chapter 4 of “LLM From Scratch”
Impressions After Reading Chapter 4 of “LLM From Scratch”
Today is November 26, so if I finish one chapter a day, I’ll complete the challenge.I’m not sure if I can do it with my first and second kids constant...
Review of Chapter 3 of Learning LLM from Scratch
Review of Chapter 3 of Learning LLM from Scratch
After spilling a bucket of water on my MacBook, I was in shock and wasted about 3-4 days. In retrospect, since my MacBook was already damaged, I should have thought of it as being sent for repair and done something. Anyway, although it's a bit late, I am determined to see it through and leave a review of Chapter 3. 1. Attention Mechanism Chapter 3...

댓글을 불러오는 중...