Convert go to data

Hi there,

Why I get error for the following code???

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?

Hi @Bijan ,

remove the “,” from this line

and change this line

into this:
fig.add_trace(Tup)

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.

Hi @Bijan

I would update the trace:

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()

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:

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?

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:

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()

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.

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 :rose:

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

1 Like