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:

Answer from vestland on Stack Overflow
🌐
Plotly
plotly.com › python › line-and-scatter
Scatter plots in Python
With px.scatter, each data point is represented as a marker point, whose location is given by the x and y columns. ... # x and y given as array_like objects import plotly.express as px fig = px.scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16]) ...
🌐
Plotly
plotly.com › python-api-reference › generated › plotly.express.scatter
plotly.express.scatter — 6.6.0 documentation
If set, a vertical subplot is drawn ... the main plot, visualizing the y-distribution. trendline (str) – One of 'ols', 'lowess', 'rolling', 'expanding' or 'ewm'. If 'ols', an Ordinary Least Squares regression line will be drawn for each discrete-color/symbol group. If 'lowess’, a Locally Weighted Scatterplot Smoothing ...
🌐
Plotly
plotly.com › python-api-reference › generated › plotly.express.scatter.html
plotly.express.scatter — 6.5.0 documentation
If set, a vertical subplot is drawn ... the main plot, visualizing the y-distribution. trendline (str) – One of 'ols', 'lowess', 'rolling', 'expanding' or 'ewm'. If 'ols', an Ordinary Least Squares regression line will be drawn for each discrete-color/symbol group. If 'lowess’, a Locally Weighted Scatterplot Smoothing ...
🌐
Plotly
plotly.com › python › plotly-express
Plotly express in Python
Plotly Express currently includes the following functions: Basics: scatter, line, area, bar, funnel, timeline
🌐
Plotly
plotly.github.io › plotly.py-docs › generated › plotly.express.scatter_matrix.html
plotly.express.scatter_matrix — 6.5.0 documentation
plotly.express.scatter_matrix(data_frame=None, dimensions=None, color=None, symbol=None, size=None, hover_name=None, hover_data=None, custom_data=None, category_orders=None, labels=None, color_discrete_sequence=None, color_discrete_map=None, color_continuous_scale=None, range_color=None, color_continuous_midpoint=None, symbol_sequence=None, symbol_map=None, opacity=None, size_max=None, title=None, subtitle=None, template=None, width=None, height=None) → plotly.graph_objects._figure.Figure¶
Top answer
1 of 4
78

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:

2 of 4
6

EDIT: fixed typo.

This works great and is even more useful with flipSTAR's clarification regarding adding a global layout to the combined fig. However, sometimes a global layout doesn't cover everything. For example, in my case (a stacked bar and two single scatter plot lines), my global layout caused me to lose my scatter plot legends. Fortunately, you can add additional arguments to the combined fig by targeting the specific figures. e.g., given a hypothetical:

fig1 = px.bar(...)
fig2 = px.line(...)
fig3 = px.line(...)
all_fig = go.Figure(data=fig1.data + fig2.data + fig3.data, layout = fig1.layout)

Which is a bar chart and two scatter plots each with a single line, you can add the legends for each line with:

all_fig['data'][1]['showlegend']=True
all_fig['data'][1]['name']='Line 1 Name'
all_fig['data'][1]['hovertemplate']='Line 1 Name<br>x=%{x}<br>y=%{y}<extra></extra>'
all_fig['data'][2]['showlegend']=True
all_fig['data'][2]['name']='Line 2 Name'
all_fig['data'][2]['hovertemplate']='Line 2 Name<br>x=%{x}<br>y=%{y}<extra></extra>'

(the bar is all_fig['data'][0]).

For some reason, the name won't show up on hover unless you explicitly add it to 'hovertemplate.'

🌐
Plotly
plotly.com › python-api-reference › generated › plotly.express.scatter_3d
plotly.express.scatter_3d — 6.6.0 documentation
plotly.express.scatter_3d(data_frame=None, x=None, y=None, z=None, color=None, symbol=None, size=None, text=None, hover_name=None, hover_data=None, custom_data=None, error_x=None, error_x_minus=None, error_y=None, error_y_minus=None, error_z=None, error_z_minus=None, animation_frame=None, animation_group=None, category_orders=None, labels=None, size_max=None, color_discrete_sequence=None, color_discrete_map=None, color_continuous_scale=None, range_color=None, color_continuous_midpoint=None, symbol_sequence=None, symbol_map=None, opacity=None, log_x=False, log_y=False, log_z=False, range_x=None, range_y=None, range_z=None, title=None, subtitle=None, template=None, width=None, height=None) → plotly.graph_objects._figure.Figure¶ ·
🌐
Codecademy
codecademy.com › docs › python:plotly › express › .scatter()
Python:Plotly | express | .scatter() | Codecademy
June 19, 2024 - fig = px.scatter(x=x, y=y, color=color, symbol=symbol, title = "Scatter Plot with Colors and Symbols")
Find elsewhere
🌐
Plotly
plotly.github.io › plotly.py-docs › generated › plotly.express.scatter_mapbox.html
plotly.express.scatter_mapbox — 6.6.0 documentation
plotly.express.scatter_mapbox(data_frame=None, lat=None, lon=None, color=None, text=None, hover_name=None, hover_data=None, custom_data=None, size=None, animation_frame=None, animation_group=None, category_orders=None, labels=None, color_discrete_sequence=None, color_discrete_map=None, color_continuous_scale=None, range_color=None, color_continuous_midpoint=None, opacity=None, size_max=None, zoom=8, center=None, mapbox_style=None, title=None, subtitle=None, template=None, width=None, height=None) → plotly.graph_objects._figure.Figure¶ ·
🌐
Towards Data Science
towardsdatascience.com › home › latest › using plotly express to create interactive scatter plots
Using Plotly Express to Create Interactive Scatter Plots | Towards Data Science
January 17, 2025 - Image by the author. As seen in the above examples, Plotly Express is a powerful library for visualising data. It allows you to create very powerful and interactive plots with minimal amounts of code.
🌐
Kaggle
kaggle.com › code › alsaniipe › scatter-plots-with-plotly-express
Scatter plots with Plotly Express
Checking your browser before accessing www.kaggle.com · Click here if you are not automatically redirected after 5 seconds
🌐
Plotly
plotly.com › python-api-reference › generated › plotly.express.scatter_map.html
plotly.express.scatter_map — 6.6.0 documentation
plotly.express.scatter_map(data_frame=None, lat=None, lon=None, color=None, text=None, hover_name=None, hover_data=None, custom_data=None, size=None, animation_frame=None, animation_group=None, category_orders=None, labels=None, color_discrete_sequence=None, color_discrete_map=None, color_continuous_scale=None, range_color=None, color_continuous_midpoint=None, opacity=None, size_max=None, zoom=8, center=None, map_style=None, title=None, subtitle=None, template=None, width=None, height=None) → plotly.graph_objects._figure.Figure¶ ·
🌐
Plotly
plotly.com › python › 3d-scatter-plots
3d scatter plots in Python
How to make 3D scatter plots in Python with Plotly. Plotly Studio: Transform any dataset into an interactive data application in minutes with AI. Try Plotly Studio now. Plotly Express is the easy-to-use, high-level interface to Plotly, which operates on a variety of types of data and produces ...
🌐
GeeksforGeeks
geeksforgeeks.org › scatter-plot-using-plotly-in-python
Scatter plot using Plotly in Python - GeeksforGeeks
February 11, 2024 - The hover_data parameter is set to 'petal_width', which means that when you hover over any point in the scatter plot, a small pop-up or tooltip will appear, displaying the value of the 'petal_width' column for the corresponding data point. ... import plotly.express as px # Loading the iris dataset df = px.data.iris() fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species", size='petal_length', hover_data=['petal_width']) fig.show()
🌐
Plotly
plotly.com › python › tile-scatter-maps
Scatter plots on tile maps in Python
import plotly.express as px df = px.data.carshare() fig = px.scatter_map(df, lat="centroid_lat", lon="centroid_lon", color="peak_hour", size="car_hours", color_continuous_scale=px.colors.cyclical.IceFire, size_max=15, zoom=10) fig.show()
🌐
DataCamp
datacamp.com › cheat-sheet › plotly-express-cheat-sheet
Plotly Express Cheat Sheet | DataCamp
November 16, 2022 - # First import plotly express as px import plotly.express as px · # Create a scatterplot on a DataFrame named clinical_data px.scatter(clinical_data, x="experiment_1", y="experiment_2")
🌐
Python Graph Gallery
python-graph-gallery.com › 511-interactive-scatterplot-with-plotly
Interactive scatterplot with Plotly
Essentially, a scatter plot allows us to see how two variables might be related or influenced by each other, making it a valuable tool for understanding data and drawing insights in a visual and intuitive manner.
🌐
Sharp Sight
sharpsight.ai › blog › plotly-scatter-plot
How to Create a Plotly Scatter Plot - Sharp Sight
October 11, 2021 - And obviously, we’ll need to import Plotly to create our Plotly scatterplot. import pandas as pd import numpy as np import plotly.express as px