How to combine scatter and line plots using Plotly Express

You can use:

fig3 = go.Figure(data=fig1.data + fig2.data)

Where fig1 and fig2 are built using px.line() and px.scatter(), respectively. And fig3 is, as you can see, built using plotly.graph_objects.

Some details:

One approach that I use alot is building two figures fig1 and fig2 using plotly.express and then combine them using their data attributes together with a go.Figure / plotly.graph_objects object like this:

import plotly.express as px
import plotly.graph_objects as go
df = px.data.iris()

fig1 = px.line(df, x="sepal_width", y="sepal_length")
fig1.update_traces(line=dict(color="rgba(50,50,50,0.2)"))

fig2 = px.scatter(df, x="sepal_width", y="sepal_length", color="species")

fig3 = go.Figure(data=fig1.data + fig2.data)
fig3.show()

Plot:

enter image description here

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)