question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SVG Support for Bokeh Backend

See original GitHub issue

I wrote this a while back for HoloExt; was wondering if there’s any interest for me to make a PR for this?

import os
from bokeh import models
from bokeh.io import export_svgs
from holoext.utils import DEFAULT_N, get_cmap, flatten, tidy_fn

def _get_figures_core(objs):
    if isinstance(objs, list):
        objs = [_get_figures_core(plot) for plot in objs]
    elif isinstance(objs, (models.Column, models.Row)):
        objs = [_get_figures_core(child) for child in objs.children
                if not isinstance(child, (models.ToolbarBox,
                                          models.WidgetBox))]
    return objs

def _get_figures(objs):
    try:
        return list(flatten(_get_figures_core(objs)))
    except TypeError:
        return [_get_figures_core(objs)]

def _save_to_svg(hv_obj, save):
    bokeh_obj = hv.renderer('bokeh').get_plot(hv_obj).state
    figures = _get_figures(bokeh_obj)
    for i, figure in enumerate(figures):
        figure.output_backend = 'svg'

        if len(figures) != 1:
            if not os.path.exists(save):
                os.mkdir(save)
            tidied_title = tidy_fn(figure.title.text)
            save_fp = os.path.join(
                save, '{0}_{1}'.format(tidied_title, i))
        else:
            save_fp = save

        if not save_fp.endswith('svg'):
            save_fp = '{0}.{1}'.format(save_fp, 'svg')

        export_svgs(figure, save_fp)
import numpy as np
import holoviews as hv
hv.extension('bokeh')

frequencies = [0.5, 0.75, 1.0, 1.25]

def sine_curve(phase, freq):
    xvals = [0.1* i for i in range(100)]
    return hv.Curve((xvals, [np.sin(phase+freq*x) for x in xvals]))

curve_dict = {f:sine_curve(0,f) for f in frequencies}

NdLayout = hv.NdLayout(curve_dict, kdims='frequency')
_save_to_svg(NdLayout, 'ndlayout_svgs')

image image

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:4
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

9reactions
RColettocommented, Sep 26, 2018

Hi guys, I think I’m missing something. I generated from Holoviews some graphs (Scatterplot, Violinplot) and managed to save all of them in svg format, using the following code:

#set the backend and the renderer
hv.extension('bokeh')
br = hv.renderer('bokeh')

#set the data
np.random.seed(37)
groups = [chr(65+g) for g in np.random.randint(0, 3, 200)]

#plot 
violin = hv.Violin((groups, np.random.randint(0, 5, 200), np.random.randn(200)),
          ['Group', 'Category'], 'Value')

#export the plot
plot = br.get_plot(violin )
plot = plot.state
plot.output_backend='svg'
export_svgs(plot, filename="Violin.svg")

It seems to work properly, so I can’t understand why Bokeh’s guide (https://bokeh.pydata.org/en/latest/docs/user_guide/compat.html) and the code behind Bokeh’s renderer in Holoviews state it is not possible doing this (i.e. exporting to svg format using Bokeh backend, while it is possible from matplotlib). Is there any limitation I can’t see that is preventing this function to be included in Holoviews?

5reactions
Sieboldianuscommented, Nov 3, 2020

As of Bokeh 0.12.6 it is also possible to export to SVG directly from python. I found this solution pretty straight forward:

from bokeh.io import export_svgs
p =  hv.render(heatmap, backend='bokeh')
p.output_backend = "svg"
export_svgs(p, filename="heatmap.svg")
Read more comments on GitHub >

github_iconTop Results From Across the Web

SVG Support for Bokeh Backend #2966 - holoviz/holoviews
I wrote this a while back for HoloExt; was wondering if there's any interest for me to make a PR for this? import...
Read more >
Exporting plots — Bokeh 2.4.3 Documentation
Exporting SVG images# · Use the SVG-Crowbar bookmarklet that adds a prompt to download each plot as an SVG file. This tool is...
Read more >
Bokeh: how to save a file as svg? - python - Stack Overflow
This is now possible! From the doc: from bokeh.io import export_svgs plot.output_backend = "svg" export_svgs(plot, filename="plot.svg").
Read more >
Q3 2021 / Weekly Sync - HackMD
Issue triaging; Working on MathJax bokeh bundle; Python conveniences for LaTeX ... cleaned up PR queue (SVG and latex); followup improvements to SVG...
Read more >
Plotting with Matplotlib — HoloViews v1.15.3
'bokeh' backend provides many useful interactive features, the · 'matplotlib' plotting extension is well suited to static exports for printed figures and because ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found