GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing plotly.com — Static image export in Python This page demonstrates how to export interactive Plotly figures to static image formats like PNG, JPEG, SVG, and PDF. If you want to export Plotly figures to HTML to retain interactivity, see the Interactive HTML Export page Install Dependencies¶Kaleido¶Static image generation requires Kaleido version 1.0.0 or greater. Install Kaleido with pip:
or with conda:
Chrome¶Kaleido uses Chrome for static image generation. Starting with Kaleido version 1.0.0, Chrome is not included in the package itself; instead, Kaleido looks for a compatible version of Chrome (or Chromium) already installed on the machine on which it's running. If you don't have Chrome installed, you can install it directly from Google following the instructions for your operating system. Plotly also provides a CLI for installing Chrome from the command line. Run You can also install Chrome from Python using import plotly.io as pio pio.get_chrome() See the Additional Information on Browsers with Kaleido section below for more details on browser compatibility for Kaleido. Write Image to a File¶Plotly figures have a To export a figure using Raster Formats¶PNG import plotly.express as px data_canada = px.data.gapminder().query("country == 'Canada'") fig = px.bar(data_canada, x='year', y='pop') fig.write_image("fig1.png") JPEG ... fig.write_image("images/fig1.jpeg") WebP ... fig.write_image("images/fig1.webp") Vector Formats¶SVG ... fig.write_image("images/fig1.svg") ... fig.write_image("images/fig1.pdf") Note: Figures containing WebGL traces (i.e. of type Note that Plotly Express automatically uses WebGL for scatter-type figures with more than 1,000 data points: the default Specify a Format¶In the earlier example, Plotly inferred the image format from the extension of the filename. You can also specify the format explicitly using the import plotly.express as px data_canada = px.data.gapminder().query("country == 'Canada'") fig = px.bar(data_canada, x='year', y='pop') fig.write_image("fig1", format="png") Write Multiple Images¶Kaleido v1 and later
import plotly.graph_objects as go import plotly.express as px import plotly.io as pio fig1 = go.Figure( data=go.Scatter(x=[1, 2, 3], y=[4, 5, 6], mode='lines+markers'), layout=go.Layout(title='Line Chart') ) fig2 = go.Figure( data=go.Bar(x=['A', 'B', 'C'], y=[10, 5, 15]), layout=go.Layout(title='Bar Chart') ) fig3 = px.pie( values=[30, 20, 10, 40], names=['A', 'B', 'C', 'D'], title='Pie Chart' ) pio.write_images( fig=[fig1, fig2, fig3], file=['export_images/line_chart.png', 'export_images/bar_chart.png', 'export_images/pie_chart.png'] ) Get Image as Bytes¶As well as exporting to a file, Plotly figures also support conversion to a bytes object.
To convert a figure to a PNG bytes object, call the figure's In [1]:
import plotly.express as px data_canada = px.data.gapminder().query("country == 'Canada'") fig = px.bar(data_canada, x='year', y='pop') img_bytes = fig.to_image(format="png") Here's the bytes object displayed using In [2]:
from IPython.display import Image Image(img_bytes) Out[2]:
Specify Image Dimensions and Scale¶In addition to the image format, the In [3]:
img_bytes = fig.to_image(format="png", width=600, height=350, scale=2) Image(img_bytes) Out[3]:
plotly.io Functions¶Previous examples on this page access The following example uses the import plotly.io as pio fig = dict({ "data": [{"type": "bar", "x": [1, 2, 3], "y": [1, 3, 2]}], "layout": {"title": {"text": "A Figure Specified By Python Dictionary"}} }) pio.write_image(fig, "fig.png") Image Export Settings¶As well as configuring height, width, and other settings by passing arguments when calling How to Configure¶Image export settings can be configured by setting their values in To set the import plotly.io as pio pio.defaults.default_format = "jpeg" You can also access current defaults. To see the default value for height: import plotly.io as pio pio.defaults.default_height Available Settings¶The following settings are available:
Additional Information on Browsers with Kaleido¶When exporting images from Plotly.py, Kaleido will attempt to find a version of Chrome or Chromium that it can use for the export. It checks in the operating system's PATH for executables with the following names: "chromium", "chromium-browser", "chrome", "Chrome", "google-chrome" "google-chrome-stable", "Chrome.app", "Google Chrome", "Google Chrome.app", and "Google Chrome for Testing". Kaleido will also check the following locations: Windows
Linux"
Mac OS
Most recent versions of Chrome or Chromium should work with Kaleido. Other Chromium-based browsers may also work, though Kaleido won't discover them automatically. You can set a browser to use by setting the path to search using an environment variable called
What About Dash?¶Dash is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library. Learn about how to install Dash at https://dash.plot.ly/installation. Everywhere in this page that you see import plotly.graph_objects as go # or plotly.express as px fig = go.Figure() # or any Plotly Express function e.g. px.bar(...) # fig.add_trace( ... ) # fig.update_layout( ... ) from dash import Dash, dcc, html app = Dash() app.layout = html.Div([ dcc.Graph(figure=fig) ]) app.run(debug=True, use_reloader=False) # Turn off reloader if inside Jupyter
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |