Plotly Object displays as blank in DataBricks

Viewed 804

I have a plotly object that should be showing up properly but for some reason it only shows up blank in DataBricks. The object type is:

plotly.graph_objs._figure.Figure

I have tried the following to display the figure:

fig.show()
display(fig)
displayHTML(fig.to_html())

All possible solutions I can think of result in the same thing. Thanks! Btw... Using Plotly Version 4.9

2 Answers

Try using the plot method from plotly.offline. This following is from DataBricks documentation, but Jupyter notebooks have a similar issue where a Plotly graph_object Figure won't render unless you use plotly.offline.

from plotly.offline import plot
import plotly.graph_objects as go

# Instead of simply calling plot(...), store your plot as a variable and pass it to displayHTML().
# Make sure to specify output_type='div' as a keyword argument.
# (Note that if you call displayHTML() multiple times in the same cell, only the last will take effect.)

p = plot(
  [ ## define your go.Figure here ##

  ],
  output_type='div'
)

displayHTML(p)

Worth noting that you can similarly use the plot method from plotly.offline to display plotly.express plots.

This saves a lot of code for simple plots where graph_objects are overkill!

from plotly.offline import plot
import plotly.express as px

p = plot(
    px.scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16]),
    output_type='div'
    )

displayHTML(p)
Related