Advertisement
Higem

Untitled

Apr 5th, 2020
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. from bokeh.io import show
  2. from bokeh.layouts import column, row
  3. from bokeh.models import DataTable, TableColumn, Button, CustomJS, ColumnDataSource
  4. from bokeh.plotting import figure
  5. from bokeh.io import output_notebook
  6. from bokeh.transform import factor_cmap
  7.  
  8. output_notebook()
  9.  
  10. # new x_ranges (1d and 2d) with their values
  11. range1d = ["c", "d"]
  12. values1d = [100, 150];
  13. range2d_p1 = ["e", "e", "f", "f", "g", "g"];
  14. range2d_p2 = ["0", "1", "0", "1", "0", "1"];
  15. values2d = [50, 30, 40, 80, 120, 110];
  16.  
  17. # default fig
  18. fig = figure(x_range=["a", "b"],
  19. plot_height=350,
  20. plot_width=490,
  21. tools="box_edit",
  22. toolbar_location="right")
  23.  
  24. source_plot = ColumnDataSource(data={"Column": ["a", "b"], "Result": [0,0]})
  25. bar = fig.vbar(x='Column', top='Result', width=0.9, source=source_plot,
  26. fill_color=factor_cmap('Column', palette=['red', 'blue'], factors=range1d, start=1, end=2),
  27. line_color="black", name="main_vbar")
  28.  
  29. # buttons
  30. xrange_1d = Button(label="xrange_1d", width=100)
  31. xrange_2d = Button(label="xrange_2d", width=100)
  32.  
  33. # callbacks
  34. xrange_1d.js_on_click(CustomJS(args=dict(fig=fig, source_plot=source_plot, bar=bar,
  35. range1d=range1d, values1d=values1d),
  36. code="""
  37. fig.x_range.factors = range1d;
  38. source_plot.data["Column"] = range1d;
  39. source_plot.data["Result"] = values1d;
  40. bar.glyph.fill_color.factors = range1d;
  41. source_plot.change.emit();
  42. """))
  43.  
  44. xrange_2d.js_on_click(CustomJS(args=dict(fig=fig, source_plot=source_plot, bar=bar,
  45. range2d_p1=range2d_p1, range2d_p2=range2d_p2,
  46. values2d=values2d),
  47. code="""
  48. var factorsZipped = range2d_p1.map(function(e, i) {
  49. return [e, range2d_p2[i]];
  50. });
  51. fig.x_range.factors = factorsZipped;
  52. source_plot.data["Column"] = factorsZipped;
  53. source_plot.data["Result"] = values2d;
  54.  
  55. bar.glyph.fill_color.factors = range2d_p2;
  56. bar.glyph.fill_color.start = 1;
  57. bar.glyph.fill_color.end = 2;
  58.  
  59. source_plot.change.emit();
  60. """))
  61.  
  62.  
  63. show(column(row(xrange_1d, xrange_2d), fig))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement