Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. import numpy as np
  2. import pandas as pd
  3.  
  4. from bokeh.models import Circle, ColumnDataSource, Range1d, HoverTool, CustomJS
  5. from bokeh.transform import factor_cmap
  6. from bokeh.plotting import figure, show, output_file
  7.  
  8. output_file("hover_linked_samples.html")
  9.  
  10. raw_array=np.random.rand(80,2)
  11. fields = ["I", "II", "III", "IV"]
  12. samples = np.arange(20)
  13. index = pd.MultiIndex.from_product([samples, fields], names=['ID', 'field'])
  14.  
  15. data = pd.DataFrame(data=raw_array,
  16. columns=['x', 'y'],
  17. index=index)
  18. data.reset_index(inplace=True)
  19. data['alpha'] = 0.4
  20. source = ColumnDataSource(data)
  21.  
  22. xdr = Range1d(start=-0.05, end=1.05)
  23. ydr = Range1d(start=-0.05, end=1.05)
  24. s = figure(plot_width=500, plot_height=500, x_range=xdr, y_range=ydr, title="Hover over points", )
  25.  
  26. cr = s.scatter("x", "y", source=source, legend="field", fill_alpha='alpha', size=20,
  27. color=factor_cmap('field', 'Category10_5', fields))
  28.  
  29. # Add a hover tool, that highlights all fields from the hovered sample.
  30. code = """
  31. var cdata = circle.data;
  32. var indices = cb_data.index['1d'].indices;
  33. for (var i = 0; i < cdata['ID'].length; i++){
  34. cdata['alpha'][i] = 0.4;
  35. }
  36. for (var j = 0; j < indices.length; j++){
  37. ind0=indices[j];
  38. id = cdata['ID'][ind0];
  39. for (var k = 0; k < cdata['ID'].length; k++){
  40. if (id==cdata['ID'][k]){
  41. cdata['alpha'][k] = 1;
  42. }
  43. }
  44. }
  45. circle.data = cdata;
  46. circle.change.emit();
  47. """
  48.  
  49. callback = CustomJS(args={'circle': cr.data_source}, code=code)
  50. s.add_tools(HoverTool(tooltips=None, callback=callback, renderers=[cr]))
  51.  
  52. show(s)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement