Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Use Kiel's OpenData to visualize traffic accidents with Python and Pandas
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Traffic Accidents in Kiel, Germany
Python 3.6
last edited: May 19th, 2021
The data is provided by the City of Kiel, Germany
https://www.kiel.de/
"""
import requests
import matplotlib.pyplot as plt
import pandas as pd
import io
CSV_URL = 'https://www.kiel.de/opendata/kiel_gesetze_justiz_strassenverkehsunfaelle_verkehrstote_verletzte_fahrerflucht.csv'
csv_data = requests.get(CSV_URL).content
df = pd.read_csv(io.StringIO(csv_data.decode('utf-8')), sep=';')
# Show first five rows
# print(df.head())
fig, ax = plt.subplots()
x = df['Jahr']
y = df['Unfaelle']
plt.title("Traffic Accidents in Kiel", size="x-large")
plt.ylabel("Quantity", size="x-large")
plt.xlabel("Year", size="x-large")
plt.plot(y, "*-", markersize=6, linewidth=1, color='b', label="Unfälle")
plt.legend(loc=(0.4, 0.8))
ax.set_xticks(range(len(x)))
ax.set_xticklabels(x, rotation='vertical')
plt.show()
# Save file
# plt.savefig('kiel_accidents.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment