Advertisement
Guest User

Bokeh Hover Disappears

a guest
Apr 21st, 2016
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. import numpy as np
  2. import pandas as pd
  3. import seaborn as sns
  4.  
  5. from bokeh import client as bkclient
  6. from bokeh import io as bkio
  7. from bokeh import plotting as bkplot
  8. from bokeh import models as bkmodels
  9.  
  10.  
  11. HEAT_DATA = pd.DataFrame(data=np.random.uniform(-3, 3, (100, 100)),
  12. index=['R{}'.format(x) for x in range(100)],
  13. columns=['C{}'.format(x) for x in range(100)])
  14.  
  15. SCATTER_DATA = pd.DataFrame(data=np.random.normal(-1, 1, (100, 2)),
  16. index=['R{}'.format(x) for x in range(100)],
  17. columns=list('xy'))
  18.  
  19.  
  20. def convert_to_colors(values, resolution=50):
  21. rgb = np.asarray(sns.color_palette('RdBu', n_colors=resolution)) * 255
  22. hex = np.asarray(['#%02x%02x%02x' % tuple(x) for x in rgb.astype(int)])[::-1]
  23. n_range = np.linspace(-3, 3, resolution)
  24. value_color_map = pd.DataFrame(dict({'value': n_range, 'color': hex}))
  25.  
  26. idxs = np.apply_along_axis(lambda o: np.searchsorted(value_color_map['value'], o), 0, values)
  27. idxs[idxs > (value_color_map.shape[0] - 1)] -= 1
  28. colors = np.apply_along_axis(lambda o: value_color_map['color'][o], 0, idxs)
  29. return colors
  30.  
  31. def refresh_heatmap(selections):
  32. print('\n\n\n\n\n')
  33. print(selections)
  34. print('\n\n\n\n\n')
  35.  
  36. frame = HEAT_DATA.loc[selections, :]
  37. frame_unstacked = frame.unstack().to_frame('values')
  38. frame_unstacked = frame_unstacked.reset_index()
  39.  
  40. frame_unstacked['color'] = convert_to_colors(frame_unstacked['values'])
  41.  
  42. # Note: These calls are referencing global object (defined below).
  43. heat_source.data = bkmodels.ColumnDataSource(frame_unstacked).data
  44. heat_figure.y_range.factors = selections
  45. return
  46.  
  47. def update_selected(attr, old, new):
  48. idxs = np.asarray(new['1d']['indices'])
  49. if len(idxs) > 0:
  50. selected = SCATTER_DATA.index[idxs].tolist()
  51. refresh_heatmap(selected)
  52. return
  53.  
  54.  
  55. # Scatter Plot
  56. scatter_source = bkmodels.ColumnDataSource(SCATTER_DATA)
  57. scatter_figure = bkplot.figure(title='Scatter', tools='pan,box_zoom,box_select,reset')
  58. scatter_plot = scatter_figure.scatter('x', 'y', source=scatter_source, alpha=0.6)
  59.  
  60. scatter_figure.select(bkmodels.BoxSelectTool).select_every_mousemove = False
  61. scatter_plot.data_source.on_change('selected', update_selected)
  62.  
  63. # Heat Map
  64. HEAT_DATA.index.name = 'Rows'
  65. HEAT_DATA.columns.name = 'Cols'
  66. heat_unstacked = HEAT_DATA.unstack().to_frame('values')
  67. heat_unstacked = heat_unstacked.reset_index()
  68. heat_unstacked['color'] = convert_to_colors(heat_unstacked['values'])
  69.  
  70. heat_source = bkmodels.ColumnDataSource(heat_unstacked)
  71.  
  72. hover = bkmodels.HoverTool(tooltips=[('Upper', '@Rows'), ('Lower', '@Cols'), ('Value', '@values')])
  73. heat_figure = bkplot.figure(title='Heat Map', tools=[hover], x_range=bkmodels.FactorRange(factors=[]),
  74. y_range=bkmodels.FactorRange(factors=[]))
  75.  
  76. heat_figure.x_range.factors = HEAT_DATA.columns.tolist()
  77. heat_figure.y_range.factors = HEAT_DATA.index.tolist()
  78.  
  79. heat_figure.xaxis.visible = None
  80. heat_figure.yaxis.visible = None
  81.  
  82. heat_plot = heat_figure.rect('Cols', 'Rows', 1, 1, source=heat_source, color='color', line_color=None)
  83.  
  84. # Render Page
  85. LAYOUT = bkplot.vplot(scatter_figure, heat_figure)
  86. bkplot.show(LAYOUT)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement