0

I have a pandas dataframe like below

            x        s       y
Date
2021-06-25  1       red      2
2021-06-28  2       red      3
2021-06-29  3       red      4
2021-06-25  1       blue     2
2021-06-28  2       blue     3
2021-06-29  3       blue     4

How can I create a scatter plot in plotly[express]/cufflinks like scatter plot

such that plot axes are x and y and red and blue are separate traces in the plot. Any hint ?

1 Answer 1

1
  • your sample data looks problematic, both red and blue have same values. Have added .5 to blue to demonstrate
  • simple pandas to structure data first, so colors are columns
  • then use plotly express scatter()
import pandas as pd
import io
import plotly.express as px

df = pd.read_csv(io.StringIO("""Date            x        s       y
2021-06-25  1       red      2
2021-06-28  2       red      3
2021-06-29  3       red      4
2021-06-25  1       blue     2.5
2021-06-28  2       blue     3.5
2021-06-29  3       blue     4.5"""), sep="\s+").set_index("Date")

df2 = df.set_index(["x","s"]).unstack("s").droplevel(0,1)

px.scatter(df2, x=df2.index, y=df2.columns)

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.