Static plots in Jupyter envs

#1

At this moment, is this the optimal way of producing static plots in Jupyter Labs with plotly:

import numpy as np

import plotly.graph_objs as go
import plotly.io as pio
from IPython.display import Image
fig = go.FigureWidget()
fig.add_scatter(y=np.random.lognormal(size=500000))

img_bytes = pio.to_image(fig, format='png', width=600, height=350, scale=20)
Image(img_bytes, width=600, height=350)

or is it possible to achieve the same simply by passing some argument somewhere?

#2

Hi,

At the moment, I think this is the way to do it indeed.
Another way to do it: If you’re using nbconvert to convert your notebook to LaTeX for example (HTML works similarly I think), the plotly figures will be automatically converted to PNG.

I tag @jmmease; His rework on the renderers (https://github.com/plotly/plotly.py/pull/1474) was merged a few days ago into the master branch. With the new renderers, it will be possible to do static plots by passing an argument.

Cheers
GorgiAstro

#3

Thanks @GorgiAstro,

Yes, when plotly.py 3.8.0 is released (any day now :slightly_smiling_face:), you could do this with the new renderers subsystem. In particular:

import plotly.io as pio
pio.renderers.default = 'png'

fig = ...
pio.show(fig)

You can try out version 3.8.0rc1 if you’d like to give it a spin now. See https://github.com/plotly/plotly.py/blob/release_3.8.0/README.md#installation for instructions.

-Jon

1 Like