Plotly - Advanced
For Example: Line Plots
📘 Download World Earthquake Data From 1906-2022
Line Plots with plotly.express
The plotly.express module (usually imported as px) contains functions that can create entire figures at once.
Plotly Express is a built-in part of the plotly library, and is the recommended starting point for creating most common figures.
1 | import pandas as pd |
- Explanation
Pandas
groupby
splits all the records from your data set into different categories or groups and offers you flexibility to analyze the data by these groups.1
2
3
4
5
6
7
8# Group by 'Year', and get the maximum of 'mag' column
df.groupby(['Year'])['mag'].max()
# Group by 'Year', get the maximum of 'mag' column, and fit the length of dataframe
df.groupby(['Year'])['mag'].transform(max)
# Query '2022 >= Year >= 2021' and get the value of 'Max_mag' column
df.query("2022 >= Year >= 2021")["Max_mag"]
Line Plots with Column Encoding Color
1 | import pandas as pd |
- Explanation
Pandas
groupby
splits all the records from your data set into different categories or groups and offers you flexibility to analyze the data by these groups.1
2
3
4
5
6
7
8# Group by 'Year' and 'magType', and get the maximum of 'mag' column
df.groupby(['Year', 'magType'])['mag'].max()
# Group by 'Year' and 'magType', get the maximum of 'mag' column, and fit the length of dataframe
df.groupby(['Year', 'magType'])['mag'].transform(max)
# Query '2022 >= Year >= 2021' and get the value of 'Max_mag' column
df.query("2022 >= Year >= 2021")["Max_mag"]
Basic Settings
Line charts with Markers
Set Title
title = 'Max magnitude and Min depth from 2012 to 2022'
Add one more field on text label
text = "Min_depth"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16import pandas as pd
import plotly.express as px
df = pd.read_csv('data.csv')
# Pre-processing
df['Year'] = pd.to_datetime(df['time']).dt.year
df["Country"] = df["place"].str.split(pat=',', expand=False).str.get(-1)
df["Max_mag"] = df.groupby(['Year', 'magType'])['mag'].transform(max)
df["Min_depth"] = df.groupby(['Year', 'magType'])['depth'].transform(min)
query_df = df.query("2022 >= Year >= 2012 & magType in ['mw', 'ml', 'ms', 'mb']")
fig = px.line(query_df, x="Year", y="Max_mag",
title='Max magnitude and Min depth from 2012 to 2022',
color='magType', text="Min_depth")
fig.show()
Mark data points
Method 1:
markers = True
Method 2:
symbol = "magType"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23import pandas as pd
import plotly.express as px
df = pd.read_csv('data.csv')
# Pre-processing
df['Year'] = pd.to_datetime(df['time']).dt.year
df["Country"] = df["place"].str.split(pat=',', expand=False).str.get(-1)
df["Max_mag"] = df.groupby(['Year', 'magType'])['mag'].transform(max)
query_df = df.query("2022 >= Year >= 2012")
# Method 1: markers
fig = px.line(query_df, x="Year", y="Max_mag",
title='Max magnitude from 2012 to 2022',
color='magType', markers=True)
fig.show()
# Method 2: symbol
fig = px.line(query_df, x="Year", y="Max_mag",
title='Max magnitude from 2012 to 2022',
color='magType', symbol="magType")
fig.show()
Save a Plot
1 | # Install Package |
Save a Figure in xxx Format: PNG, JPEG, SVG, PDF
1
2
3
4
5
6
7
8
9
10
11# PNG
fig.write_image("fig1.png")
# JPEG
fig.write_image("fig1.jpeg")
# SVG
fig.write_image("fig1.svg")
# PDF
fig.write_image("fig1.pdf")Change Image dimension and Scale
1
fig.write_image('fig2_scale2.jpeg', width=600, height=350, scale=2)
Interactive HTML export in Plotly Python
1
fig.write_html("fig1.html", auto_open=True)
Advanced: Interactive Custom Controls
Basic Range Slider and Range Selectors
1 | import plotly.graph_objects as go |
References
- USGS - Magnitude Types
- 矩量級 Moment Magnitude Scale
- Magnitude Type: Mw vs. ML vs. MS vs. mb
- Plotly Open Source Graphing Library for Python