How to set color_continuous_scale in subplot?

Iā€™m following an example from this page Continuous color scales and color bars in Python. The original code is as follows:

df = px.data.iris()
fig = px.scatter(df, x="sepal_width", y="sepal_length",
                 color="sepal_length", color_continuous_scale=px.colors.sequential.Viridis)

fig.show()

But now, If I make a subplot, it loses the color scheme

df = px.data.iris()
fig = make_subplots(rows=2, cols=1, shared_xaxes=True)
fig1 = px.scatter(df, x="sepal_width", y="sepal_length",
                  color="sepal_length", color_continuous_scale=px.colors.sequential.Viridis)
for trace in fig1.data:
    fig.add_trace(trace, row=1, col=1)

fig.show()

Any advice is appreciated!

Iā€™m not sure with px, but with go.Scatter you can do something as below:

import plotly.express as px
from plotly.subplots import make_subplots
import plotly.graph_objs as go

df = px.data.iris()
fig = make_subplots(rows=2, cols=1, shared_xaxes=True)
fig1 = go.Scatter(x=df["sepal_width"], 
                  y=df["sepal_length"], 
                  mode='markers',
                  marker=dict(
                      color=df["sepal_length"],
                      colorbar=dict(
                          title="sepal_length"
                      ),
        colorscale="Viridis"
    ))

fig.add_trace(fig1, row=1, col=1)
fig.show()

1 Like