Advertisement
Higem

Untitled

Apr 5th, 2020
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 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. range1_p1 = ["b", "b", "c", "c", "d", "d"]
  12. range1_p2 = ["2", "3", "2", "3", "2", "3"]
  13. values1 = [100, 150, 120, 180, 130, 150];
  14. range2_p1 = ["e", "e", "f", "f", "g", "g"];
  15. range2_p2 = ["0", "1", "0", "1", "0", "1"];
  16. values2 = [50, 30, 40, 80, 120, 110];
  17.  
  18. # default fig
  19. fig = figure(x_range=["a", "b"],
  20. plot_height=350,
  21. plot_width=490,
  22. tools="box_edit",
  23. toolbar_location="right")
  24.  
  25. source_plot = ColumnDataSource(data={"Column": ["a", "b"], "Result": [0,0]})
  26. bar = fig.vbar(x='Column', top='Result', width=0.9, source=source_plot,
  27. fill_color=factor_cmap('Column', palette=['red', 'blue'], factors=range2_p2, start=1, end=2),
  28. line_color="black", name="main_vbar")
  29.  
  30. # buttons
  31. xrange_1d = Button(label="xrange_1d", width=100)
  32. xrange_2d = Button(label="xrange_2d", width=100)
  33.  
  34. # callbacks
  35. xrange_1d.js_on_click(CustomJS(args=dict(fig=fig, source_plot=source_plot, bar=bar,
  36. range1_p1=range1_p1, range1_p2=range1_p2, values1=values1),
  37. code="""
  38. var factorsZipped = range1_p1.map(function(e, i) {
  39. return [e, range1_p2[i]];
  40. });
  41. console.log(factorsZipped);
  42. fig.x_range.factors = factorsZipped;
  43. source_plot.data["Column"] = factorsZipped;
  44. source_plot.data["Result"] = values1;
  45.  
  46. source_plot.change.emit();
  47. """))
  48.  
  49. xrange_2d.js_on_click(CustomJS(args=dict(fig=fig, source_plot=source_plot, bar=bar,
  50. range2_p1=range2_p1, range2_p2=range2_p2,
  51. values2=values2),
  52. code="""
  53. var factorsZipped = range2_p1.map(function(e, i) {
  54. return [e, range2_p2[i]];
  55. });
  56. fig.x_range.factors = factorsZipped;
  57. source_plot.data["Column"] = factorsZipped;
  58. source_plot.data["Result"] = values2;
  59.  
  60. source_plot.change.emit();
  61. """))
  62.  
  63.  
  64. show(column(row(xrange_1d, xrange_2d), fig))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement