散景:如何在图例中同时显示颜色和标记类型

时间:2019-02-15 11:24:43

标签: bokeh

在jupyter笔记本中使用boekh软件包时,我想绘制同时显示线条颜色和标记类型的图例。

我在一个情节中有很多行。为了区分它们,我尽力通过在色彩空间中均匀分布它们的颜色来区分它们。但是,当行数达到例如9,某些线条的颜色很相似。因此,我想在不同的颜色之上添加不同的标记类型,以便当两行颜色相似时,它们具有不同的标记类型。 使用matplotlib可以直接进行,但是使用bokeh则不能直接进行。下面是我现在只能用颜色绘制图例的代码。

import pandas as pd
import numpy as np
import math
import matplotlib.pyplot as plt
%matplotlib inline
from bokeh.plotting import figure, show, ColumnDataSource, save,     output_notebook, output_file, reset_output
from bokeh.io import export_svgs,export_png
from bokeh.models import HoverTool, Legend
from bokeh.layouts import gridplot
import colorsys # needed for generating N equally extinguishable colors
from operator import add # needed for add lists

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;
for key, group in values.groupby(['Sex']):
    for key_sub1, group_sub1 in group.groupby(['age']):
        loop_count+=1
        #print type(key)
        #print group_sub1
        #print count
        #hover = HoverTool(tooltips=[("data (x,y)", "($x, $y)")])
        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)))
        legend_its_row_col.append(("%s %s"%(key,key_sub1), [plot_row_col_line]))


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)

我的代码可以得到: actual results 我想要的是: expected results

1 个答案:

答案 0 :(得分:1)

这应该可以提供所需的结果,我添加了一个循环列表,其中包含可以与p.scatter一起使用的所有标记,并且每次迭代都需要使用另一个标记。之后,我将其与字形线一起添加到图例字典中。

#!/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()
show(grid_row_col)

enter image description here