<jupyter_start><jupyter_text>COVID-19 in the United StatesIllustrating the growth of COVID-19 cases in the United States, using dataaggregated by the New York Times (https://github.com/nytimes/covid-19-data)<jupyter_code># Import libraries
import geopandas as gpd
import pandas as pd
import geoplot as gplt
import matplotlib.pyplot as plt

import datetime
# Analysis hyperparameters
state = 'California'
# Load in data
states_df = pd.read_csv('https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-states.csv')
# Change date strings to datetime objects
states_df.date = pd.to_datetime(states_df.date, format='%Y-%m-%d')
states_df.head()

groupby_states = states_df.groupby('state')

with plt.style.context('ggplot'):
    fig, ax = plt.subplots()
    groupby_states.plot.line(x='date', y='cases', ax=ax)
# Plot cumulative cases
state_df = states_df[states_df.state == state]
with plt.style.context('ggplot'):
    state_df.plot.line(x='date', y='cases')
    plt.title('Cumulative cases: {}'.format(state))
# TODO: Plot daily-increase cases
state_df = states_df[states_df.state == state]
with plt.style.context('ggplot'):
    # recent_state_df = state_df[datetime.datetime(2020, 4, 1) <= state_df.date]
    # recent_state_df.plot.bar(x='date', y='cases')
    state_df.plot.bar(x='date', y='cases')
    # plt.xticks(ticks=[datetime.datetime(2020, 4, 1), datetime.datetime(2020, 5, 15)])
# Grab data from the most recent date.
recent_state_df = groupby_states.apply(lambda row: row.loc[row.date.idxmax()])

with plt.style.context('ggplot'):
    recent_state_df.plot.bar(y='cases')
    plt.title('Cases by state')<jupyter_output><empty_output><jupyter_text>Plot overlay on map<jupyter_code># Based on example: https://residentmario.github.io/geoplot/gallery/plot_usa_city_elevations.html
contiguous_usa = gpd.read_file(gplt.datasets.get_path('contiguous_usa'))

proj = gplt.crs.AlbersEqualArea(central_longitude=-98, central_latitude=39.5)
f, ax = plt.subplots(figsize=(12, 8), subplot_kw={'projection': proj})
# now... change facecolor plots differ b
polyplot_kwargs = {'facecolor': (0.9, 0.9, 0.9),
                   'linewidth': 0.2}

gplt.polyplot(contiguous_usa.geometry, ax=ax, **polyplot_kwargs)
# Try out GeoPandas
df = gpd.read_file(gpd.datasets.get_path('nybb'))
ax = df.plot(figsize=(10, 10), alpha=0.5, edgecolor='k')
df.head()
# contiguous_usa
# gplt.datasets.available
# gplt.datasets.get_path('contiguous_usa')
usa = gpd.read_file('https://eric.clst.org/assets/wiki/uploads/Stuff/gz_2010_us_040_00_20m.json')
# Change some of the column names to match above
usa.rename(columns={'NAME' : 'state'}, inplace=True)
usa.set_index('state', inplace=True)
usa

proj = gplt.crs.AlbersEqualArea(central_longitude=-98, central_latitude=39.5)
f, ax = plt.subplots(figsize=(12, 8), subplot_kw={'projection': proj})
# now... change facecolor plots differ b
polyplot_kwargs = {'facecolor': (0.9, 0.9, 0.9),
                   'linewidth': 0.2}

gplt.polyplot(usa,
              edgecolor='white',
              ax=ax, **polyplot_kwargs)
# Get ready to merge datasets
# Merge datasets
merged_df = usa.merge(recent_state_df, left_index=True, right_index=True, how='outer')
merged_df.head()
mask = merged_df.index.isin(contiguous_usa.state)
mask
# In theory, all dates should be the same.
# Report the most common date as most-recent data
date = merged_df.date.max()
str(date.date())
import matplotlib.colors as colors

with plt.style.context('ggplot'):
    plt.figure(figsize=(12,8))
    gplt.choropleth(
        merged_df[mask], hue='cases', # scheme=scheme,
        projection=proj,
        cmap='viridis',
        # legend=True, # This causes a type error: unclear why
        norm=colors.LogNorm(vmin=merged_df.cases.min(), vmax=merged_df.cases.max())
    )
    plt.title('Contiguous USA, colored by COVID-19 cases\n' +
              str(date.date()))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14575835475578405, 'fraction_numerical': 0.02082262210796915, 'mean_word_length': 4.833583208395802, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 9, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '347204', 'license': 'MIT', 'n_tokens_mistral': 1459, 'n_tokens_neox': 1382, 'n_words': 281, 'path': 'plotting/covid_usa.ipynb', 'repo_name': 'charlesincharge/python-examples', 'size': '3890'}