Setup:
- I'm tring to plot a subplots with
plotly
library, but can't figure out how to reference a specific subplots' axis to change its' name (or other properties). - In
Code 1
I show a simple example where I add two plots one on thop of the other withplotly.subplots.make_subplots
.
Code 1
import numpy as np
from plotly.subplots import make_subplots
from math import exp
fig = make_subplots(2, 1)
x = np.linspace(0, 10, 1000)
y = np.array(list(map(lambda x: 1 / (1 + exp(-0.1 * x + 5)), x)))
fig.add_trace(
go.Scatter(
x=x,
y=y,
name=f'\N{Greek Small Letter Sigma}(x)',
showlegend=True
),
row=1,
col=1
)
x = np.where(np.random.randint(0, 2, 100)==1)[0]
fig.add_trace(
go.Scatter(
x=x,
y=np.zeros_like(x),
name=f'Plot 2',
mode='markers',
marker=dict(
symbol='circle-open',
color='green',
size=5
),
showlegend=True
),
row=2,
col=1
)
fig.show()
What I've Tried
I've tried using the fig.update_xaxes()
after each trace addition, but it messes the plots and does not produce the desired output, as shown in Code 2
.
Code 2:
import numpy as np
from plotly.subplots import make_subplots
from math import exp
fig = make_subplots(2, 1)
x = np.linspace(0, 10, 1000)
y = np.array(list(map(lambda x: 1 / (1 + exp(-0.1 * x + 5)), x)))
fig.add_trace(
go.Scatter(
x=x,
y=y,
name=f'\N{Greek Small Letter Sigma}(x)',
showlegend=True
),
row=1,
col=1
)
fig.update_xaxes(title_text='x')
x = np.where(np.random.randint(0, 2, 100)==1)[0]
fig.add_trace(
go.Scatter(
x=x,
y=np.zeros_like(x),
name=f'Plot 2',
mode='markers',
marker=dict(
symbol='circle-open',
color='green',
size=5
),
showlegend=True
),
row=2,
col=1
)
fig.update_xaxes(title_text='active users')
fig.show()
which results in (note the active users
being printed on the top):
My Questions:
- How can I assign the top plot x axis the label
x
, andactive users
label to the x axis of the bottom plot? - And in general - how can I access the properties of an individual subplot?