Show Sidebar Hide Sidebar

Static Image Export in Python

Plotly allows you to save static images of your plots. Save the image to your local computer, or embed it inside your Jupyter notebooks as a static image.

New to Plotly?

Plotly's Python library is free and open source! Get started by downloading the client and reading the primer.
You can set up Plotly to work in online or offline mode, or in jupyter notebooks.
We also have a quick-reference cheatsheet (new!) to help you get started!

Export Static Images Online

To save the image, you need to login to plotly using your credentials (username and API Key).

In [1]:
import plotly.plotly as py
import plotly.graph_objs as go

py.sign_in('DemoAccount', '2qdyfjyr7o') # Replace the username, and API key with your credentials.

trace = go.Bar(x=[2, 4, 6], y= [10, 12, 15])
data = [trace]
layout = go.Layout(title='A Simple Plot', width=800, height=640)
fig = go.Figure(data=data, layout=layout)

py.image.save_as(fig, filename='a-simple-plot.png')

from IPython.display import Image
Image('a-simple-plot.png')
Out[1]:

Embed Static Images in Jupyter Notebooks

You can also directly render the image inside the IPython notebook using the utility function ishow. It is similar to Matplotlib's imshow method:

In [2]:
py.image.ishow(fig)

Retrieve an Image from an Existing Online Chart

To export an image of a chart you (or someone else) have already created, first you can retreive it using get_figure method, and then save it.

In [3]:
fig = py.get_figure('chris', '1638')
py.image.save_as(fig,'chris-plot.png')

Image('chris-plot.png') # Display a static image
Out[3]:

Supported Formats

The common image formats: 'PNG', 'JPG/JPEG' are supported. In addition, formats like 'EPS', 'SVG' and 'PDF' are also supported.

Note: The SVG, EPS and PDF Formats are only available for Plotly Professional users. You can get more details on our pricing page

To access the image in a particular format, you can either:

Saving Image Data in Memory

The data from the charts can also be stored in the memory. This reduces the overhead of saving the image in the hard disk. It is particularly useful when the image needs to be embedded (for example in email reports).

In [4]:
import requests
image_bytes = requests.get('https://plot.ly/~chris/1638.jpg').content

Export Static Image Offline

We can use the same iplot and plot functions as before, but if we want to download these same images, then we'll have to include an additional argument called image. image will define the format of the image file that is to be downloaded.

import plotly.offline as offline
import plotly.graph_objs as go

offline.init_notebook_mode()

offline.iplot({'data': [{'y': [4, 2, 3, 4]}],
               'layout': {'title': 'Test Plot',
                          'font': dict(size=16)}},
             image='png')


import plotly.offline as offline
import plotly.graph_objs as go

offline.plot({'data': [{'y': [4, 2, 3, 4]}],
               'layout': {'title': 'Test Plot',
                          'font': dict(size=16)}},
             image='png')

Note that you can also define the height, width and filename of the image. See help(offline.iplot) or help(offline.plot) for more information.

Reference

In [5]:
help(py.image)
Help on class image in module plotly.plotly.plotly:

class image
 |  Helper functions wrapped around plotly's static image generation api.
 |
 |  Class methods defined here:
 |
 |  ishow(cls, figure_or_data, format='png', width=None, height=None, scale=None) from __builtin__.classobj
 |      Display a static image of the plot described by `figure_or_data`
 |      in an IPython Notebook.
 |
 |      positional arguments:
 |      - figure_or_data: The figure dict-like or data list-like object that
 |                        describes a plotly figure.
 |                        Same argument used in `py.plot`, `py.iplot`,
 |                        see https://plot.ly/python for examples
 |      - format: 'png', 'svg', 'jpeg', 'pdf'
 |      - width: output width
 |      - height: output height
 |      - scale: Increase the resolution of the image by `scale` amount
 |             Only valid for PNG and JPEG images.
 |
 |      example:
 |      ```
 |      import plotly.plotly as py
 |      fig = {'data': [{'x': [1, 2, 3], 'y': [3, 1, 5], 'type': 'bar'}]}
 |      py.image.ishow(fig, 'png', scale=3)
 |
 |  save_as(cls, figure_or_data, filename, format=None, width=None, height=None, scale=None) from __builtin__.classobj
 |      Save a image of the plot described by `figure_or_data` locally as
 |      `filename`.
 |
 |      Valid image formats are 'png', 'svg', 'jpeg', and 'pdf'.
 |      The format is taken as the extension of the filename or as the
 |      supplied format.
 |
 |      positional arguments:
 |      - figure_or_data: The figure dict-like or data list-like object that
 |                        describes a plotly figure.
 |                        Same argument used in `py.plot`, `py.iplot`,
 |                        see https://plot.ly/python for examples
 |      - filename: The filepath to save the image to
 |      - format: 'png', 'svg', 'jpeg', 'pdf'
 |      - width: output width
 |      - height: output height
 |      - scale: Increase the resolution of the image by `scale` amount
 |             Only valid for PNG and JPEG images.
 |
 |      example:
 |      ```
 |      import plotly.plotly as py
 |      fig = {'data': [{'x': [1, 2, 3], 'y': [3, 1, 5], 'type': 'bar'}]}
 |      py.image.save_as(fig, 'my_image.png', scale=3)
 |      ```
 |
 |  ----------------------------------------------------------------------
 |  Static methods defined here:
 |
 |  get(figure_or_data, format='png', width=None, height=None, scale=None)
 |      Return a static image of the plot described by `figure_or_data`.
 |
 |      positional arguments:
 |      - figure_or_data: The figure dict-like or data list-like object that
 |                        describes a plotly figure.
 |                        Same argument used in `py.plot`, `py.iplot`,
 |                        see https://plot.ly/python for examples
 |      - format: 'png', 'svg', 'jpeg', 'pdf'
 |      - width: output width
 |      - height: output height
 |      - scale: Increase the resolution of the image by `scale`
 |               amount (e.g. `3`)
 |               Only valid for PNG and JPEG images.
 |
 |      example:
 |      ```
 |      import plotly.plotly as py
 |      fig = {'data': [{'x': [1, 2, 3], 'y': [3, 1, 5], 'type': 'bar'}]}
 |      py.image.get(fig, 'png', scale=3)
 |      ```

In [7]:
import plotly.offline as offline
help(offline.plot)
Help on function plot in module plotly.offline.offline:

plot(figure_or_data, show_link=True, link_text='Export to plot.ly', validate=True, output_type='file', include_plotlyjs=True, filename='temp-plot.html', auto_open=True, image=None, image_filename='plot_image', image_width=800, image_height=600)
    Create a plotly graph locally as an HTML document or string.

    Example:
    ```
    from plotly.offline import plot
    import plotly.graph_objs as go

    plot([go.Scatter(x=[1, 2, 3], y=[3, 2, 6])], filename='my-graph.html')
    # We can also download an image of the plot by setting the image parameter
    # to the image format we want
    plot([go.Scatter(x=[1, 2, 3], y=[3, 2, 6])], filename='my-graph.html'
         image='jpeg')
    ```
    More examples below.

    figure_or_data -- a plotly.graph_objs.Figure or plotly.graph_objs.Data or
                      dict or list that describes a Plotly graph.
                      See https://plot.ly/python/ for examples of
                      graph descriptions.

    Keyword arguments:
    show_link (default=True) -- display a link in the bottom-right corner of
        of the chart that will export the chart to Plotly Cloud or
        Plotly Enterprise
    link_text (default='Export to plot.ly') -- the text of export link
    validate (default=True) -- validate that all of the keys in the figure
        are valid? omit if your version of plotly.js has become outdated
        with your version of graph_reference.json or if you need to include
        extra, unnecessary keys in your figure.
    output_type ('file' | 'div' - default 'file') -- if 'file', then
        the graph is saved as a standalone HTML file and `plot`
        returns None.
        If 'div', then `plot` returns a string that just contains the
        HTML <div> that contains the graph and the script to generate the
        graph.
        Use 'file' if you want to save and view a single graph at a time
        in a standalone HTML file.
        Use 'div' if you are embedding these graphs in an HTML file with
        other graphs or HTML markup, like a HTML report or an website.
    include_plotlyjs (default=True) -- If True, include the plotly.js
        source code in the output file or string.
        Set as False if your HTML file already contains a copy of the plotly.js
        library.
    filename (default='temp-plot.html') -- The local filename to save the
        outputted chart to. If the filename already exists, it will be
        overwritten. This argument only applies if `output_type` is 'file'.
    auto_open (default=True) -- If True, open the saved file in a
        web browser after saving.
        This argument only applies if `output_type` is 'file'.
    image (default=None |'png' |'jpeg' |'svg' |'webp') -- This parameter sets
        the format of the image to be downloaded, if we choose to download an
        image. This parameter has a default value of None indicating that no
        image should be downloaded. Please note: for higher resolution images
        and more export options, consider making requests to our image servers.
        Type: `help(py.image)` for more details.
    image_filename (default='plot_image') -- Sets the name of the file your
        image will be saved to. The extension should not be included.
    image_height (default=600) -- Specifies the height of the image in `px`.
    image_width (default=800) -- Specifies the width of the image in `px`.

Still need help?
Contact Us

For guaranteed 24 hour response turnarounds, upgrade to a Developer Support Plan.