Setup:
plotly
library, but can't figure out how to reference a specific subplots' axis to change its' name (or other properties).Code 1
I show a simple example where I add two plots one on thop of the other with plotly.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 Tryied
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:
x
, and active users
label to the x axis of the bottom plot?Appreciate any help. Thanks in advance.
With the help from this answer I as able to solve it, by referencing the xaxis
for plot on the position row=1
, col=1
and the xaxis1
for the plot on the row=2
, col=1
position. The full solution is in Code 1
.
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
)
fig['layout']['xaxis'].update(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['layout']['xaxis2'].update(title_text='active users')
fig.show()
Cheers.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.