How to use export_png() function to save plot as PNG In Python

Using the export_png() function, we can export our plot as a PNG image directly from the Python code.

Syntax: export_png(obj, filename, width, height, webdriver)

Arguments:

  • obj: obj can be any plot that we are going to export.
  • filename: It is an optional argument, the default plot filename will be the python file name.
  • width: It is an optional argument,  used to set the width of the exported plot layout obj, by default it will be ignored.
  • height: It is an optional argument, used to set the height of the exported plot layout obj, by default it will be ignored.
  • webdriver: It is an optional argument, used to set the default web driver instance to use to export the plot. A selenium web driver is by default if we don’t specify anything.

First, prepare the data to be visualized then call the figure() function to creates a plot with the default properties, such as its title and axes labels. Use the different varieties of renderers to create different varieties of plots. For example, to render a circle, we can use the circle() function instead of line() to render circles. Save the plot using export_png(plot_obj, filename) function and then display the resultant plot using show() function.

Python3




# importing necessary libraries
from bokeh.plotting import figure
from bokeh.plotting import output_file
from bokeh.plotting import show
from bokeh.io import export_png
  
# dummy data
x = [2, 4, 8, 10, 12, 14]
y = [22, 54, 18, 50, 22, 24]
  
# set output to static HTML file
output_file("line.html")
  
# Adding plot
fig = figure(
    title="Bokeh Plot",
    x_axis_label='x-axis',
    y_axis_label='y-axis',)
  
# add a line renderer to plot line
fig.line(x, y)
  
# saving the plot on disk
print('Exporting bokeh_plot.png.....')
export_png(fig, filename = "bokeh_plot.png")
  
# displaying plot
show(fig)


Output:

Exporting bokeh_plot.png....

Exported as PNG

Exporting Bokeh Plots

Bokeh is an interactive data visualization library available for Python. Using Bokeh we can embed our plot in any HTML file. It internally uses HTML and JavaScript to render the plot in Web Browsers for representation. Under the hood, it converts the data source into a JSON file which is used as input for BokehJS (a JavaScript library) and renders the visualizations in modern browsers. In this article, we will see how we can export/save a Bokeh plot to local storage.

We will need the following dependencies to export the plots in bokeh:

  • Selenium
  • WebDriver

To install these two using conda, run the following command one after another:

conda install selenium geckodriver -c conda-forge
conda install selenium python-chromedriver-binary -c conda-forge

using pip:

pip install selenium geckodriver firefox

Similar Reads

Method 1: Using export_png() function to save plot as PNG

Using the export_png() function, we can export our plot as a PNG image directly from the Python code....

Method 2: Using export_svg() function to save plot as SVG

...