Last active
June 19, 2021 16:35
-
-
Save niftycode/76ad1c04ed4b613b0063f35a347a1960 to your computer and use it in GitHub Desktop.
Use Kiel's OpenData to visualize traffic accidents with Python and Pandas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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