Last active
January 8, 2021 17:54
-
-
Save Caellwyn/e8adb7a948ddfc542b2fdb329490bbd5 to your computer and use it in GitHub Desktop.
Plotly basics.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "Plotly basics.ipynb", | |
"private_outputs": true, | |
"provenance": [], | |
"collapsed_sections": [], | |
"authorship_tag": "ABX9TyOtIVTx0PAaXo9XZKEg/Jue", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/Caellwyn/e8adb7a948ddfc542b2fdb329490bbd5/plotly-basics.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "8lDP1DG7n1nM" | |
}, | |
"source": [ | |
"import pandas as pd\r\n", | |
"import plotly.graph_objects as go\r\n", | |
"\r\n", | |
"def get_covid_data(country, region=None, \r\n", | |
" stats = ['ConfirmedCases', 'ConfirmedDeaths']):\r\n", | |
" #downloads data for 'country' and 'region' and returns a pandas dataframe\r\n", | |
" \r\n", | |
" DATA_URL = 'https://raw.githubusercontent.com/OxCGRT/covid-policy-tracker/master/data/OxCGRT_latest.csv'\r\n", | |
" df = pd.read_csv(DATA_URL,\r\n", | |
" parse_dates=['Date'],\r\n", | |
" encoding=\"ISO-8859-1\",\r\n", | |
" dtype={\"RegionName\": str,\r\n", | |
" \"RegionCode\": str,\r\n", | |
" \"CountryName\": str,\r\n", | |
" \"CountryCode\": str},\r\n", | |
" usecols = ['Date','CountryName','RegionName',\r\n", | |
" 'ConfirmedCases','ConfirmedDeaths','Jurisdiction'],\r\n", | |
" index_col = 'Date',\r\n", | |
" error_bad_lines=False)\r\n", | |
" df = df[df['CountryName'] == country]\r\n", | |
"\r\n", | |
" if region:\r\n", | |
" df = df[df['RegionName'] == region]\r\n", | |
" else: \r\n", | |
" df = df[df['Jurisdiction'] == 'NAT_TOTAL']\r\n", | |
" df = df[stats]\r\n", | |
" return df\r\n", | |
"\r\n", | |
"def plot_graph(df, stats, country, region=None):\r\n", | |
" #plots 'stats' from 'df'. \r\n", | |
" #Uses 'country' and 'region' to make appropriate title\r\n", | |
" #returns a plotly figure\r\n", | |
"\r\n", | |
" if region:\r\n", | |
" title = f'Cumulative {\" and \".join(stats)} in {region}, {country}'\r\n", | |
" else:\r\n", | |
" title = f'Cumulative {\" and \".join(stats)} in {country}'\r\n", | |
"\r\n", | |
" fig = go.Figure(layout_title_text = title)\r\n", | |
" for stat in stats:\r\n", | |
" fig.add_trace(go.Scatter(x=df.index, y=df[stat], name=stat))\r\n", | |
" return fig\r\n", | |
"\r\n", | |
"country = 'United States'\r\n", | |
"region = None\r\n", | |
"stats = ['ConfirmedCases','ConfirmedDeaths']\r\n", | |
"\r\n", | |
"#Get the data for 'country' and 'region'\r\n", | |
"df = get_covid_data(country=country, region=region)\r\n", | |
"\r\n", | |
"#create a Plotly figure\r\n", | |
"plot_graph(df=df, country=country, region=region)" | |
], | |
"execution_count": null, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment