Reputation: 1899
I've recently gotten into plotly and am absolutely loving it, so am trying to use it in each project I do.
With matplotlib I can plot a line plot and a scatter plot on the same graph using the code below.
plt.figure(figsize = (20,5))
plt.scatter(x, y)
plt.plot(x, y_pred, color = "r")
plt.show()
Using the trendline
parameter in the scatter
function inside plotly.express
I can plot a line of best fit through the scattered points, but I don't want that as I am trying to demonstrate how to calculate that line.
Thanks for the help in advance!
Upvotes: 0
Views: 3645
Reputation: 1
If you create a line plot instead, you can actually use the markers
arument:
px.line(x=x, y=y, markers=True)
https://i.sstatic.net/aJaaM.png
https://plotly.com/python-api-reference/generated/plotly.express.line.html
Upvotes: 0
Reputation: 31146
Using same defined arrays / lists, x, y, y_pred
. An equivalent approach is to use Plotly Express to create a figure then add additional traces to it.
import pandas as pd
import numpy as np
import plotly.express as px
x = np.linspace(1, 20, 16)
y = np.random.uniform(1, 6, 16)
y_pred = y * 1.1
fig = px.scatter(x=x, y=y, color_discrete_sequence=["yellow"])
fig.add_traces(px.line(x=x, y=y_pred, color_discrete_sequence=["red"]).data)
Upvotes: 1
Reputation: 398
You can use fig.add_shape
after you have created the scatter plot like so:
fig.add_shape(
type="line",
x0=0,
y0=0,
x1=10,
y1=10,
line=dict(width=1, dash="dash"),
)
Upvotes: 0