I’d like to automatically increase the scale_factor
(well I’m after an increased resolution or DPI in general) of my plots when clicking the “Save” button via the Bokeh toolbar on the plot.
Clicking the “Save” button as of present saves image of exactly how it appears on screen. However, when saving a plot, I want this to be a near-publication quality export.
I’m aware of export_png and its scale_factor
argument, but this doesn’t feel right to me as I need to install a headless browser to manually execute this (firefox
and geckodriver
). It feels weird to me to implement a hook that will then re-plot in a headless browser, server side when this could be done directly in the client browser… but maybe that’s the way it needs to be done?
I’m wondering if there is a way to tell bokeh to increase the scale_factor
to some default value so that I can get improved resolutions of my saved plots when clicking the default “Save” button.
Increase the width/height of the plot in my UI isn’t really a solution either as I really want to upscale it or increase the DPI while maintaining relative font size to the plot itself.
import numpy as np
from bokeh.plotting import figure, show
rng = np.random.default_rng()
x = rng.normal(loc=0, scale=1, size=1000)
p = figure(width=670, height=400,
title="Normal (Gaussian) Distribution")
# Histogram
bins = np.linspace(-3, 3, 40)
hist, edges = np.histogram(x, density=True, bins=bins)
p.quad(top=hist, bottom=0, left=edges[:-1], right=edges[1:],
fill_color="skyblue", line_color="white",
legend_label="1000 random samples")
# Probability density function
x = np.linspace(-3.0, 3.0, 100)
pdf = np.exp(-0.5*x**2) / np.sqrt(2.0*np.pi)
p.line(x, pdf, line_width=2, line_color="navy",
legend_label="Probability Density Function")
p.y_range.start = 0
p.xaxis.axis_label = "x"
p.yaxis.axis_label = "PDF(x)"
show(p)
Export I get from the Save button:
If I try to put that plot in a presentation or document, its quite low resolution
The following gives me the desired effect, but I really want this to be handled by the “Save” button… and I just noticed this includes the toolbar which I don’t want in the screenshot
from bokeh.io import export_png
export_png(p, filename='foo.png', scale_factor=3)