Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Challenge: Map Local News Events | Data Analysis and Visualization for Media
/
Python for Journalists and Media
Sektion 2. Kapitel 7
single

single

bookChallenge: Map Local News Events

Stryg for at vise menuen

Mapping local news events is a powerful way to tell stories visually, allowing audiences to quickly grasp the scope, distribution, and types of incidents occurring in their communities. By placing events on a map according to their geographic coordinates, you can reveal patterns and clusters that might otherwise go unnoticed in raw data tables. This approach is especially valuable for journalists aiming to contextualize news, highlight hotspots, or draw attention to trends across neighborhoods or regions.

12345678910111213141516171819202122232425262728
import pandas as pd import matplotlib.pyplot as plt # Sample DataFrame of local news events data = { "event": ["Burglary", "Town Hall", "Charity Run", "Robbery", "Election Rally", "Food Drive"], "type": ["crime", "politics", "community", "crime", "politics", "community"], "latitude": [40.7128, 40.7135, 40.7150, 40.7142, 40.7130, 40.7160], "longitude": [-74.0060, -74.0050, -74.0070, -74.0045, -74.0080, -74.0030] } df = pd.DataFrame(data) # Assign a color to each event type color_map = {"crime": "red", "politics": "blue", "community": "green"} df["color"] = df["type"].map(color_map) # Plotting the events plt.figure(figsize=(8, 6)) for event_type in df["type"].unique(): subset = df[df["type"] == event_type] plt.scatter(subset["longitude"], subset["latitude"], c=subset["color"], label=event_type.capitalize(), s=100, edgecolor="black") plt.title("Local News Events Map") plt.xlabel("Longitude") plt.ylabel("Latitude") plt.legend(title="Event Type") plt.show()
copy

Using color-coding for different event types makes the map much easier to interpret at a glance. When each category—such as crime, politics, or community—is assigned a distinct color, you can quickly identify clusters, outliers, or locations where certain types of events are more common. This not only improves the visual appeal of your map but also helps your audience draw meaningful conclusions from the data.

Opgave

Swipe to start coding

Write a function that takes a DataFrame of local news events and produces a scatter plot map.

  • Plot each event as a point using its longitude and latitude.
  • Assign a unique color to each event type category.
  • Include a legend that explains the color for each event type.
  • Add a title to the map.

Løsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 7
single

single

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

some-alt