Code
import pandas as pd
import numpy as np
import matplotlib.pyplot as mlp
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
from urllib.request import urlopen
import json
Rebekah Chuang
January 15, 2023
# Create dataframes using pandas. Merged and dropped irrelevant columns after viewing the csv in Excel and we have to use "latin-1" encoding for ANSI for the model_county
national_data = pd.read_csv('https://raw.githubusercontent.com/washingtonpost/data-2C-beyond-the-limit-usa/main/data/processed/climdiv_national_year.csv', dtype={"fips": str})
state_data = pd.merge(pd.read_csv('https://raw.githubusercontent.com/washingtonpost/data-2C-beyond-the-limit-usa/main/data/processed/climdiv_state_year.csv', dtype={"fips": str}), pd.read_csv('https://raw.githubusercontent.com/washingtonpost/data-2C-beyond-the-limit-usa/main/data/processed/model_state.csv', dtype={"fips": str}), on ='fips')
county_data = pd.merge(pd.read_csv('https://raw.githubusercontent.com/washingtonpost/data-2C-beyond-the-limit-usa/main/data/processed/climdiv_county_year.csv', dtype={"fips": str}), pd.read_csv('https://raw.githubusercontent.com/washingtonpost/data-2C-beyond-the-limit-usa/main/data/processed/model_county.csv', encoding = "latin-1", dtype={"fips": str}), on ='fips')
with urlopen('https://raw.githubusercontent.com/PublicaMundi/MappingAPI/master/data/geojson/us-states.json') as response:
states = json.load(response)
fig = px.choropleth_mapbox(state_data,
geojson=states,
locations='fips',
color='temp',
color_continuous_scale = 'sunset',
range_color=(35, 75),
labels = {'temp':'Temperature'},
hover_name = 'STATE_NAME',
hover_data = ["Annual"],
mapbox_style = 'open-street-map',
zoom= 3,
center = {'lat' : 37.0902, 'lon' : -95.7129},
opacity = 0.8,
animation_frame='year')
fig.update_layout(
margin={"r":0,"t":0,"l":0,"b":0},
geo_scope='usa',
)
fig.show()