bokeh-将图作为svg(valueerror:outputdocument for Expection to trupection of Expection furect of Expection furect of Expection furect of Models序列)

发布于 2025-01-21 05:32:06 字数 1573 浏览 0 评论 0原文

我在Bokeh中制作了一个绘图,但是当我尝试将其导出为SVG文件时,我会收到以下错误:OutputDocument For Expect for Tresection tocument for Outs tocument。

我的代码是:

# plot 2D histogram

def plot_hist(ch1, ch2, ch1_name='G3BP1', ch2_name='Nucleocapsid'):
    '''Plot a 2D histogram from two channels'''
    # get histogram data
    df_hist = pd.DataFrame({ch1_name: ch1.ravel(), ch2_name: ch2.ravel()})
    df_hist = df_hist.groupby(df_hist.columns.tolist()).size().reset_index(name='count')
    df_hist['log count'] = np.log10(df_hist['count'])

    # make the plot
    return hv.VLine(10).opts(apply_ranges=True, line_width=2, color = 'black', line_dash = 'dashed') * hv.HLine(10).opts(apply_ranges=True, line_width=2, color = 'black', line_dash = 'dashed')*hv.Points(
        data=df_hist, kdims=['G3BP1', 'Nucleocapsid'], vdims=['log count'],
    ).opts(
        size=10,
        cmap='magma',
        color='log count',
        colorbar=True,
        colorbar_opts={"title": "log₁₀ count"},
        frame_height=500,
        frame_width=500,
        padding=0.05,
        xlabel='G3BP1',
        ylabel='Nucleocapsid',
        fontsize=15,        
    )

# make ROI mask
roi1_nt = (single_cell_roi_rCh[1] > 0) | (single_cell_roi_gCh[1] > 0)
roi1_nt = skimage.morphology.remove_small_objects(roi1_nt, min_size=3)



hv.renderer('bokeh').theme = 'caliber'

plot=plot_hist(im_r[roi1_nt], im_g[roi1_nt])

from bokeh.io import export_svgs
plot.output_backend = "svg"
export_svgs(plot, filename = "plot.svg")

最终是否有一种方法可以在带有Geotiff的TIFF文件中转换此图中保存的PNG文件?

事先感谢您的帮助。

I made a plot in Bokeh but when I try to export it as a svg file I get the following error: OutputDocumentFor expects a sequence of Models.

My code is:

# plot 2D histogram

def plot_hist(ch1, ch2, ch1_name='G3BP1', ch2_name='Nucleocapsid'):
    '''Plot a 2D histogram from two channels'''
    # get histogram data
    df_hist = pd.DataFrame({ch1_name: ch1.ravel(), ch2_name: ch2.ravel()})
    df_hist = df_hist.groupby(df_hist.columns.tolist()).size().reset_index(name='count')
    df_hist['log count'] = np.log10(df_hist['count'])

    # make the plot
    return hv.VLine(10).opts(apply_ranges=True, line_width=2, color = 'black', line_dash = 'dashed') * hv.HLine(10).opts(apply_ranges=True, line_width=2, color = 'black', line_dash = 'dashed')*hv.Points(
        data=df_hist, kdims=['G3BP1', 'Nucleocapsid'], vdims=['log count'],
    ).opts(
        size=10,
        cmap='magma',
        color='log count',
        colorbar=True,
        colorbar_opts={"title": "log₁₀ count"},
        frame_height=500,
        frame_width=500,
        padding=0.05,
        xlabel='G3BP1',
        ylabel='Nucleocapsid',
        fontsize=15,        
    )

# make ROI mask
roi1_nt = (single_cell_roi_rCh[1] > 0) | (single_cell_roi_gCh[1] > 0)
roi1_nt = skimage.morphology.remove_small_objects(roi1_nt, min_size=3)



hv.renderer('bokeh').theme = 'caliber'

plot=plot_hist(im_r[roi1_nt], im_g[roi1_nt])

from bokeh.io import export_svgs
plot.output_backend = "svg"
export_svgs(plot, filename = "plot.svg")

Is there eventually a way to transform the png file saved from this plot in a tiff file with Geotiff?

Thanks in advance for the help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

反目相谮 2025-01-28 05:32:06

看起来您正在使用holoviews在函数plot_hist()中创建图。这不是bokeh 对象

请尝试调用hv.render()获取基础的散景图。

下面的行应创建一个SVG文件。

import holoviews as hv
from bokeh.io import export_svgs

plot = plot_hist(im_r[roi1_nt], im_g[roi1_nt])
plot = hv.render(plot)
plot.output_backend = "svg"
export_svgs(plot, filename = "plot.svg")

edit

作为@bigreddot Metioned,bokeh还具有export_png()工具。在这种情况下,代码看起来像下面。

import holoviews as hv
from bokeh.io import export_png

plot = plot_hist(im_r[roi1_nt], im_g[roi1_nt])
plot = hv.render(plot)
export_png(plot, filename = "plot.png")

如果您的目标是从“ PNG”生成“ TIFF”文件,则可以从此 so 的问题。

from PIL import Image
img = Image.open('plot.png')
img.save('image.tiff')

如果PNG具有α值,这可能会带来一些问题。在这种情况下,请检查此问题关于在枕头中打开PNG。

It looks like you are using holoviews to create the figure in your function plot_hist(). This is not bokeh object.

Please try to call hv.render() to get the underlying bokeh figure.

The lines below should create a svg file.

import holoviews as hv
from bokeh.io import export_svgs

plot = plot_hist(im_r[roi1_nt], im_g[roi1_nt])
plot = hv.render(plot)
plot.output_backend = "svg"
export_svgs(plot, filename = "plot.svg")

Edit

As @bigreddot metioned, bokeh also has a export_png() tool. In this case the code shhould look like below.

import holoviews as hv
from bokeh.io import export_png

plot = plot_hist(im_r[roi1_nt], im_g[roi1_nt])
plot = hv.render(plot)
export_png(plot, filename = "plot.png")

If your goal is to generate an "tiff" file from "png", you can follow the solution from this question on SO.

from PIL import Image
img = Image.open('plot.png')
img.save('image.tiff')

If the png has an alpha value, this could bring some problems. In this case, check this question about open a png in Pillow.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文