如何使用数据框显示散点图的散景图?

时间:2016-04-18 16:44:04

标签: python scatter-plot bokeh

我有以下情节:

import pandas as pd
from bokeh.plotting import ColumnDataSource, figure, output_file, show
from bokeh.models import HoverTool

output_file("scatter.html")
df = pd.read_csv('/Users/jz/07_isolate_selection.csv')
hash = {}

c = ('aliceblue', 'antiquewhite', 'aqua', 'aquamarine', 'azure', 'beige', 'bisque', 'black', 
    'blanchedalmond', 'blue', 'blueviolet', 'brown', 'burlywood', 'cadetblue', 'chartreuse', 
    'chocolate', 'coral', 'cornflowerblue', 'cornsilk', 'crimson', 'cyan', 'darkblue', 'darkcyan', 
    'darkgoldenrod', 'darkgray', 'darkgreen', 'darkgrey', 'darkkhaki', 'darkmagenta', 'darkolivegreen', 
    'darkorange', 'darkorchid', 'darkred', 'darksalmon', 'darkseagreen', 'darkslateblue', 'darkslategray', 
    'darkslategrey', 'darkturquoise', 'darkviolet', 'deeppink', 'deepskyblue', 'dimgray', 'dimgrey', 'dodgerblue', 
    'firebrick', 'floralwhite', 'forestgreen', 'fuchsia', 'gainsboro', 'ghostwhite', 'gold', 'goldenrod', 
    'gray', 'green', 'greenyellow', 'grey', 'honeydew', 'hotpink', 'indianred', 'indigo', 'ivory', 'khaki', 
    'lavender', 'lavenderblush', 'lawngreen', 'lemonchiffon', 'lightblue', 'lightcoral', 'lightcyan', 
    'lightgoldenrodyellow', 'lightgray', 'lightgreen', 'lightgrey', 'lightpink', 'lightsalmon', 'lightseagreen', 
    'lightskyblue', 'lightslategray', 'lightslategrey', 'lightsteelblue', 'lightyellow', 'lime', 'limegreen', 
    'linen', 'magenta', 'maroon', 'mediumaquamarine', 'mediumblue', 'mediumorchid', 'mediumpurple', 'mediumseagreen', 
    'mediumslateblue', 'mediumspringgreen', 'mediumturquoise', 'mediumvioletred', 'midnightblue', 'mintcream', 'mistyrose', 
    'moccasin', 'navajowhite', 'navy', 'oldlace', 'olive', 'olivedrab', 'orange', 'orangered', 'orchid', 'palegoldenrod', 
    'palegreen', 'paleturquoise', 'palevioletred', 'papayawhip', 'peachpuff', 'peru', 'pink', 'plum', 'powderblue', 
    'purple', 'red', 'rosybrown', 'royalblue', 'saddlebrown', 'salmon', 'sandybrown', 'seagreen', 'seashell', 'sienna', 
    'silver', 'skyblue', 'slateblue', 'slategray', 'slategrey', 'snow', 'springgreen', 'steelblue', 'tan', 'teal', 
    'thistle', 'tomato', 'turquoise', 'violet', 'wheat', 'white', 'whitesmoke', 'yellow', 'yellowgreen');


#loop through and assign colors:
counter = 0
for a in df.material.unique():
    hash[a] = c[counter]
    counter += 1

# generate colors:

colors = [hash[code] for code in df.material]
df['color'] = colors


source = ColumnDataSource(df)
TOOLS="pan,wheel_zoom,box_zoom,reset,hover"
p = figure(title="Hoverful Scatter", tools=TOOLS)
p.circle('peak.count', 'purity.score', radius=.66, source=source,
          fill_color=colors, fill_alpha=0.6, line_color=None)

hover = p.select(dict(type=HoverTool))[0]
hover.tooltips = [
    ("material", "@material")
]

show(p)

如何显示与df.material列中定义的组对应的图例?颜色编码在下面工作,我用df.material对圆圈进行分组,但我还想显示一个图例,以便你知道' aliceblue'对应于“FOO'和橄榄'对应' BAR'举个例子。

谢谢!

2 个答案:

答案 0 :(得分:0)

我的线图有类似的问题,我的解决方案是将p.line()添加到for循环中。请查看此简单示例here

另请使用更复​​杂的示例检查my notebook,其中我使用此方法制作多线图。

祝你好运!

答案 1 :(得分:0)

On Bokeh 0.12.3

您可以尝试制作一个专栏'颜色'对于你的数据框df使用字典,让我们说:

colors = {'typeA':'red', 'typeB':'blue', 'typeC':'black'}

其中colors.keys()来自你df的typeColumn

上的值

现在将颜色列添加到df:

df['colorColumn'] = [colors[key] for key in df.typeColumn.values]

因此您只需要从刚刚定义的来源访问这些列

p.circle('x', 'y', size=8, fill_color='colorColumn', legend='typeColumn', source=source)

希望有所帮助