In [13]:
import plotly.graph_objs as go
from plotly.offline import init_notebook_mode,iplot,plot
init_notebook_mode(connected=True)
Import pandas and read the csv file: 2014_World_Power_Consumption
In [2]:
import pandas as pd
In [3]:
df = pd.read_csv('2014_World_Power_Consumption')
Check the head of the DataFrame.
In [4]:
df.head()
Out[4]:
Referencing the lecture notes, create a Choropleth Plot of the Power Consumption for Countries using the data and layout dictionary.
In [19]:
data = dict(
type = 'choropleth',
colorscale = 'Viridis',
reversescale = True,
locations = df['Country'],
locationmode = "country names",
z = df['Power Consumption KWH'],
text = df['Country'],
colorbar = {'title' : 'Power Consumption KWH'},
)
layout = dict(title = '2014 Power Consumption KWH',
geo = dict(showframe = False,projection = {'type':'Mercator'})
)
In [20]:
choromap = go.Figure(data = [data],layout = layout)
plot(choromap,validate=False)
Out[20]:
In [7]:
usdf = pd.read_csv('2012_Election_Data')
Check the head of the DataFrame.
In [8]:
usdf.head()
Out[8]:
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 [16]:
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':"Voting-Age Population (VAP)"}
)
In [17]:
layout = dict(title = '2012 General Election Voting Data',
geo = dict(scope='usa',
showlakes = True,
lakecolor = 'rgb(85,173,240)')
)
In [18]:
choromap = go.Figure(data = [data],layout = layout)
plot(choromap,validate=False)
Out[18]: