Interactive Scatter Plots
Basic scatter
Interactive scatter
import pandas as pd
import plotly.express as px
df = pd.DataFrame({
"hours": [1, 2, 3, 4, 5, 6, 7, 2, 4, 6],
"score": [50, 55, 65, 70, 75, 78, 85, 52, 69, 77],
"group": ["A", "A", "A", "A", "A", "A", "A", "B", "B", "B"],
})
fig = px.scatter(df, x="hours", y="score", color="group", title="Hours vs score")
fig.show()Interactive scatter
import pandas as pd
import plotly.express as px
df = pd.DataFrame({
"hours": [1, 2, 3, 4, 5, 6, 7, 2, 4, 6],
"score": [50, 55, 65, 70, 75, 78, 85, 52, 69, 77],
"group": ["A", "A", "A", "A", "A", "A", "A", "B", "B", "B"],
})
fig = px.scatter(df, x="hours", y="score", color="group", title="Hours vs score")
fig.show()Add trend line
Trend line
import plotly.express as px
fig = px.scatter(df, x="hours", y="score", color="group", trendline="ols", title="Trend line")
fig.show()Trend line
import plotly.express as px
fig = px.scatter(df, x="hours", y="score", color="group", trendline="ols", title="Trend line")
fig.show()Tips
- Use
hover_data=[...]hover_data=[...]to show more context. - Trend lines can be sensitive to outliers.
If this helped you, consider buying me a coffee ☕
Buy me a coffeeWas this page helpful?
Let us know how we did
