7. Saving Plotly Figures#
Getting ready#
Install the library
kaleidousing
pip install -U kaleido
The -U option here means upgrade and tells pip to upgrade all specified packages to the newest available version.
Create a figure object using
plotly.express
import numpy as np
import plotly
import plotly.express as px
x = np.linspace(-10, 10, 300)
y = np.sin(x)
fig = px.line(x=x, y=y)
fig.show()
How to do it#
Saving Figures as HTML#
Save the figure as a
html
plotly.offline.plot(fig, filename='images/fig_recipe07.html')
'images/fig_recipe07.html'
Note that this opens the open the saved file in a web browser after saving. To avoid this behaviour, simply set the argument auto_open as False.
plotly.offline.plot(fig, filename='images/fig_recipe07.html', auto_open=False)
'images/fig_recipe07.html'
Saving Figures as Static Objects#
Verify that the library
kaleidois present in your virtual environment. From the terminal, yo can do this by using the command
pip show kaleido
If you are working in Jupyter, then you can use the following to show the version. You will see an error if kaleido is not install.
Note: If you are working on Jupyter, remember to restart your Kernel after installing a new package.
from importlib.metadata import version
version('kaleido')
'0.2.1'
Save the figure using the method
write_image. Use the argumentsfileandformatto specify a local file path or a writeable object for the figure to be saved, and the format in which it will be saved. You can choose any of the following formats:
‘png’
‘jpg’ or ‘jpeg’
‘webp’
‘svg’
‘pdf’
‘eps’ Note that this requires the
popplerlibrary to be available in the local environment.
fig.write_image(file ="images/fig_recipe07.png", format="png")
Alternatively, you can simply provide the file arument passing a string with the desired extension as folllows:
fig.write_image("images/fig_recipe07.png")
fig.write_image("images/fig_recipe07.webp")
fig.write_image("images/fig_recipe07.pdf")
If format is not specified and file is not a string then format will default to:
plotly.io.kaleido.scope.default_format if engine is “kaleido”
plotly.io.orca.config.default_format if engine is “orca”
To control the resolution of the saved image, use the argument
scale. This is a numerical value (integer or float) which represents the scale factor to be used when exporting the figure. A scale factor larger than 1.0 will increase the image resolution with respect to the figure’s layout pixel dimensions. Whereas as scale factor of less than 1.0 will decrease the image resolution.
fig.write_image(file ="images/fig_recipe07_high.png", format="png", scale=2)
fig.write_image(file ="images/fig_recipe07_low.png", format="png", scale=0.5)
To control the heigh and width of the saved image, use the arguments:
width(int or None) – Represents the width of the exported image in layout pixels. If thescaleis 1.0, this will also be the width of the exported image in physical pixels.height(int or None) – Represents the height of the exported image in layout pixels. If thescaleproperty is 1.0, this will also be the height of the exported image in physical pixels.
fig.write_image(file ="images/fig_recipe07_size.png",
height = 1000, width = 1600)