散景:保存的svg与jupyter笔记本中显示的不同

时间:2019-03-13 14:40:25

标签: svg jupyter-notebook bokeh

我首先将图形渲染到笔记本中,然后将其保存为svg格式。笔记本中的图形显示正确,但是保存的svg缺少一些标记。

#!/usr/bin/python3
import pandas as pd
import numpy as np
import math
from bokeh.plotting import figure, show, ColumnDataSource, save, output_file, reset_output
from bokeh.models import HoverTool, Legend
from bokeh.layouts import gridplot
import colorsys # needed for generating N equally extinguishable colors
from itertools import cycle

d = {'Sex': ['male', 'male','male','male', 'male','male','female','female','female','female','female','female'], 'age': [20, 20,20, 25,25,25,20, 20,20,25,25,25], 'working_hours': [20,30,40,20,30,40,20,30,40,20,30,40],'income': [1000, 2000,3000,1500, 2500,3500,1100, 2100,3100,1300, 2300,3300] }
values = pd.DataFrame(data=d)

x_var = 'working_hours'
x_var_dimension = 'H'
y_var = 'income'
y_var_dimension = 'Dollars'
hover = HoverTool(tooltips=[("data (x,y)", "(@x, @y)")])
TOOLS=[hover]
p= figure(width=1200, height=600,tools=TOOLS, x_axis_type='linear', x_axis_label='%s [%s]'%(x_var, x_var_dimension),y_axis_label='%s [%s]'%(y_var, y_var_dimension))
nr_expressions_row_col=9
figs_array_row_col = []
figs_row_row_col=[]
legend_its_row_col = []
legend_its_row_col_renderer = []
loop_count = 0;
markers = ['circle', 'square', 'triangle', 'asterisk', 'circle_x', 'square_x', 'inverted_triangle', 'x', 'circle_cross', 'square_cross', 'diamond', 'cross']
pool = cycle(markers)
for key, group in values.groupby(['Sex']):
    for key_sub1, group_sub1 in group.groupby(['age']):
        loop_count+=1
        x_data = group_sub1[x_var].values;
        y_data =  group_sub1[y_var].values
        (color_r,color_g,color_b) = colorsys.hsv_to_rgb(loop_count*1.0/nr_expressions_row_col, 1, 1)
        plot_row_col_line = p.line(x_data, y_data,line_color=(int(255*color_r),int(255*color_g),int(255*color_b)))
        plot_row_col_glyph = p.scatter(x_data, y_data, color=(int(255*color_r),int(255*color_g),int(255*color_b)), size=10, marker=next(pool))
        legend_its_row_col.append(("%s %s"%(key,key_sub1), [plot_row_col_line, plot_row_col_glyph]))


legend_row_col = Legend(items = legend_its_row_col, location=(0,0))
legend_row_col.click_policy = 'hide'
legend_row_col.background_fill_alpha = 0
p.add_layout(legend_row_col, 'left')

figs_row_row_col.append(p)
figs_array_row_col.append(figs_row_row_col)

grid_row_col = gridplot(figs_array_row_col)
reset_output()
output_notebook()
show(grid_row_col)
p.output_backend = "svg"
export_svgs(grid_row_col, filename="%s/"%'.' + "_" +"stackoverflow.svg")

这是我在笔记本中看到的,这是我期望的: enter image description here

这是我打开“ _stackoverflow.svg”时看到的内容

“ female 25”和“ male 20”的图例颜色部分为黑色。 (标记部分)和“ female 20”缺少标记及其图例。 enter image description here

1 个答案:

答案 0 :(得分:1)

在笔记本和导出的图像中看到不同颜色的原因是它们使用两个不同的后端。您将后端设置为SVG,就在最后一行中将其导出之前。因此,尽管导出的图像使用SVG后端,但笔记本计算机上的图像使用默认后端,即canvas。

可以使用两个选项来设置后端:一个是您使用的,另一个是您在调用output_backend="svg"时添加figure自变量的选项。也就是说,替换

p= figure(width=1200, height=600,tools=TOOLS, x_axis_type='linear', x_axis_label='%s [%s]'%(x_var, x_var_dimension),y_axis_label='%s [%s]'%(y_var, y_var_dimension))

p= figure(width=1200, height=600,tools=TOOLS, x_axis_type='linear', x_axis_label='%s [%s]'%(x_var, x_var_dimension),y_axis_label='%s [%s]'%(y_var, y_var_dimension), output_backend="svg")

现在,您将在笔记本图像上的导出图像中看到相同的问题。似乎这是SVG后端中的bug,并且很可能它们是working。不幸的是,没有预期的错误修复日期。