我正在尝试使用Bokeh而不是matplotlib复制[此问题中](http://codingdict.com/questions/12821显示的HeatMap。我不能完全正确。现有的示例并没有帮助我了解我在做什么错。我卑微的尝试
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
更改数据的生成以按metric元素重复,并且它应该是正确的:
metric
'metric': [item for item in list(nba.columns) for i in range(len(nba.index))],
因此,适用于我的代码如下:
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)