From 58ad56cb80be2d1073c31b31733b9cb40ec07f3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Sun, 12 Nov 2023 17:13:01 +0100 Subject: [PATCH] Plotly --- README.md | 20 +++++++++++--------- index.html | 24 +++++++++++++----------- web/covid_cases.js | 2 +- 3 files changed, 25 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 24e97c6..b9e7c5d 100644 --- a/README.md +++ b/README.md @@ -3428,7 +3428,7 @@ import pandas as pd, plotly.express as ex #### Displays a line chart of total coronavirus deaths per million grouped by continent: ![Covid Deaths](web/covid_deaths.png) -
+
```python covid = pd.read_csv('https://covid.ourworldindata.org/data/owid-covid-data.csv', @@ -3447,25 +3447,26 @@ ex.line(df, x='Date', y='Total Deaths per Million', color='Continent').show() #### Displays a multi-axis line chart of total coronavirus cases and changes in prices of Bitcoin, Dow Jones and gold: ![Covid Cases](web/covid_cases.png) -
+
```python import pandas as pd, plotly.graph_objects as go def main(): - display_data(wrangle_data(*scrape_data())) + covid, bitcoin, gold, dow = scrape_data() + display_data(wrangle_data(covid, bitcoin, gold, dow)) def scrape_data(): - def scrape_covid(): + def get_covid_cases(): url = 'https://covid.ourworldindata.org/data/owid-covid-data.csv' df = pd.read_csv(url, usecols=['location', 'date', 'total_cases']) return df[df.location == 'World'].set_index('date').total_cases - def scrape_yahoo(slug): - url = (f'https://query1.finance.yahoo.com/v7/finance/download/{slug}?' + def get_ticker(symbol): + url = (f'https://query1.finance.yahoo.com/v7/finance/download/{symbol}?' 'period1=1579651200&period2=9999999999&interval=1d&events=history') df = pd.read_csv(url, usecols=['Date', 'Close']) return df.set_index('Date').Close - out = scrape_covid(), scrape_yahoo('BTC-USD'), scrape_yahoo('GC=F'), scrape_yahoo('^DJI') + out = get_covid_cases(), get_ticker('BTC-USD'), get_ticker('GC=F'), get_ticker('^DJI') return map(pd.Series.rename, out, ['Total Cases', 'Bitcoin', 'Gold', 'Dow Jones']) def wrangle_data(covid, bitcoin, gold, dow): @@ -3485,8 +3486,9 @@ def display_data(df): figure.update_layout( yaxis1=dict(title='Total Cases', rangemode='tozero'), yaxis2=dict(title='%', rangemode='tozero', overlaying='y', side='right'), - legend=dict(x=1.1), - height=450 + legend=dict(x=1.08), + width=944, + height=423 ) figure.show() diff --git a/index.html b/index.html index 0698207..5c5251f 100644 --- a/index.html +++ b/index.html @@ -54,7 +54,7 @@
- +
@@ -2786,7 +2786,7 @@ c 7 8 '<path>') # Also <Figure>.show(). -

Displays a line chart of total coronavirus deaths per million grouped by continent:

covid = pd.read_csv('https://covid.ourworldindata.org/data/owid-covid-data.csv',
+

Displays a line chart of total coronavirus deaths per million grouped by continent:

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://gist.githubusercontent.com/stevewithington/20a69c0b6d2ff'
                          '846ea5d35e5fc47f26c/raw/country-and-continent-codes-list-csv.csv',
@@ -2801,22 +2801,23 @@ ex.line(df, x='Date', y=Displays a multi-axis line chart of total coronavirus cases and changes in prices of Bitcoin, Dow Jones and gold:

import pandas as pd, plotly.graph_objects as go
+

Displays a multi-axis line chart of total coronavirus cases and changes in prices of Bitcoin, Dow Jones and gold:

import pandas as pd, plotly.graph_objects as go
 
 def main():
-    display_data(wrangle_data(*scrape_data()))
+    covid, bitcoin, gold, dow = scrape_data()
+    display_data(wrangle_data(covid, bitcoin, gold, dow))
 
 def scrape_data():
-    def scrape_covid():
+    def get_covid_cases():
         url = 'https://covid.ourworldindata.org/data/owid-covid-data.csv'
         df = pd.read_csv(url, usecols=['location', 'date', 'total_cases'])
         return df[df.location == 'World'].set_index('date').total_cases
-    def scrape_yahoo(slug):
-        url = (f'https://query1.finance.yahoo.com/v7/finance/download/{slug}?'
+    def get_ticker(symbol):
+        url = (f'https://query1.finance.yahoo.com/v7/finance/download/{symbol}?'
                'period1=1579651200&period2=9999999999&interval=1d&events=history')
         df = pd.read_csv(url, usecols=['Date', 'Close'])
         return df.set_index('Date').Close
-    out = scrape_covid(), scrape_yahoo('BTC-USD'), scrape_yahoo('GC=F'), scrape_yahoo('^DJI')
+    out = get_covid_cases(), get_ticker('BTC-USD'), get_ticker('GC=F'), get_ticker('^DJI')
     return map(pd.Series.rename, out, ['Total Cases', 'Bitcoin', 'Gold', 'Dow Jones'])
 
 def wrangle_data(covid, bitcoin, gold, dow):
@@ -2836,8 +2837,9 @@ ex.line(df, x='Date', y='Total Cases', rangemode='tozero'),
         yaxis2=dict(title='%', rangemode='tozero', overlaying='y', side='right'),
-        legend=dict(x=1.1),
-        height=450
+        legend=dict(x=1.08),
+        width=944,
+        height=423
     )
     figure.show()
 
@@ -2924,7 +2926,7 @@ $ deactivate                  # Deactivates the activ
  
 
   
 
diff --git a/web/covid_cases.js b/web/covid_cases.js
index 9e0deff..d8e55e8 100644
--- a/web/covid_cases.js
+++ b/web/covid_cases.js
@@ -6224,7 +6224,7 @@ if (document.getElementById("e23ccacc-a456-478b-b467-7282a2165921")) {
               "side": "right"
             },
             "legend": {
-              "x": 1.1
+              "x": 1.08
             },
             "margin": {
               "t": 24,