我正在尝试使用Bokeh而不是matplotlib复制this question中显示的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)
给
请注意,尽管标题相似,但this不能回答我的问题。这不是一个相同的错误,我正在使用Bokeh 0.12.3
最佳答案
将数据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)
关于python - 如何使用Bokeh正确创建HeatMap,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41282592/