I am trying to plot this dataset:
Date Time Sample Name Result
2020-08-26 14:34:02 ZK - Myoblasts 74.2
2020-08-26 14:55:24 QC 50.6
2020-08-26 15:12:27 CD T2 60.7
2020-08-27 15:23:10 QC 85.4
2020-08-27 15:25:06 ZK - Myoblasts 85.4
2020-08-27 15:27:32 CD T2 83.1
2020-08-27 15:47:05 CD T2 89.4
2020-08-27 16:57:02 QC 96.3
2020-09-01 18:49:00 QC 45.6
2020-09-01 18:54:00 Amy F4 P2 32.7
2020-09-01 18:58:00 Amy F4 P2 10.0
2020-09-02 14:41:00 KL - plate 1 78.0
2020-09-02 14:48:00 KL - plate 1 56.0
2020-09-02 14:52:00 Suspension 80.0
2020-09-02 15:51:00 Suspension 23.0
2020-09-02 15:55:00 Amy F4 P2 60.0
I managed to plot it but it plots everything as a single trace, like this:
What I want is to have different trace for each 'Sample Name', so that I can remove/add them and leave only those I'd like to see on the plot. So, each of the different 'Sample Name' will be shown in a different color in the legend on the right as a list of discrete traces.
My code is here:
layout = go.Layout(title='Viability Plot',
xaxis={'title':'Sample Name'},
yaxis={'title':'Viability(%)'})
fig = go.Figure(layout=layout)
fig.add_trace(go.Scatter(x=df['Sample Name'],
y=df['Result'],
mode='markers',
hovertext=df['Date Time'].astype(str)
)
)
fig.show()
Please help to have Sample Names as different traces :(
CodePudding user response:
The simple answer is use Plotly Express
import pandas as pd
import plotly.express as px
import io
df = pd.read_csv(
io.StringIO(
"""Date Time Sample Name Result
2020-08-26 14:34:02 ZK - Myoblasts 74.2
2020-08-26 14:55:24 QC 50.6
2020-08-26 15:12:27 CD T2 60.7
2020-08-27 15:23:10 QC 85.4
2020-08-27 15:25:06 ZK - Myoblasts 85.4
2020-08-27 15:27:32 CD T2 83.1
2020-08-27 15:47:05 CD T2 89.4
2020-08-27 16:57:02 QC 96.3
2020-09-01 18:49:00 QC 45.6
2020-09-01 18:54:00 Amy F4 P2 32.7
2020-09-01 18:58:00 Amy F4 P2 10.0
2020-09-02 14:41:00 KL - plate 1 78.0
2020-09-02 14:48:00 KL - plate 1 56.0
2020-09-02 14:52:00 Suspension 80.0
2020-09-02 15:51:00 Suspension 23.0
2020-09-02 15:55:00 Amy F4 P2 60.0"""
),
sep="\s\s ",
engine="python",
)
df["Date Time"] = pd.to_datetime(df["Date Time"])
px.scatter(
df, x="Sample Name", y="Result", color="Sample Name", hover_data=["Date Time"]
).update_layout(
title="Viability Plot",
xaxis={"title": "Sample Name"},
yaxis={"title": "Viability(%)"},
)