From ca27357e15f2319f60ea705980882b237ac6019e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Sat, 4 Mar 2023 13:11:14 +0100 Subject: [PATCH 001/448] Plotly --- README.md | 10 +++++----- index.html | 14 +++++++------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 75f701c37..4c446ce29 100644 --- a/README.md +++ b/README.md @@ -3445,8 +3445,7 @@ line(df, x='Date', y='Total Deaths per Million', color='Continent').show()
```python -import pandas as pd -import plotly.graph_objects as go +import pandas as pd, plotly.graph_objects as go def main(): display_data(wrangle_data(*scrape_data())) @@ -3457,8 +3456,8 @@ def scrape_data(): 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}' + \ - '?period1=1579651200&period2=9999999999&interval=1d&events=history' + url = (f'https://query1.finance.yahoo.com/v7/finance/download/{slug}?' + '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') @@ -3483,7 +3482,8 @@ def display_data(df): yaxis2=dict(title='%', rangemode='tozero', overlaying='y', side='right'), legend=dict(x=1.1), height=450 - ).show() + ) + figure.show() if __name__ == '__main__': main() diff --git a/index.html b/index.html index c762fb953..662a1e6c8 100644 --- a/index.html +++ b/index.html @@ -54,7 +54,7 @@
- +
@@ -2807,8 +2807,7 @@

Format

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

import pandas as pd
-import 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()))
@@ -2819,8 +2818,8 @@ 

Format

'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}' + \ - '?period1=1579651200&period2=9999999999&interval=1d&events=history' + url = (f'https://query1.finance.yahoo.com/v7/finance/download/{slug}?' + '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') @@ -2845,7 +2844,8 @@

Format

'%', rangemode='tozero', overlaying='y', side='right'), legend=dict(x=1.1), height=450 - ).show() + ) + figure.show() if __name__ == '__main__': main() @@ -2932,7 +2932,7 @@

Format

<array> = np.copy/abs/sqrt/log/int64(<array>)           # Returns new array of the same shape.
 <array> = <array>.sum/max/mean/argmax/all([axis])       # Passed dimension gets aggregated.
 <array> = np.apply_along_axis(<func>, axis, <array>)    # Func can return a scalar or array.
 
+
<array> = np.concatenate(<list_of_arrays>, axis=0)      # Links arrays along first axis (rows).
+<array> = np.row_stack/column_stack(<list_of_arrays>)   # Treats 1d arrays as rows or columns.
+<array> = np.tile(<array>, <int/shape>)                 # Multiplies passed array.
+
  • Shape is a tuple of dimension sizes. A 100x50 RGB image has shape (50, 100, 3).
  • Axis is an index of the dimension that gets aggregated. Leftmost dimension has index 0. Summing the RGB image along axis 2 will return a greyscale image with shape (50, 100).
  • -
  • Passing a tuple of axes will chain the operations like this: '<array>.<method>(axis_1).<method>(axis_2 - 1 if axis_2 > axis_1 else axis_2)'.

Indexing

<el>       = <2d_array>[row_index, column_index]        # <3d_a>[table_i, row_i, column_i]
 <1d_view>  = <2d_array>[row_index]                      # <3d_a>[table_i, row_i]
@@ -2787,9 +2787,9 @@ 

Format

#Plotly

# $ 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>.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()
+<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), …)  # `paper_bgcolor='rgb(0, 0, 0)'`.
+<Figure>.write_html/json/image('<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',
@@ -2932,7 +2932,7 @@ 

Format