Black Lives Matter. Please consider donating to Black Girls Code today.

Errors with displaying plotly graph objects

I had been running a code in Jupyter Notebook that had been working fine before. I’m using version 4.5.0 of plotly. The code added traces and output a plot, but now I see the error:

AttributeError: ‘Figure’ object has no attribute ‘update_layout’

I commented out the update_layout part, but now it looks like the figure simply doesnt show up. This is what my original function that was working looks like:

import pandas as pd
import numpy as np
import plotly.graph_objects as go
import datetime as dt


def create_scatter(df,ticker_list,y_column_list,title_name='',xlabel='',ylabel=''):
    fig = go.Figure()
    for y in y_column_list:
        for t in ticker_list:
            fig.add_trace(go.Scatter(x=df[df.entity_code == t].date,y=df[df.entity_code == t][y],name=t))
    fig.update_layout(
                title={'text': title_name,
                               'y':0.9,
                               'x':0.5,
                       'xanchor': 'center',
                       'yanchor': 'top'},
                xaxis_title=xlabel,
                yaxis_title=ylabel,
                showlegend=True)
    fig.show()   

Any help would be appreciated, thank you!

Update: Even when I run the example code in the documentation, I get the same attribute error.

import plotly.graph_objects as go

fig = go.Figure()

fig.add_trace(go.Scatter(
x=[0, 1, 2, 3, 4, 5, 6, 7, 8],
y=[0, 1, 2, 3, 4, 5, 6, 7, 8],
name=“Name of Trace 1” # this sets its legend entry
))

fig.add_trace(go.Scatter(
x=[0, 1, 2, 3, 4, 5, 6, 7, 8],
y=[1, 0, 3, 2, 5, 4, 7, 6, 8],
name=“Name of Trace 2”
))

fig.update_layout(
title=“Plot Title”,
xaxis_title=“x Axis Title”,
yaxis_title=“y Axis Title”,
font=dict(
family=“Courier New, monospace”,
size=18,
color="#7f7f7f"
)
)

fig.show()

Hi @meaydemi! Thanks for debugging your problem up to the point that you could provide a reproducible example, this really helps. So this is not normal at all. Can you confirm that you’re using plotly 4.5.0 and not another version by printing within the same notebook session print(plotly.__version__). Thanks!

Thanks for the reply Emmanuelle. I realized what my mistake was. I initially had created a new environment and deliberately installed a much older version of Plotly (3.10.0). When I upgraded to 4.5.0 I never actually restarted the kernel in notebook. It seems that even though package installations are read in instantly (for example I also installed sklearn and didn’t need to restart kernel), it looks like not everything gets picked up? Anyways, after I did the kernel restart it worked as expected.

Cool, happy to know you solved your problem. Yeah, you have to reload(plotly) if you changed the version after you had already installed it. But restarting the kernel is a cleaner solution.