gpt4 book ai didi

holoviews - 将全息图保存为 SVG

转载 作者:行者123 更新时间:2023-12-03 23:02:17 25 4
gpt4 key购买 nike

我想将全息图保存为 SVG,但是我收到一条我不明白的错误消息:

    np.random.seed(9)
data = np.random.rand(10, 2)
points = hv.Points(data)
labels = hv.Labels({('x', 'y'): data, 'text': [chr(65+i) for i in range(10)]}, ['x', 'y'], 'text')
overlay = (points * labels).redim.range(x=(-0.2, 1.2), y=(-.2, 1.2))

overlay.opts(
opts.Labels(text_font_size='10pt', xoffset=0.08),
opts.Points(color='black', size=5))

hv.save(overlay, 'overlay.svg', fmt='svg')
    ---------------------------------------------------------------------------
Exception Traceback (most recent call last)
<ipython-input-29-b23ca97e9c01> in <module>
9 opts.Points(color='black', size=5))
10
---> 11 hv.save(overlay, 'overlay.svg', fmt='svg')

~/miniconda3/envs/mybrew/lib/python3.7/site-packages/holoviews/util/__init__.py in save(obj, filename, fmt, backend, resources, toolbar, title, **kwargs)
818 filename = '.'.join(formats[:-1])
819 return renderer_obj.save(obj, filename, fmt=fmt, resources=resources,
--> 820 title=title)
821
822

~/miniconda3/envs/mybrew/lib/python3.7/site-packages/holoviews/plotting/renderer.py in save(self_or_cls, obj, basename, fmt, key, info, options, resources, title, **kwargs)
592
593 with StoreOptions.options(obj, options, **kwargs):
--> 594 plot, fmt = self_or_cls._validate(obj, fmt)
595
596 if isinstance(plot, Viewable):

~/miniconda3/envs/mybrew/lib/python3.7/site-packages/holoviews/plotting/renderer.py in _validate(self, obj, fmt, **kwargs)
302 if fmt not in all_formats:
303 raise Exception("Format %r not supported by mode %r. Allowed formats: %r"
--> 304 % (fmt, self.mode, fig_formats + holomap_formats))
305 self.last_plot = plot
306 return plot, fmt

Exception: Format 'svg' not supported by mode 'default'. Allowed formats: ['html', 'auto', 'png', 'widgets', 'scrubber', 'gif', 'auto', None]
有没有人知道为什么会发生这种情况?根据文档:
http://holoviews.org/user_guide/Exporting_and_Archiving.html
应该可以导出到 SVG。

最佳答案

使用默认'bokeh'时出现问题后端。我找到了解决方案 here :

import holoviews as hv
from bokeh.io import export_svgs

def export_svg(obj, filename):
plot_state = hv.renderer('bokeh').get_plot(obj).state
plot_state.output_backend = 'svg'
export_svgs(plot_state, filename=filename)

overlay = hv.Overlay(...)
export_svg(overlay, 'overlay.svg')
但是,由于某种原因,点元素不会使用此方法呈现。
另一种解决方案是切换到 'matplotlib'后端(下面代码中的第一行),然后是 hv.save(...)作品。不幸的是,这两个后端与所有选项都不兼容。例如,在您的情况下,您必须更改 text_font_sizefontsizesizes并将值提高到 2 以进行相应的缩放:
hv.extension('matplotlib')

np.random.seed(9)
data = np.random.rand(10, 2)
points = hv.Points(data)
labels = hv.Labels({('x', 'y'): data, 'text': [chr(65+i) for i in range(10)]}, ['x', 'y'], 'text')
overlay = (points * labels).redim.range(x=(-0.2, 1.2), y=(-.2, 1.2))

overlay.opts(
hv.opts.Labels(fontsize=10, xoffset=0.08),
hv.opts.Points(color='black', s=25))

hv.save(overlay, 'overlay.svg', fmt='svg')
overlay
如果 Holoviews 对样式选项有一个统一的 api,就像它对视觉元素所做的那样,那肯定会有所帮助。

关于holoviews - 将全息图保存为 SVG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64865556/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com