
Create Subplots with Plotly Python –
To create subplots in Plotly, we use the make_subplots function from plotly.subplots module.
Let’s read a dataset to work with
import pandas as pd
url = "https://raw.githubusercontent.com/bprasad26/lwd/master/data/batting.csv"
df = pd.read_csv(url)
df.head()

Now, Let’s say that you want to plot two scatter plots in one figure, to do that you will write.
To learn more about scatter plot – How to create Scatter plot in Plotly Python
import plotly.graph_objects as go
from plotly.subplots import make_subplots
indian = df[df['Nationality']=='Indian']
overseas = df[df['Nationality']=='Overseas']
# create subplots
fig = make_subplots(rows=1, cols=2)
fig.add_trace(go.Scatter(x=indian['SR'], y=df['Runs'],
mode='markers', name='Indian'), row=1, col=1)
fig.add_trace(go.Scatter(x=overseas['SR'], y=overseas['Runs'],
mode='markers', name='Overseas'), row=1, col=2)
fig.update_layout(title="Runs vs Strike Rate")
fig.show()

If you want to stack both the subplots on top of each others.
# stacked subplots
fig = make_subplots(rows=2, cols=1)
fig.add_trace(go.Scatter(x=indian['SR'], y=df['Runs'],
mode='markers', name='Indian'), row=1, col=1)
fig.add_trace(go.Scatter(x=overseas['SR'], y=overseas['Runs'],
mode='markers', name='Overseas'), row=2, col=1)
fig.update_layout(title="Runs vs Strike Rate")
fig.show()

Multiple Subplots with Titles –
teams_title = ['Royal Challengers Bangalore','Kings Xi Punjab','Mumbai Indians',
'Kolkata Knight Riders','Delhi Capitals','Rajasthan Royals',
'Chennai Super Kings','Sunrisers Hyderabad']
rcb = df[df['Team']=='Royal Challengers Bangalore']
kxp = df[df['Team']=='Kings Xi Punjab']
mi = df[df['Team']=='Mumbai Indians']
kkr = df[df['Team']=='Kolkata Knight Riders']
dc = df[df['Team']=='Delhi Capitals']
rr = df[df['Team']=='Rajasthan Royals']
csk = df[df['Team']=='Chennai Super Kings']
srh = df[df['Team']=='Sunrisers Hyderabad']
fig = make_subplots(rows=4, cols=2, subplot_titles=teams_title)
fig.add_trace(go.Scatter(x=rcb['SR'], y=rcb['Runs'], mode='markers'), row=1, col=1)
fig.add_trace(go.Scatter(x=kxp['SR'], y=kxp['Runs'], mode='markers'), row=1, col=2)
fig.add_trace(go.Scatter(x=mi['SR'], y=mi['Runs'], mode='markers'), row=2, col=1)
fig.add_trace(go.Scatter(x=kkr['SR'], y=kkr['Runs'], mode='markers'), row=2, col=2)
fig.add_trace(go.Scatter(x=dc['SR'], y=dc['Runs'], mode='markers'), row=3, col=1)
fig.add_trace(go.Scatter(x=rr['SR'], y=rr['Runs'], mode='markers'), row=3, col=2)
fig.add_trace(go.Scatter(x=csk['SR'], y=csk['Runs'], mode='markers'), row=4, col=1)
fig.add_trace(go.Scatter(x=srh['SR'], y=srh['Runs'], mode='markers'), row=4, col=2)
fig.update_layout(title="Runs vs Strike Rate",showlegend=False)
fig.show()

Customize Subplots Axes –
After a figure with subplots is created using the make_subplots function, its axis properties (title, font, range, grid style, etc.) can be customized using the update_xaxes and update_yaxes graph object figure methods. By default, these methods apply to all of the x axes or y axes in the figure. The row and col arguments can be used to control which axes are targeted by the update.
# stacked subplots
fig = make_subplots(rows=2, cols=1, subplot_titles=['Indian','Overseas'])
fig.add_trace(go.Scatter(x=indian['SR'], y=df['Runs'],
mode='markers', name='Indian'), row=1, col=1)
fig.add_trace(go.Scatter(x=overseas['SR'], y=overseas['Runs'],
mode='markers', name='Overseas'), row=2, col=1)
# update xaxis
fig.update_xaxes(title_text="Strike Rate", row=1, col=1)
fig.update_xaxes(title_text="Strike Rate", row=2, col=1)
# update yaxis
fig.update_yaxes(title_text="Runs", row=1, col=1)
fig.update_yaxes(title_text="Runs", row=2, col=1)
fig.update_layout(title="Runs vs Strike Rate")
fig.show()

For default behavior without row and col arguments
# stacked subplots
fig = make_subplots(rows=2, cols=1, subplot_titles=['Indian','Overseas'])
fig.add_trace(go.Scatter(x=indian['SR'], y=df['Runs'],
mode='markers', name='Indian'), row=1, col=1)
fig.add_trace(go.Scatter(x=overseas['SR'], y=overseas['Runs'],
mode='markers', name='Overseas'), row=2, col=1)
# update xaxis
fig.update_xaxes(title_text="Strike Rate")
# update yaxis
fig.update_yaxes(title_text="Runs")
fig.update_layout(title="Runs vs Strike Rate")
fig.show()
Subplots with Shared Axes –
The shared_xaxes and shared_yaxes argument to make_subplots can be used to link the x and y axes of subplots in the resulting figure. The vertical_spacing argument is used to control the vertical spacing between rows in the subplot grid.
# stacked subplots
fig = make_subplots(rows=2, cols=1, shared_xaxes=True,vertical_spacing=0.3,
subplot_titles=['Indian','Overseas'])
fig.add_trace(go.Scatter(x=indian['SR'], y=df['Runs'],
mode='markers', name='Indian'), row=1, col=1)
fig.add_trace(go.Scatter(x=overseas['SR'], y=overseas['Runs'],
mode='markers', name='Overseas'), row=2, col=1)
# update xaxis
fig.update_xaxes(title_text="Strike Rate")
# update yaxis
fig.update_yaxes(title_text="Runs")
fig.update_layout(title="Runs vs Strike Rate")
fig.show()

Subplot with shared Y axis –
indian = df[df['Nationality']=='Indian']
overseas = df[df['Nationality']=='Overseas']
# create subplots
fig = make_subplots(rows=1, cols=2, shared_yaxes=True, horizontal_spacing=0.2)
fig.add_trace(go.Scatter(x=indian['SR'], y=df['Runs'],
mode='markers', name='Indian'), row=1, col=1)
fig.add_trace(go.Scatter(x=overseas['SR'], y=overseas['Runs'],
mode='markers', name='Overseas'), row=1, col=2)
# update xaxis
fig.update_xaxes(title_text="Strike Rate")
# update yaxis
fig.update_yaxes(title_text="Runs")
fig.update_layout(title="Runs vs Strike Rate")
fig.show()

Subplots with different Plots –
indian = df[df['Nationality']=='Indian']
overseas = df[df['Nationality']=='Overseas']
fig = make_subplots(rows=1, cols=2)
fig.add_trace(go.Scatter(x=df['SR'], y=df['Runs'], mode='markers'), row=1, col=1)
fig.add_trace(go.Histogram(x=df['Nationality'], y=df['Runs']), row=1, col=2)
# update x axis
fig.update_xaxes(title_text="Strike Rate", row=1, col=1)
fig.update_xaxes(title_text="Nationality", row=1, col=2)
# update y axis
fig.update_yaxes(title_text="Runs", row=1, col=1)
fig.update_yaxes(title_text="Count", row=1, col=2)
fig.update_layout(showlegend=False)
fig.show()

Related Posts –
1 . How to install plotly python with pip
2 . How to create a Line Chart with Plotly Python
3 . How to create Scatter plot in Plotly Python
4 . How to create a Bar Chart in Plotly Python
5 . How to create Horizontal Bar Chart in Plotly Python
6 . How to create a Histogram in plotly python
7 . How to Create a Box Plot in Plotly Python
8 . How to create a Pie Chart in Plotly Python
9 . How to create a Dot Plot in Plotly Python