Browse Source

Plotly and added update_plots.py

pull/79/head
Jure Šorn 4 years ago
parent
commit
9bf27137c2
5 changed files with 125 additions and 15 deletions
  1. 10
      README.md
  2. 10
      index.html
  3. 4
      web/covid_cases.js
  4. 6
      web/covid_deaths.js
  5. 110
      web/update_plots.py

10
README.md

@ -3348,14 +3348,15 @@ Plotly
```python
# $ pip3 install plotly kaleido
from plotly.express import line
<Figure> = line(<DF>, x=<col_name>, y=<col_name>) # Or: line(x=<list>, y=<list>)
<Figure>.write_html/json/image('<path>') # Also: <Figure>.show()
<Figure> = line(<DF>, x=<col_name>, y=<col_name>) # Or: line(x=<list>, y=<list>)
<Figure>.update_layout(margin=dict(t=0, r=0, b=0, l=0)) # Or: paper_bgcolor='rgba(0, 0, 0, 0)'
<Figure>.write_html/json/image('<path>') # Also: <Figure>.show()
```
#### Covid deaths by continent:
![Covid Deaths](web/covid_deaths.png)
<div id="2a950764-39fc-416d-97fe-0a6226a3095f" class="plotly-graph-div" style="height:360px; width:100%;"></div>
<div id="2a950764-39fc-416d-97fe-0a6226a3095f" class="plotly-graph-div" style="height:340px; width:100%;"></div>
```python
covid = pd.read_csv('https://covid.ourworldindata.org/data/owid-covid-data.csv',
@ -3374,9 +3375,10 @@ line(df, x='Date', y='Total Deaths per Million', color='Continent').show()
#### Confirmed covid cases, Dow Jones, gold, and Bitcoin price:
![Covid Cases](web/covid_cases.png)
<div id="e23ccacc-a456-478b-b467-7282a2165921" class="plotly-graph-div" style="height:333px; width:100%;"></div>
<div id="e23ccacc-a456-478b-b467-7282a2165921" class="plotly-graph-div" style="height:315px; width:100%;"></div>
```python
import pandas as pd
import plotly.graph_objects as go
import datetime

10
index.html

@ -2845,11 +2845,12 @@ c <span class="hljs-number">7</span> <span class="hljs-number">8</span>
<div><h2 id="plotly"><a href="#plotly" name="plotly">#</a>Plotly</h2><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install plotly kaleido</span>
<span class="hljs-keyword">from</span> plotly.express <span class="hljs-keyword">import</span> line
&lt;Figure&gt; = line(&lt;DF&gt;, x=&lt;col_name&gt;, y=&lt;col_name&gt;) <span class="hljs-comment"># Or: line(x=&lt;list&gt;, y=&lt;list&gt;)</span>
&lt;Figure&gt;.write_html/json/image(<span class="hljs-string">'&lt;path&gt;'</span>) <span class="hljs-comment"># Also: &lt;Figure&gt;.show()</span>
&lt;Figure&gt; = line(&lt;DF&gt;, x=&lt;col_name&gt;, y=&lt;col_name&gt;) <span class="hljs-comment"># Or: line(x=&lt;list&gt;, y=&lt;list&gt;)</span>
&lt;Figure&gt;.update_layout(margin=dict(t=<span class="hljs-number">0</span>, r=<span class="hljs-number">0</span>, b=<span class="hljs-number">0</span>, l=<span class="hljs-number">0</span>)) <span class="hljs-comment"># Or: paper_bgcolor='rgba(0, 0, 0, 0)'</span>
&lt;Figure&gt;.write_html/json/image(<span class="hljs-string">'&lt;path&gt;'</span>) <span class="hljs-comment"># Also: &lt;Figure&gt;.show()</span>
</code></pre></div>
<div><h4 id="coviddeathsbycontinent">Covid deaths by continent:</h4><p></p><div id="2a950764-39fc-416d-97fe-0a6226a3095f" class="plotly-graph-div" style="height:360px; width:100%;"></div><pre><code class="python language-python hljs">covid = pd.read_csv(<span class="hljs-string">'https://covid.ourworldindata.org/data/owid-covid-data.csv'</span>,
<div><h4 id="coviddeathsbycontinent">Covid deaths by continent:</h4><p></p><div id="2a950764-39fc-416d-97fe-0a6226a3095f" class="plotly-graph-div" style="height:340px; width:100%;"></div><pre><code class="python language-python hljs">covid = pd.read_csv(<span class="hljs-string">'https://covid.ourworldindata.org/data/owid-covid-data.csv'</span>,
usecols=[<span class="hljs-string">'iso_code'</span>, <span class="hljs-string">'date'</span>, <span class="hljs-string">'total_deaths'</span>, <span class="hljs-string">'population'</span>])
continents = pd.read_csv(<span class="hljs-string">'https://datahub.io/JohnSnowLabs/country-and-continent-codes-'</span> + \
<span class="hljs-string">'list/r/country-and-continent-codes-list-csv.csv'</span>,
@ -2864,7 +2865,8 @@ line(df, x=<span class="hljs-string">'Date'</span>, y=<span class="hljs-string">
<div><h4 id="confirmedcovidcasesdowjonesgoldandbitcoinprice">Confirmed covid cases, Dow Jones, gold, and Bitcoin price:</h4><p></p><div id="e23ccacc-a456-478b-b467-7282a2165921" class="plotly-graph-div" style="height:333px; width:100%;"></div><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> plotly.graph_objects <span class="hljs-keyword">as</span> go
<div><h4 id="confirmedcovidcasesdowjonesgoldandbitcoinprice">Confirmed covid cases, Dow Jones, gold, and Bitcoin price:</h4><p></p><div id="e23ccacc-a456-478b-b467-7282a2165921" class="plotly-graph-div" style="height:315px; width:100%;"></div><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> pandas <span class="hljs-keyword">as</span> pd
<span class="hljs-keyword">import</span> plotly.graph_objects <span class="hljs-keyword">as</span> go
<span class="hljs-keyword">import</span> datetime
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>:</span>

4
web/covid_cases.js
File diff suppressed because it is too large
View File

6
web/covid_deaths.js
File diff suppressed because it is too large
View File

110
web/update_plots.py

@ -0,0 +1,110 @@
#!/usr/bin/env python3
#
# Usage: ./update_plots.py
# Updates plots from the Plotly section so they show the latest data.
from pathlib import Path
from datetime import date, time, datetime, timedelta
import pandas as pd
from plotly.express import line
import plotly.graph_objects as go
def main():
print('Updating covid deaths...')
update_covid_deaths()
print('Updating covid cases...')
update_confirmed_cases()
def update_covid_deaths():
def update_readme(date_treshold):
lines = read_file('../README.md')
out = [re.sub("df.date < '\d{4}-\d{2}-\d{2}'", f"df.date < '{date_treshold}'", line)
for line in lines]
write_to_file('../README.md', out)
covid = pd.read_csv('https://covid.ourworldindata.org/data/owid-covid-data.csv',
usecols=['iso_code', 'date', 'total_deaths', 'population'])
continents = pd.read_csv('https://datahub.io/JohnSnowLabs/country-and-continent-codes-' + \
'list/r/country-and-continent-codes-list-csv.csv',
usecols=['Three_Letter_Country_Code', 'Continent_Name'])
df = pd.merge(covid, continents, left_on='iso_code', right_on='Three_Letter_Country_Code')
df = df.groupby(['Continent_Name', 'date']).sum().reset_index()
df['Total Deaths per Million'] = df.total_deaths * 1e6 / df.population
date_treshold = str(date.today() - timedelta(days=2))
df = df[('2020-03-14' < df.date) & (df.date < date_treshold)]
df = df.rename({'date': 'Date', 'Continent_Name': 'Continent'}, axis='columns')
f = line(df, x='Date', y='Total Deaths per Million', color='Continent')
f.update_layout(margin=dict(t=24, b=0), paper_bgcolor='rgba(0, 0, 0, 0)')
update_file('covid_deaths.js', f)
update_readme(date_treshold)
def update_confirmed_cases():
def main():
df = wrangle_data(*scrape_data())
f = get_figure(df)
update_file('covid_cases.js', f)
def scrape_data():
def scrape_yahoo(id_):
BASE_URL = 'https://query1.finance.yahoo.com/v7/finance/download/'
now = int(datetime.now().timestamp())
url = f'{BASE_URL}{id_}?period1=1579651200&period2={now}&interval=1d&events=history'
return pd.read_csv(url, usecols=['Date', 'Close']).set_index('Date').Close
covid = pd.read_csv('https://covid.ourworldindata.org/data/owid-covid-data.csv',
usecols=['date', 'total_cases'])
covid = covid.groupby('date').sum()
dow, gold, bitcoin = [scrape_yahoo(id_) for id_ in ('^DJI', 'GC=F', 'BTC-USD')]
dow.name, gold.name, bitcoin.name = 'Dow Jones', 'Gold', 'Bitcoin'
return covid, dow, gold, bitcoin
def wrangle_data(covid, dow, gold, bitcoin):
df = pd.concat([dow, gold, bitcoin], axis=1)
df = df.sort_index().interpolate()
df = df.rolling(10, min_periods=1, center=True).mean()
df = df.loc['2020-02-23':].iloc[:-2]
df = (df / df.iloc[0]) * 100
return pd.concat([covid, df], axis=1, join='inner')
def get_figure(df):
def get_trace(col_name):
return go.Scatter(x=df.index, y=df[col_name], name=col_name, yaxis='y2')
traces = [get_trace(col_name) for col_name in df.columns[1:]]
traces.append(go.Scatter(x=df.index, y=df.total_cases, name='Total Cases', yaxis='y1'))
figure = go.Figure()
figure.add_traces(traces)
figure.update_layout(
yaxis1=dict(title='Total Cases', rangemode='tozero'),
yaxis2=dict(title='%', rangemode='tozero', overlaying='y', side='right'),
legend=dict(x=1.1),
margin=dict(t=24, b=0),
paper_bgcolor='rgba(0, 0, 0, 0)'
)
return figure
main()
def update_file(filename, figure):
lines = read_file(filename)
out = lines[:6] + [f' {figure.to_json()}\n', ' )\n', '};\n']
write_to_file(filename, out)
###
## UTIL
#
def read_file(filename):
with open(filename, encoding='utf-8') as file:
return file.readlines()
def write_to_file(filename, lines):
with open(filename, 'w', encoding='utf-8') as file:
file.writelines(lines)
if __name__ == '__main__':
main()
Loading…
Cancel
Save