18

I am very new to python and plotly.express, and I find it very confusing...

I am trying to use the principle of adding different traces to my figure, using example code shown here https://plotly.com/python/line-charts/, Line Plot Modes, #Create traces.

BUT I get my data from a .CSV file.

import plotly.express as px
import plotly as plotly
import plotly.graph_objs as go
import pandas as pd

data = pd.read_csv(r"C:\Users\x.csv")

fig = px.scatter(data, x="Time", y="OD", color="C-source", size="C:A 1 ratio")

fig = px.line(data, x="Time", y="OD", color="C-source")
fig.show()

The above lines produces scatter/line plots with the correct data, but the data is mixed together. I have data from 2 different sources marked by a column named "Strain" in my .csv file that I would like the chart to reflect.

Is the traces option a possible way to do it, or is there another way?

figure plotted with px.line

Community
  • 1
  • 1
MikkelC
  • 261
  • 1
  • 3
  • 9

5 Answers5

23

You can add traces using an Express plot by using .select_traces(). Something like:

fig.add_traces(
    list(px.line(...).select_traces())
)

Note the need to convert to list, since .select_traces() returns a generator.

Peque
  • 13,638
  • 11
  • 69
  • 105
8

It looks like you probably want the lines with the scatter dots as well on a single plot?

You're setting fig to equal px.scatter() and then setting (changing) it to equal px.line(). When set to line, the scatter plot is overwritten.

You're already importing graph objects so you can use add_trace with go, something like this:

fig.add_trace(go.Scatter(x=data["Time"], y=data["OD"], mode='markers', marker=dict(color=data["C-source"], size=data["C:A 1 ratio"])))

Depending on how your data is set up, you may need to add each C-source separately doing something like:

x=data.query("C-source=='Term'")["Time"], ... , name='Term'`

Here's a few references with examples and options you can use to set up your scatter:

Scatter plot examples  

Marker styles  

Scatter arguments and attributes

lxmmxl56
  • 492
  • 4
  • 10
3

You can use the apporach stated in Plotly: How to combine scatter and line plots using Plotly Express?

fig3 = go.Figure(data=fig1.data + fig2.data)

or a more convenient and scalable approach:

fig1.data and fig2.data are common tuples that hold all the info needed for a plot and the + just concatenates them.

# this will hold all figures until they are combined
all_figures = []

# data_collection: dictionary with Pandas dataframes
 
for df_label in data_collection:

    df = data_collection[df_label]
    fig = px.line(df, x='Date', y=['Value'])
    all_figures.append(fig)

import operator
import functools

# now you can concatenate all the data tuples
# by using the programmatic add operator 
fig3 = go.Figure(data=functools.reduce(operator.add, [_.data for _ in all_figures]))
fig3.show()
Joe
  • 6,758
  • 2
  • 26
  • 47
  • 1
    Or you can append `fig.data` and then `go.Figure(data=sum(all_figures, ()))`. Or `go.Figure(data=sum((fig.data for fig in figures), ()))` if you need to iterate twice. ^^ – Peque Jan 06 '22 at 02:09
1

thanks for taking the time to help me out. I ended up with two solutions that worked, of which using "facet_col" to divide the plot into two subplots (1 for each strain) was the most simple solution.

https://plotly.com/python/axes/

MikkelC
  • 261
  • 1
  • 3
  • 9
0

Thanks. this worked for me also where Fig_Set_B is a list of scatter plots

# create a tuple of first line plots in first 6 plots from plot set Fig_Set_B` 
fig_combined = go.Figure(data= tuple(Fig_Set_B[x].data[0] for x in range(6)) ) 
fig_combined.show()
user15420598
  • 107
  • 7