Last modified: Jan 02, 2025 By Alexander Williams
Python Bokeh save(): Export Plots to Static Files Guide
The Bokeh library in Python offers powerful visualization capabilities, and one essential feature is the ability to save plots as static files using the save()
function. This guide will show you how to effectively use this functionality.
Understanding Bokeh save() Function
The save()
function allows you to export your Bokeh visualizations to various file formats, making them suitable for documentation, presentations, or sharing with others who don't have Bokeh installed.
Basic Usage of save()
Here's a simple example demonstrating how to create a basic plot and save it:
from bokeh.plotting import figure, save
from bokeh.io import output_file
# Create a simple plot
p = figure(width=400, height=300, title="Basic Example")
p.line([1, 2, 3, 4, 5], [2, 5, 8, 2, 7])
# Save the plot
output_file("basic_plot.html")
save(p)
This example creates a simple line plot and saves it as an HTML file. Similar to how you might create interactive visualizations in Bokeh line plots.
Saving Different File Formats
While HTML is the default format, Bokeh supports multiple export formats through the export_png()
and export_svg()
functions:
from bokeh.io import export_png, export_svg
# Create your plot
p = figure(width=400, height=300)
p.circle([1, 2, 3], [4, 5, 6])
# Export as PNG
export_png(p, filename="plot.png")
# Export as SVG
export_svg(p, filename="plot.svg")
Adding Interactive Features Before Saving
You can add interactive features to your plot before saving it. This is particularly useful when saving to HTML format, as these features will be preserved:
from bokeh.models import HoverTool
from bokeh.plotting import figure, save
from bokeh.io import output_file
# Create plot with hover tool
p = figure(width=400, height=300)
p.add_tools(HoverTool(tooltips=[("x", "@x"), ("y", "@y")]))
p.circle([1, 2, 3], [4, 5, 6])
# Save interactive plot
output_file("interactive_plot.html")
save(p)
This example incorporates hover tooltips, similar to what you might learn in the Bokeh HoverTool guide.
Best Practices for Saving Plots
File Naming: Use clear, descriptive filenames that indicate the plot's content.
Resolution Settings: When exporting to PNG, consider setting appropriate DPI (dots per inch) for quality:
from bokeh.io import export_png
# Export with custom DPI
export_png(p, filename="high_res_plot.png", height=600, width=800)
Saving Multiple Plots
When working with multiple plots, you can save them together using layouts:
from bokeh.layouts import column
from bokeh.plotting import figure, save
from bokeh.io import output_file
# Create two plots
p1 = figure(width=300, height=200)
p1.line([1, 2, 3], [4, 5, 6])
p2 = figure(width=300, height=200)
p2.circle([1, 2, 3], [4, 5, 6])
# Combine and save
layout = column(p1, p2)
output_file("multiple_plots.html")
save(layout)
For more complex layouts, you might want to explore the Bokeh gridplot functionality.
Common Issues and Solutions
Missing Dependencies: When exporting to PNG or SVG, ensure you have selenium and chrome/chromium installed:
pip install selenium
pip install chromedriver-binary
File Permissions: Ensure you have write permissions in the target directory.
Conclusion
The Bokeh save()
function is a versatile tool for exporting your visualizations. Whether you need static images or interactive HTML files, proper understanding of this function is essential for sharing your data visualizations effectively.