Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
501 views
in Technique[技术] by (71.8m points)

python - How to properly create a HeatMap with Bokeh

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)

Gives

enter image description here

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In case you still want to create a heatmap using Bokeh: the charts module was removed in more recent versions. In other words, this command will not work with newer versions of Bokeh:

from bokeh.charts import HeatMap

Since it gives the error:

ImportError: cannot import name 'charts'

Charts was moved to bkcharts package, which was than discontinued (further reference in in this answer). Holoviews still has some support for Bokeh, but has some different syntax.

A solution for creating Heatmaps in Bokeh is using p.rect() as instructed in this link about unemployment.py, which results in something like this:

p = figure()
hm = p.rect(data, x='metric', y='players',values='score', title='Fruits', stat=None)

Which yields a results that looks like this:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...