Skip to content

Instantly share code, notes, and snippets.

@rcompton
Last active April 29, 2016 18:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rcompton/67bb614b86297c970451 to your computer and use it in GitHub Desktop.
Save rcompton/67bb614b86297c970451 to your computer and use it in GitHub Desktop.
Bokeh confusion matrix plot bug
#cf https://groups.google.com/a/continuum.io/forum/#!msg/bokeh/rvFcJV5_WQ8/Pf5hsF9mCAAJ
from collections import OrderedDict
import numpy as np
from bokeh.plotting import figure, show, output_file
from bokeh.models import HoverTool, ColumnDataSource
N = 20
arr2d = np.random.randint(0,10,size=(N,N))
predicted = []
actual = []
count = []
color = []
alpha = []
the_color = '#cc0000'
for col in range(N):
for rowi in range(N):
predicted.append(str(rowi))
actual.append(str(col))
count.append(arr2d[rowi,col])
a = arr2d[rowi,col]/10.0
alpha.append(a)
color.append(the_color)
source = ColumnDataSource(
data=dict(predicted=predicted, actual=actual, count=count, alphas=alpha, colors=color)
)
p = figure(title='Confusion Matrix',
x_axis_location="above", tools="resize,hover,save",
y_range=list(reversed(actual)), x_range=actual)
p.plot_width = 600
p.plot_height = p.plot_width
rectwidth = 10.9
p.rect('predicted', 'actual', rectwidth, rectwidth, source=source,
color='colors', alpha='alphas',line_width=1, line_color='k')
p.axis.major_label_text_font_size = "12pt"
p.axis.major_label_standoff = 1
p.xaxis.major_label_orientation = np.pi/3
hover = p.select(dict(type=HoverTool))
hover.tooltips = OrderedDict([
('predicted', '@predicted'),
('actual', '@actual'),
('count', '@count'),
])
show(p)
@rcompton
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment