Willkommen zur Flächenkarten (en. choropleth) Übung. In dieser Übung werden wir anhand von einfachen vorgegebenen Datensätzen Flächenkarten erzeugen. Plotly erlaubt es uns leider nicht Beispiellösungen innerhalb des Notebooks vorgeben.
Schaut dafür bitte nach der Bearbeitung in die Lösung, führt den Code aus und vergleicht anschließend mit eurer Lösung. Euren Code könnt ihr dann selbstständig oder mit Rückgriff auf die Lösung anpassen.
import plotly.graph_objs as go
from plotly.offline import init_notebook_mode,iplot,plot
init_notebook_mode(connected=True)
Importiere Pandas und lese die CSV-Daten: 2014_World_Power_Consumption
import pandas as pd
df = pd.read_csv("2014_World_Power_Consumption")
Kontrolliere die ersten Einträge im DataFrame.
df.head()
Country | Power Consumption KWH | Text | |
---|---|---|---|
0 | China | 5.523000e+12 | China 5,523,000,000,000 |
1 | United States | 3.832000e+12 | United 3,832,000,000,000 |
2 | European | 2.771000e+12 | European 2,771,000,000,000 |
3 | Russia | 1.065000e+12 | Russia 1,065,000,000,000 |
4 | Japan | 9.210000e+11 | Japan 921,000,000,000 |
Unter Rückgriff auf die Notizen der Lektion: Erstelle ein Flächendiagramm der Energienutzung (en. power consumption) der Länder durch Verwendung von Daten- und Layout-Dictionary.
data = dict(
type = 'choropleth',
colorscale = 'Viridis',
reversescale = True,
locations = df['Country'],
locationmode = "country names",
z = df['Power Consumption KWH'],
text = df['Country'],
colorbar = {'title' : 'Energienutzungn KWH'},
)
layout = dict(title = '2014 Energienutzung KWH',
geo = dict(showframe = False,projection = {'type':'Mercator'})
)
choromap = go.Figure(data = [data],layout = layout)
iplot(choromap,validate=False)
Importiere die 2012_Election_Data CSV-Datei indem du Pandas nutzt.
usdf = pd.read_csv("2012_Election_Data")
Kontrolliere die ersten Einträge im neuen DataFrame.
usdf.head()
Year | ICPSR State Code | Alphanumeric State Code | State | VEP Total Ballots Counted | VEP Highest Office | VAP Highest Office | Total Ballots Counted | Highest Office | Voting-Eligible Population (VEP) | Voting-Age Population (VAP) | % Non-citizen | Prison | Probation | Parole | Total Ineligible Felon | State Abv | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 2012 | 41 | 1 | Alabama | NaN | 58.6% | 56.0% | NaN | 2,074,338 | 3,539,217 | 3707440.0 | 2.6% | 32,232 | 57,993 | 8,616 | 71,584 | AL |
1 | 2012 | 81 | 2 | Alaska | 58.9% | 58.7% | 55.3% | 301,694 | 300,495 | 511,792 | 543763.0 | 3.8% | 5,633 | 7,173 | 1,882 | 11,317 | AK |
2 | 2012 | 61 | 3 | Arizona | 53.0% | 52.6% | 46.5% | 2,323,579 | 2,306,559 | 4,387,900 | 4959270.0 | 9.9% | 35,188 | 72,452 | 7,460 | 81,048 | AZ |
3 | 2012 | 42 | 4 | Arkansas | 51.1% | 50.7% | 47.7% | 1,078,548 | 1,069,468 | 2,109,847 | 2242740.0 | 3.5% | 14,471 | 30,122 | 23,372 | 53,808 | AR |
4 | 2012 | 71 | 5 | California | 55.7% | 55.1% | 45.1% | 13,202,158 | 13,038,547 | 23,681,837 | 28913129.0 | 17.4% | 119,455 | 0 | 89,287 | 208,742 | CA |
Erstelle jetzt eine Flächenkarte, die die wahlberechtigte Bevölkerung (en. voting-age population (VAP)) pro Staat zeigt.
data = dict(type='choropleth',
colorscale = 'Viridis',
reversescale = True,
locations = usdf['State Abv'],
z = usdf['Voting-Age Population (VAP)'],
locationmode = 'USA-states',
text = usdf['State'],
marker = dict(line = dict(color = 'rgb(255,255,255)',width = 1)),
colorbar = {'title':"Wahlberechtigte Bevölkerung (VAP)"}
)
layout = dict(title = '2012 Allgemeine Wahldaten',
geo = dict(scope='usa',
showlakes = True,
lakecolor = 'rgb(85,173,240)')
)
choromap = go.Figure(data = [data],layout = layout)
iplot(choromap,validate=False)