# Learning Dash - How to Add Traces

**URL:** <https://community.plotly.com/t/learning-dash-how-to-add-traces/61130>\
**Category:** Dash Python\
**Tags:** question\
**Created:** [February 16, 2022, 6:03pm UTC](https://community.plotly.com/t/learning-dash-how-to-add-traces/61130 "2022-02-16T18:03:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmcilip](https://avatars.discourse-cdn.com/v4/letter/c/e9c0ed/32.png) [@cmcilip](https://community.plotly.com/u/cmcilip)\
**Post date:** [February 16, 2022, 6:03pm UTC](https://community.plotly.com/t/learning-dash-how-to-add-traces/61130/1 "2022-02-16T18:03:22Z")

</div>

Hello Everyone and sorry for such a simple question.

My goal is to be more functional in Dash to display results of data science applications that I am trying to build - for this application I would like to view some data from one of my data repositories. The tutorial I am following is here - I am trying to alter the code and understand how to add a trace to the top scatter chart which is in the Singapore.py file. The link to the tutorial is here: [https://towardsdatascience.com/beginners-guide-to-building-a-multi-page-dashboard-using-dash-5d06dbfc7599](https://towardsdatascience.com/beginners-guide-to-building-a-multi-page-dashboard-using-dash-5d06dbfc7599)

I have attempted two changes:  
1)  
data = go.Scatter()  
data.add\_trace[go.scatter(x=dff[‘Date’], y=dff[‘total’],  
mode=‘lines+markers’, name=‘Daily confirmed’)]

and 2)  
data = [go.Scatter(x=dff[‘Date’], y=dff[‘total’],  
mode=‘lines+markers’,name=‘Daily confirmed’)]  
data.add\_trace[go.Scatter(x=dff[‘Date’], y=dff[‘Daily Imported’],  
mode=‘lines+markers’,name=‘Daily confirmed’)]

The code which does work but only includes 1 trace and not multiple traces is:  
data = [go.Scatter(x=dff[‘Date’], y=dff[‘total’],  
mode=‘lines+markers’,name=‘Daily confirmed’)]

The error I get is:  
AttributeError: ‘Scatter’ object has no attribute ‘add\_trace’

I am sure there is more information needed - this is my first app and experience with Python so please, if you could reach out and let me know what more to include - I would love to collaborate and use your help to solve my issue.

Thank you,  
Chris

---

<div class="post-metadata">

**Author:** ![jlfsjunior](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/jlfsjunior/32/16061_2.png) [@jlfsjunior](https://community.plotly.com/u/jlfsjunior)\
**Post date:** [February 16, 2022, 6:54pm UTC](https://community.plotly.com/t/learning-dash-how-to-add-traces/61130/2 "2022-02-16T18:54:04Z")

</div>

Hi,

Welcome to the community, Chris! 🙂

Your code number one can be fixed by changing the first line to:

```nohighlight
fig = go.Figure()
fig.add_trace(...) # and so on

```

I changed `data` to `fig` because that’s usually how the object gets initialized (so it can be less confusing for you).

You can add traces to `fig` using `fig,add_traces()`, of course. Another approach is to pass the traces in a list on initialization using the `data` argument:

```nohighlight
fig = go.Figure(
     data=[
          go.Scatter(...),
          go.Scatter(...),
          # and whatever traces you need...
     ]
)

```

I guess this is closer to the method 2 you mentioned.

Link to the docs: [Creating And Updating Figures](https://plotly.com/python/creating-and-updating-figures/#figures-as-graph-objects)

Hope this helps! 🙂

---

<div class="post-metadata">

**Author:** ![cmcilip](https://avatars.discourse-cdn.com/v4/letter/c/e9c0ed/32.png) [@cmcilip](https://community.plotly.com/u/cmcilip)\
**Post date:** [February 17, 2022, 1:59am UTC](https://community.plotly.com/t/learning-dash-how-to-add-traces/61130/3 "2022-02-17T01:59:36Z")

</div>

Oh great, thank you so much. I used the second example you gave me and it worked great - now I will go learn how to format the legend and the line colors … etc…

Thanks again and also, thanks for the link - it was very useful.
