如何使用Bokeh正确创建HeatMap [英] How to properly create a HeatMap with Bokeh

查看:165
本文介绍了如何使用Bokeh正确创建HeatMap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试使用Bokeh复制此问题中显示的HeatMap matplotlib.我不能完全正确.现有的例子并没有帮助我理解我做错了什么.我的谦卑尝试

I'm trying to replicate the HeatMap shown in this question using Bokeh instead of matplotlib. I can't get it quite right though. The existing examples have not helped me to understand what I'm doing wrong. My humble attempt

from bokeh.io import output_notebook; output_notebook()
from bokeh.charts import HeatMap, show
from bokeh.palettes import RdYlGn6
import pandas as pd
import numpy as np

nba = pd.read_csv(urlopen("http://datasets.flowingdata.com/ppg2008.csv"), index_col=0)

# Normalize the data columns and sort.
nba = (nba - nba.mean()) / (nba.max() - nba.min())
nba.sort('PTS', inplace=True)

score = []
for x in nba.apply(tuple):
  score.extend(x)

data = {
  'players': list(nba.index) * len(nba.columns),
  'metric':  list(nba.columns) * len(nba.index),
  'score':   score,
}

hm = HeatMap(data, x='metric', y='players',values='score', title='Fruits', stat=None)
show(hm)

给予

请注意,尽管标题相似,但并不能回答我的问题.这不是一个相同的错误,我正在使用Bokeh 0.12.3

Note that, despite the similar title, this does not answer my question. It's not the same error and I'm using Bokeh 0.12.3

推荐答案

更改数据的生成metric以逐元素重复,并且它应该是正确的:

Change the generation of the data metric to repeat element-wise and it should be correct:

'metric': [item for item in list(nba.columns) for i in range(len(nba.index))],

因此对我有用的代码如下:

So the code that works for me is the following:

from bokeh.charts import HeatMap, show, output_file
import pandas as pd, numpy as np
from urllib2 import urlopen

nba = pd.read_csv(urlopen("http://datasets.flowingdata.com/ppg2008.csv"), index_col=0)

# Normalize the data columns and sort.
nba = (nba - nba.mean()) / (nba.max() - nba.min())
nba.sort_values(by = 'PTS', inplace=True)

score = []
for x in nba.apply(tuple):
  score.extend(x)

data = {
  'players': list(nba.index) * len(nba.columns),
  'metric':  [item for item in list(nba.columns) for i in range(len(nba.index))],
  'score':   score,
}

output_file('test.html')
hm = HeatMap(data, x='metric', y='players',values='score', title='Fruits', stat=None)
show(hm)

这篇关于如何使用Bokeh正确创建HeatMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆