AttributeError: 'NoneType' object has no attribute 'layout'

Hi Guys.

I would be grateful if someone could help me with the problem below.

IĀ“m trying to run the code below, but appeared an error only in the ā€œdebug modeā€ (ā€œCtrl + F10ā€) in Spyder IDE.
Running the whole code (F5) and selection (F9) is working correctly.

  • Spyder version: 5.4.3 (conda)
  • Python version: 3.8.16 64-bit
  • Operating System: Windows 10
  • numpy v1.24.3
  • plotly v5.14.1
  • spyder v5.4.3
  • Code Example:

import numpy as np
import plotly.express as px

x_data = [1,2,3,4]
y_data = [1,4,9,16]

fig = px.scatter(x= x_data, y = y_data)
plot(fig)

  • Message in IPython Console:

    File ~\Doutorado\Python\Feeders\outlier_class.py:85 in grafico_dispersao
    fig = px.scatter(x= x_data, y = y_data)

    File ~\Anaconda3\lib\site-packages\plotly\express_chart_types.py:66 in scatter
    return make_figure(args=locals(), constructor=go.Scatter)

    File ~\Anaconda3\lib\site-packages\plotly\express_core.py:1994 in make_figure
    apply_default_cascade(args)

    File ~\Anaconda3\lib\site-packages\plotly\express_core.py:932 in apply_default_cascade
    x[1] for x in args[ā€œtemplateā€].layout.colorscale.sequential

AttributeError: ā€˜NoneTypeā€™ object has no attribute ā€˜layoutā€™

Thanks.

I think itā€™s saying you need to add layout configs

Hi @efremfer !

It is maybe a typo, but why are you using plot(fig) and not fig.show() ?
(first time I see this syntax)

For the plot rendering you can take a look at the doc Displaying Figures in Python

hi @efremfer
Welcome to the community :wave:

Iā€™m confident that @allsyntax and @Skiks will be able to support you in finding an answer.

For any questions you ask in the future, I highly recommend you read over this post. It will help make your posts more readable and even easier to understand and respond to.

Hi @Skiks .

Answering your question, I used ā€œplot(fig)ā€ instead of ā€œfig.show()ā€ because the first struct didĀ“nt show the plot in my web browser (chrome).

But the issue is when I run ā€œfig = px.scatter(x= x_data, y = y_data)ā€ in debug mode (Ctr + F10) that appear the error showed above.

Thanks.

Hi @allsyntax.

Thank you for your attention, but I dont have access to ā€œlayoutā€.

I only using o method sccater of library ā€œplotly expressā€ through the code below:

fig = px.scatter(x= x_data, y = y_data)

maybe you can try fig.show() instead of plot(fig)

also try adding template=None to the end of your fig =px.scatter line

fig = px.scatter(x=x_data, y=y_data, template=None)

Hello @allsyntax.

I tried the code plot(fig) and it works well. :slight_smile:

HoweverI tried the parameter ā€œtemplate=Noneā€, but it didā€™nt work. I have got the same error!! :frowning:

fig = px.scatter(x= x_data, y = y_data, template=None)

I donĀ“t know what to do to solve this problem.

Ok, so maybe fig.show was all you needed and do not need to add template=None

Hi @allsyntax ,

 I still have the error to be solved in the line code below.

fig = px.scatter(x= x_data, y = y_data)

Maybe check and make sure you donā€™t have another variable or a module named px. But the code you posted seems fine on its own and should work. You might need to provide more context or try debugging to pinpoint where the issue is.

Hi, did you find any solution to this? I am experiencing the same error when plotting using px.scatter without any template.

The tracebacks I have are:

  File "/usr/local/lib/python3.10/site-packages/plotly/express/_chart_types.py", line 264, in line
    return make_figure(args=locals(), constructor=go.Scatter)
  File "/usr/local/lib/python3.10/site-packages/plotly/express/_core.py", line 2213, in make_figure
    elif args["template"].layout.margin.t is None:
AttributeError: 'NoneType' object has no attribute 'layout'

So to debug, I went to line 2213 of plotly.express._core, and I found this line a little confusing: plotly.py/_core.py at 981ace849c9749ff9e600a9f0ac1ac97b14c202d Ā· plotly/plotly.py Ā· GitHub
It seems only when title is not None, it is checking the args['template'].layout? Is it intended?