简体   繁体   中英

bokeh (python) export_svgs doesn't render image alpha

I'm having trouble getting bokeh.io.export_svgs() to output an image with alpha correctly.

Toy example:

import numpy as np

import bokeh.plotting as bk_plt
import bokeh.io as bk_io

bk_plt.output_notebook()

N = 500
x = np.linspace(0, 10, N)
y = np.linspace(0, 10, N)
xx, yy = np.meshgrid(x, y)
d = np.sin(xx)*np.cos(yy)

p1 = bk_plt.figure(x_range=(0, 10), y_range=(0, 10))
p1.image(image=[d], x=0, y=0, dw=10, dh=10, palette="Spectral11", alpha = 0.5)
bk_plt.show(p1)


p2 = bk_plt.figure(x_range=(0, 10), y_range=(0, 10))
p2.image(image=[d], x=0, y=0, dw=10, dh=10, palette="Spectral11", alpha = 0.5)
p2.output_backend = 'svg'
bk_io.export_svgs(p2, filename = 'asdf.svg')
bk_plt.show(p2)

Bokeh correctly outputs this to notebook:

在此处输入图片说明

export_svgs() incorrectly outputs this:

在此处输入图片说明

Is there an easy way to fix this?

Found a workable solution - create an rgba image and plot it instead:

import numpy as np

import bokeh.plotting as bk_plt
import bokeh.palettes as bk_pal
import bokeh.io as bk_io

bk_plt.output_notebook()

N = 500
x = np.linspace(0, 10, N)
y = np.linspace(0, 10, N)
xx, yy = np.meshgrid(x, y)
d = np.sin(xx)*np.cos(yy)


def Make_RGBA(Val, Palette, alpha):
    X, Y = Val.shape

    Pal = [tuple(int(P[i:i+2], 16) for i in (1, 3 ,5)) for P in Palette]
    a = (len(Pal)) / (Val.max() - Val.min()) 
    b = Val.min()
    Temp_Val = (a * (Val - b)).astype(int)
    Temp_Val = np.minimum(Temp_Val, len(Pal) - 1)

    img = np.empty((X, Y), dtype=np.uint32)
    view = img.view(dtype=np.uint8).reshape((X, Y, 4))
    for i in range(X):
        for j in range(Y):
            view[i, j, 0] = Pal[Temp_Val[i][j]][0]
            view[i, j, 1] = Pal[Temp_Val[i][j]][1]
            view[i, j, 2] = Pal[Temp_Val[i][j]][2]
            view[i, j, 3] = 255 * alpha

    return img

img = Make_RGBA(d, bk_pal.Spectral11, 0.5)

p1 = bk_plt.figure(x_range=(0, 10), y_range=(0, 10))
p1.image(image=[d], x=0, y=0, dw=10, dh=10, palette="Spectral11", alpha = 0.5)
bk_plt.show(p1)

p2 = bk_plt.figure(x_range=(0, 10), y_range=(0, 10))
p2.image_rgba(image=[img], x=[0], y=[0], dw=[10], dh=[10])
p2.output_backend = 'svg'
bk_io.export_svgs(p2, filename = 'asdf.svg')
bk_plt.show(p2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM