Skip to content
Advertisement

Plotly: How to combine scatter and line plots using Plotly Express?

Plotly Express has an intuitive way to provide pre-formatted plotly plots with minimal lines of code; sort of how Seaborn does it for matplotlib.

It is possible to add traces of plots on Plotly to get a scatter plot on an existing line plot. However, I couldn’t find such a functionality in Plotly Express.

Is it possible to combine a scatter and line graph in Plotly Express?

Advertisement

Answer

You can use:

JavaScript

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:

JavaScript

Plot:

enter image description here

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement