# Convert go to data

**URL:** <https://community.plotly.com/t/convert-go-to-data/73191>\
**Category:** 📊 Plotly Python\
**Created:** [March 2, 2023, 7:11am UTC](https://community.plotly.com/t/convert-go-to-data/73191 "2023-03-02T07:11:27Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bijan](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/bijan/32/22061_2.png) [@Bijan](https://community.plotly.com/u/Bijan)\
**Post date:** [March 2, 2023, 7:11am UTC](https://community.plotly.com/t/convert-go-to-data/73191/1 "2023-03-02T07:11:27Z")

</div>

Hi there,

Why I get error for the following code???

```auto
import pandas as pd
import plotly.graph_objs as go

df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')

# Build Graph
web = go.Scatter(x=df['Date'],y=df['AAPL.High'],fill='tozeroy',line=dict(color='rgb(145, 50, 125)',),name = 'Web')

Tup = (web,)

print(type(Tup))

fig = go.Figure()

fig.data=Tup

fig.show()

```

For some reasons I need to use `fig.data=Tup` after creating the Figure object. What is my fault?

---

<div class="post-metadata">

**Author:** ![AIMPED](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/aimped/32/21758_2.png) [@AIMPED](https://community.plotly.com/u/AIMPED)\
**Post date:** [March 2, 2023, 7:31am UTC](https://community.plotly.com/t/convert-go-to-data/73191/2 "2023-03-02T07:31:13Z")

</div>

Hi @Bijan ,

remove the “,” from this line

> [@Bijan](#):
>
> `Tup = (web,)`

and change this line

> [@Bijan](#):
>
> `fig.data=Tup`

into this:  
`fig.add_trace(Tup)`

---

<div class="post-metadata">

**Author:** ![Bijan](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/bijan/32/22061_2.png) [@Bijan](https://community.plotly.com/u/Bijan)\
**Post date:** [March 2, 2023, 7:36am UTC](https://community.plotly.com/t/convert-go-to-data/73191/3 "2023-03-02T07:36:15Z")

</div>

Dear @AIMPED , Thanks for contribution

The problem is that I don’t want to add, I want to set to be equal! (The only set of data be Tup)

Also I don’t want to create a new Figure and Just want to set its Data just equal to Tup!

To be more explicit about my problem:

Imagine that I have a figure with some layout settings and some plotted data, Now I need without remove the figure or without creating a new figure, just set its data to Tup.

---

<div class="post-metadata">

**Author:** ![AIMPED](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/aimped/32/21758_2.png) [@AIMPED](https://community.plotly.com/u/AIMPED)\
**Post date:** [March 2, 2023, 7:49am UTC](https://community.plotly.com/t/convert-go-to-data/73191/4 "2023-03-02T07:49:01Z")

</div>

Hi @Bijan

I would update the trace:

```python
import pandas as pd
import plotly.graph_objs as go

df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')

# Initial figure
web = go.Scatter(
    x=df['Date'],
    y=df['AAPL.High'],
    fill='tozeroy',
    line=dict(color='rgb(145, 50, 125)'),
    name = 'Web'
)
fig = go.Figure(data=web)

# change data
fig.update_traces({'y': df['AAPL.Volume']})

fig.show()

```

---

<div class="post-metadata">

**Author:** ![Bijan](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/bijan/32/22061_2.png) [@Bijan](https://community.plotly.com/u/Bijan)\
**Post date:** [March 2, 2023, 8:06am UTC](https://community.plotly.com/t/convert-go-to-data/73191/5 "2023-03-02T08:06:52Z")

</div>

Dear @AIMPED

Is there any reason that we can’t assign any type of data to `fig.data` or `fig['data']`?

Look at the following code:

```auto
import pandas as pd
import plotly.graph_objs as go

df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')

fig = go.Figure()
fig.add_scatter(x=df['Date'],y=df['AAPL.Volume'])

# Build Graph
web = go.Scatter(x=df['Date'],y=df['AAPL.High'],fill='tozeroy',line=dict(color='rgb(145, 50, 125)',),name = 'Web')

Tup = (web)
Tup=go.Figure(data=Tup)

fig['data']=fig['data']

fig.show()

```

It works!!! But if I change the line `fig['data']=fig['data']` with `fig['data']=Tup['data']` it doesn’t work!!! Why it happens?

---

<div class="post-metadata">

**Author:** ![AIMPED](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/aimped/32/21758_2.png) [@AIMPED](https://community.plotly.com/u/AIMPED)\
**Post date:** [March 2, 2023, 8:51am UTC](https://community.plotly.com/t/convert-go-to-data/73191/6 "2023-03-02T08:51:28Z")

</div>

Hi @Bijan, I really don’t understand why you want to do this. There is a way to do so, but I personally would not do it and use the `update_traces` method. Anyway, since it seems to be important for you here is the possible solution:

```auto
import pandas as pd
import plotly.graph_objs as go

# data source
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')

# Initial figure
fig = go.Figure(
        go.Scatter(
        x=df['Date'],
        y=df['AAPL.High'],
        fill='tozeroy',
        line=dict(color='rgb(145, 50, 125)'),
        name = 'Web'
    )
)

# Bijan's tuple
Tup = (
    go.Scatter(
        x=df['Date'],
        y=df['AAPL.Volume']
    )
)

# convert fig to dictionary
fig = fig.to_dict()

# update dictionary with Bijan's tuple
fig['data'] = Tup

# create figure from dictionary and show it
fig = go.Figure(fig)
fig.show()

```

---

<div class="post-metadata">

**Author:** ![Bijan](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/bijan/32/22061_2.png) [@Bijan](https://community.plotly.com/u/Bijan)\
**Post date:** [March 2, 2023, 9:56am UTC](https://community.plotly.com/t/convert-go-to-data/73191/7 "2023-03-02T09:56:12Z")

</div>

Thanks @AIMPED  
Really thanks for all of your always guides and helps.

So Finally also you had to use `fig = go.Figure()` command again in last line.

> [@AIMPED](#):
>
> Hi @Bijan, I really don’t understand why you want to do this

Creating a new figure needs to define layouts. In a project I have more than 50 figures and each one have different designed layout and also may have various traces. I want to keep their layouts and simply when I create a new data using `go` just replace all created data by `go` instead of current data. `update_traces` needs to specify the parameter/s name and imagine how hard can it be if you have various figures with various types and various parameters and  
it can be imagine how can be simple if we can replace the created data by `go` into an existing figure with its defined layout and (I think it maybe become faster than recreating figure)

Any way thanks because of your patient and answers 🌹

---

<div class="post-metadata">

**Author:** ![AIMPED](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/aimped/32/21758_2.png) [@AIMPED](https://community.plotly.com/u/AIMPED)\
**Post date:** [March 2, 2023, 1:11pm UTC](https://community.plotly.com/t/convert-go-to-data/73191/8 "2023-03-02T13:11:13Z")

</div>

> [@Bijan](#):
>
> So Finally also you had to use `fig = go.Figure()` command again in last line.

This is only for converting the dictionary into a figure object. The layout of the initial figure is not changed.
