# Multiple traces plotly\_express

**URL:** <https://community.plotly.com/t/multiple-traces-plotly-express/23360>\
**Category:** 📊 Plotly Python\
**Created:** [May 12, 2019, 8:09am UTC](https://community.plotly.com/t/multiple-traces-plotly-express/23360 "2019-05-12T08:09:36Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![sladkovm](https://avatars.discourse-cdn.com/v4/letter/s/f07891/32.png) [@sladkovm](https://community.plotly.com/u/sladkovm)\
**Post date:** [May 12, 2019, 8:09am UTC](https://community.plotly.com/t/multiple-traces-plotly-express/23360/1 "2019-05-12T08:09:36Z")

</div>

I’m trying to plot the multiple traces on one plot using the plotly\_express

```auto
import plotly_express as px

fig1 = px.scatter(df, x='date', y='cp_setting')
fig1.add_scatter(df, x='date', y='20m_critical_power')
fig1

```

but stumble upon this error:

```auto
    The 'cliponaxis' property must be specified as a bool
    (either True, or False)

```

What are the general recommendations on how to plot multiple traces with px?

---

<div class="post-metadata">

**Author:** ![nicolaskruchten](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/nicolaskruchten/32/7018_2.png) [@nicolaskruchten](https://community.plotly.com/u/nicolaskruchten)\
**Post date:** [May 12, 2019, 6:27pm UTC](https://community.plotly.com/t/multiple-traces-plotly-express/23360/2 "2019-05-12T18:27:12Z")

</div>

The way you’re trying to do it is the recommended way… @jmmease any thoughts about this error?

EDIT:

I would now actually recommend doing things like this

```auto
import plotly_express as px

px.scatter(df.melt(id_vars="date"), x='date', y='value', color='variable')

```

---

<div class="post-metadata">

**Author:** ![jmmease](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/jmmease/32/3706_2.png) [@jmmease](https://community.plotly.com/u/jmmease)\
**Post date:** [May 13, 2019, 10:43am UTC](https://community.plotly.com/t/multiple-traces-plotly-express/23360/3 "2019-05-13T10:43:12Z")

</div>

Hi @sladkovm,

This is the recommended way to add traces. Could you include the full stack trace for the error you’re getting? Also, if you could write out a sample data frame that causes the error that would be helpful.

Thanks!  
-Jon

---

<div class="post-metadata">

**Author:** ![sladkovm](https://avatars.discourse-cdn.com/v4/letter/s/f07891/32.png) [@sladkovm](https://community.plotly.com/u/sladkovm)\
**Post date:** [May 13, 2019, 2:58pm UTC](https://community.plotly.com/t/multiple-traces-plotly-express/23360/4 "2019-05-13T14:58:15Z")

</div>

```auto
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-17-d67da134c7b9> in <module>
      1 fig = px.scatter(_df, x='date', y='cp_setting')
----> 2 fig.add_scatter(_df, x='date', y='20m_critical_power')
      3 fig

~/.local/share/virtualenvs/cycling-analytics-o7NF_DiO/lib/python3.6/site-packages/plotly/graph_objs/_figure.py in add_scatter(self, cliponaxis, connectgaps, customdata, customdatasrc, dx, dy, error_x, error_y, fill, fillcolor, groupnorm, hoverinfo, hoverinfosrc, hoverlabel, hoveron, hovertemplate, hovertemplatesrc, hovertext, hovertextsrc, ids, idssrc, legendgroup, line, marker, mode, name, opacity, orientation, r, rsrc, selected, selectedpoints, showlegend, stackgaps, stackgroup, stream, t, text, textfont, textposition, textpositionsrc, textsrc, tsrc, uid, uirevision, unselected, visible, x, x0, xaxis, xcalendar, xsrc, y, y0, yaxis, ycalendar, ysrc, row, col, **kwargs)
   7492 ycalendar=ycalendar,
   7493 ysrc=ysrc,
-> 7494 **kwargs
   7495 )
   7496 return self.add_trace(new_trace, row=row, col=col)

~/.local/share/virtualenvs/cycling-analytics-o7NF_DiO/lib/python3.6/site-packages/plotly/graph_objs/ __init__.py in __init__ (self, arg, cliponaxis, connectgaps, customdata, customdatasrc, dx, dy, error_x, error_y, fill, fillcolor, groupnorm, hoverinfo, hoverinfosrc, hoverlabel, hoveron, hovertemplate, hovertemplatesrc, hovertext, hovertextsrc, ids, idssrc, legendgroup, line, marker, mode, name, opacity, orientation, r, rsrc, selected, selectedpoints, showlegend, stackgaps, stackgroup, stream, t, text, textfont, textposition, textpositionsrc, textsrc, tsrc, uid, uirevision, unselected, visible, x, x0, xaxis, xcalendar, xsrc, y, y0, yaxis, ycalendar, ysrc, **kwargs)
  37400 # ----------------------------------
  37401 _v = arg.pop('cliponaxis', None)
> 37402 self['cliponaxis'] = cliponaxis if cliponaxis is not None else _v
  37403 _v = arg.pop('connectgaps', None)
  37404 self['connectgaps'] = connectgaps if connectgaps is not None else _v

~/.local/share/virtualenvs/cycling-analytics-o7NF_DiO/lib/python3.6/site-packages/plotly/basedatatypes.py in __setitem__ (self, prop, value)
   3164 # ### Handle simple property ###
   3165 else:
-> 3166 self._set_prop(prop, value)
   3167 
   3168 # Handle non-scalar case

~/.local/share/virtualenvs/cycling-analytics-o7NF_DiO/lib/python3.6/site-packages/plotly/basedatatypes.py in _set_prop(self, prop, val)
   3405 return
   3406 else:
-> 3407 raise err
   3408 
   3409 # val is None

~/.local/share/virtualenvs/cycling-analytics-o7NF_DiO/lib/python3.6/site-packages/plotly/basedatatypes.py in _set_prop(self, prop, val)
   3400 validator = self._validators.get(prop)
   3401 try:
-> 3402 val = validator.validate_coerce(val)
   3403 except ValueError as err:
   3404 if self._skip_invalid:

~/.local/share/virtualenvs/cycling-analytics-o7NF_DiO/lib/python3.6/site-packages/_plotly_utils/basevalidators.py in validate_coerce(self, v)
    590 pass
    591 elif not isinstance(v, bool):
--> 592 self.raise_invalid_val(v)
    593 
    594 return v

~/.local/share/virtualenvs/cycling-analytics-o7NF_DiO/lib/python3.6/site-packages/_plotly_utils/basevalidators.py in raise_invalid_val(self, v, inds)
    275 typ=type_str(v),
    276 v=repr(v),
--> 277 valid_clr_desc=self.description()))
    278 
    279 def raise_invalid_elements(self, invalid_els):

ValueError: 
    Invalid value of type 'pandas.core.frame.DataFrame' received for the 'cliponaxis' property of scatter
        Received value: date cp_setting \
date                                                              
2003-07-10 22:00:00+00:00 2003-07-10 22:00:00+00:00 220.0   
2003-07-11 22:00:00+00:00 2003-07-11 22:00:00+00:00 220.0   
2003-07-12 22:00:00+00:00 2003-07-12 22:00:00+00:00 220.0   
2003-07-13 22:00:00+00:00 2003-07-13 22:00:00+00:00 220.0   
2003-07-15 22:00:00+00:00 2003-07-15 22:00:00+00:00 220.0   

                           20m_critical_power  
date                                           
2003-07-10 22:00:00+00:00 162.56768  
2003-07-11 22:00:00+00:00 246.19832  
2003-07-12 22:00:00+00:00 209.03568  
2003-07-13 22:00:00+00:00 182.16999  
2003-07-15 22:00:00+00:00 125.85205  

    The 'cliponaxis' property must be specified as a bool
    (either True, or False)

```

I wonder if it has anything to do with a datetime, but the vanilla plotly way seems to work:

```auto
trace1 = go.Scatter(x=df.date, y=df.cp_setting, mode='lines', name='CP Setting')
trace2 = go.Scatter(x=df.date, y=df['20m_critical_power'], mode='markers', name='20m Power')
iplot(go.Figure([trace1, trace2]))

```

---

<div class="post-metadata">

**Author:** ![nicolaskruchten](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/nicolaskruchten/32/7018_2.png) [@nicolaskruchten](https://community.plotly.com/u/nicolaskruchten)\
**Post date:** [May 13, 2019, 3:07pm UTC](https://community.plotly.com/t/multiple-traces-plotly-express/23360/5 "2019-05-13T15:07:08Z")

</div>

Ah sorry I didn’t see the `df` in your `fig1.add_scatter(df, ...` call!

`add_scatter` doesn’t have the same “data frame + column names” API as Plotly Express so here you’ll need to do something like `fig.add_scatter(x=df.date, y=df.20m_critical_power)`.

---

<div class="post-metadata">

**Author:** ![sladkovm](https://avatars.discourse-cdn.com/v4/letter/s/f07891/32.png) [@sladkovm](https://community.plotly.com/u/sladkovm)\
**Post date:** [June 11, 2019, 11:52am UTC](https://community.plotly.com/t/multiple-traces-plotly-express/23360/6 "2019-06-11T11:52:11Z")

</div>

Thanks, this works. I like add\_xxx API better. Does allow mixing columns from two different dataframes while plotting.

---

<div class="post-metadata">

**Author:** ![tmbluth](https://avatars.discourse-cdn.com/v4/letter/t/278dde/32.png) [@tmbluth](https://community.plotly.com/u/tmbluth)\
**Post date:** [June 24, 2019, 10:13pm UTC](https://community.plotly.com/t/multiple-traces-plotly-express/23360/7 "2019-06-24T22:13:48Z")

</div>

What about for adding traces after you’ve split data by facets?  
For example:

```
import plotly_express as px
import pandas as pd

df = px.data.gapminder()
df_grouped = df.groupby(['continent','year'], as_index=False).mean()

px_plot = px.line(df_grouped, x='year', y='gdpPercap', color='continent', facet_row='continent')
# Plot before....
px_plot

```

 ![px_plot1](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/9/9b510bb44a9500980f0a35d71a15e972d21351d2.png)

```
for c in list(df_grouped.continent.unique()):
    continent_df = df_grouped.loc[df_grouped.continent==c, :].reset_index()
    px_plot.add_scatter(x=continent_df.year, y=continent_df.lifeExp*100, name=continent_df.continent[0])

# ...plot after
px_plot

```

 ![px_plot2](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/5/53f01af50dbc6b5cae99cc1a4f35a2e33e069c62.png)

They all get plotted in the first facet but I’m hoping to add a trace within each facet based off the facet attribute(s) and/or color in a dual axis fashion. Any suggestions?

PS Yes, this is a non-practical example, but was the easiest reproducible example to avoid sharing proprietary data

---

<div class="post-metadata">

**Author:** ![nicolaskruchten](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/nicolaskruchten/32/7018_2.png) [@nicolaskruchten](https://community.plotly.com/u/nicolaskruchten)\
**Post date:** [June 25, 2019, 1:32pm UTC](https://community.plotly.com/t/multiple-traces-plotly-express/23360/8 "2019-06-25T13:32:46Z")

</div>

This is tricky at the moment but in a couple of weeks when Plotly.py v4 is released, with Plotly Express built-in as `plotly.express` you’ll be able to add `row=n, col=m` in your `add_scatter()` call to target a specific facet.

In the meantime, you’ll need to set the `yaxis` and `xaxis` attributes of your new traces to match whatever subplot you want to target.

---

<div class="post-metadata">

**Author:** ![Soerendip](https://avatars.discourse-cdn.com/v4/letter/s/d2c977/32.png) [@Soerendip](https://community.plotly.com/u/Soerendip)\
**Post date:** [September 20, 2019, 8:26pm UTC](https://community.plotly.com/t/multiple-traces-plotly-express/23360/9 "2019-09-20T20:26:47Z")

</div>

What if I want to add a line\_3d?? fig does not have that property add\_line\_3d??

---

<div class="post-metadata">

**Author:** ![Soerendip](https://avatars.discourse-cdn.com/v4/letter/s/d2c977/32.png) [@Soerendip](https://community.plotly.com/u/Soerendip)\
**Post date:** [September 20, 2019, 8:31pm UTC](https://community.plotly.com/t/multiple-traces-plotly-express/23360/10 "2019-09-20T20:31:49Z")

</div>

fig.add\_scatter3d also does not work. It does not accept a dataframe as first argument. It would be nice if the api would be more consistent.

---

<div class="post-metadata">

**Author:** ![nicolaskruchten](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/nicolaskruchten/32/7018_2.png) [@nicolaskruchten](https://community.plotly.com/u/nicolaskruchten)\
**Post date:** [May 26, 2020, 4:21pm UTC](https://community.plotly.com/t/multiple-traces-plotly-express/23360/11 "2020-05-26T16:21:54Z")

</div>

With [version 4.8 of Plotly.py](https://community.plotly.com/t/announcing-plotly-py-4-8-plotly-express-support-for-wide-and-mixed-form-data-plus-a-pandas-backend/40048/1), you can now plot this kind of data directly from PX, with `y=['cp_setting', '20m_critical_power']`
