In [10]:
import plotly.graph_objs as go
from plotly.offline import init_notebook_mode,iplot
init_notebook_mode(connected=True)
Import pandas and read the csv file: 2014_World_Power_Consumption
In [11]:
import pandas as pd
In [12]:
df = pd.read_csv('2014_World_Power_Consumption')
Check the head of the DataFrame.
In [13]:
df.head()
Out[13]:
Referencing the lecture notes, create a Choropleth Plot of the Power Consumption for Countries using the data and layout dictionary.
In [30]:
data = dict(type ='choropleth',
locations=df['Country'],
colorscale = 'Viridis',
reversescale='True',
locationmode= 'country names',
z=df['Power Consumption KWH'],
text=df['Country'],
colorbar = {'title': 'Power Consumption in KiloWatt Hours'})
In [31]:
layout = dict(title='2014 Power Consumption',
geo = dict(showframe=False,projection={'type':'Mercator'}))
In [32]:
choromap = go.Figure(data = [data],layout = layout)
iplot(choromap,validate=False)
In [33]:
df2 = pd.read_csv('2012_Election_Data')
Check the head of the DataFrame.
In [34]:
df2.head()
Out[34]:
Now create a plot that displays the Voting-Age Population (VAP) per state. If you later want to play around with other columns, make sure you consider their data type. VAP has already been transformed to a float for you.
In [39]:
data = dict(type = 'choropleth',
colorscale='Viridis',
locations = df2['State Abv'],
z = df2['Voting-Age Population (VAP)'],
locationmode = 'USA-states',
text = df2['State'],
colorbar = {'title': 'Voting Age Population'})
In [40]:
layout = dict(title = 'Voting Age Population Per State',
geo = dict(scope='usa',showlakes=True,lakecolor='rgb(85.173.240)'))
In [41]:
choromap = go.Figure(data = [data],layout = layout)
iplot(choromap,validate=False)