|
| 1 | +# First, we import a tool to allow text to pop up on a plot when the cursor |
| 2 | +# hovers over it. Also, we import a data structure used to store arguments |
| 3 | +# of what to plot in Bokeh. Finally, we will use numpy for this section as well! |
| 4 | + |
| 5 | +from bokeh.models import HoverTool, ColumnDataSource |
| 6 | +import numpy as np |
| 7 | + |
| 8 | +# Let's plot a simple 5x5 grid of squares, alternating in color as red and blue. |
| 9 | + |
| 10 | +plot_values = [1,2,3,4,5] |
| 11 | +plot_colors = ["red", "blue"] |
| 12 | + |
| 13 | +# How do we tell Bokeh to plot each point in a grid? Let's use a function that |
| 14 | +# finds each combination of values from 1-5. |
| 15 | +from itertools import product |
| 16 | + |
| 17 | +grid = list(product(plot_values, plot_values)) |
| 18 | +print(grid) |
| 19 | + |
| 20 | +# The first value is the x coordinate, and the second value is the y coordinate. |
| 21 | +# Let's store these in separate lists. |
| 22 | + |
| 23 | +xs, ys = zip(*grid) |
| 24 | +print(xs) |
| 25 | +print(ys) |
| 26 | + |
| 27 | +# Now we will make a list of colors, alternating between red and blue. |
| 28 | + |
| 29 | +colors = [plot_colors[i%2] for i in range(len(grid))] |
| 30 | +print(colors) |
| 31 | + |
| 32 | +# Finally, let's determine the strength of transparency (alpha) for each point, |
| 33 | +# where 0 is completely transparent. |
| 34 | + |
| 35 | +alphas = np.linspace(0, 1, len(grid)) |
| 36 | + |
| 37 | +# Bokeh likes each of these to be stored in a special dataframe, called |
| 38 | +# ColumnDataSource. Let's store our coordinates, colors, and alpha values. |
| 39 | + |
| 40 | +source = ColumnDataSource( |
| 41 | + data={ |
| 42 | + "x": xs, |
| 43 | + "y": ys, |
| 44 | + "colors": colors, |
| 45 | + "alphas": alphas, |
| 46 | + } |
| 47 | +) |
| 48 | +# We are ready to make our interactive Bokeh plot! |
| 49 | + |
| 50 | +output_file("Basic_Example.html", title="Basic Example") |
| 51 | +fig = figure(tools="resize, hover, save") |
| 52 | +fig.rect("x", "y", 0.9, 0.9, source=source, color="colors",alpha="alphas") |
| 53 | +hover = fig.select(dict(type=HoverTool)) |
| 54 | +hover.tooltips = { |
| 55 | + "Value": "@x, @y", |
| 56 | + } |
| 57 | +show(fig) |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | +cluster_colors = ["red", "orange", "green", "blue", "purple", "gray"] |
| 62 | +regions = ["Speyside", "Highlands", "Lowlands", "Islands", "Campbelltown", "Islay"] |
| 63 | + |
| 64 | +region_colors = {regions[i] : cluster_colors[i] for i in range(len(cluster_colors))} |
| 65 | + |
| 66 | +print(region_colors) |
| 67 | + |
| 68 | + |
| 69 | +distilleries = list(whisky.Distillery) |
| 70 | +correlation_colors = [] |
| 71 | +for i in range(len(distilleries)): |
| 72 | + for j in range(len(distilleries)): |
| 73 | + if (correlations[i][j] < 0.7): # if low correlation, |
| 74 | + correlation_colors.append('white') # just use white. |
| 75 | + else: # otherwise, |
| 76 | + if (whisky.Group[i]==whisky.Group[j]): # if the groups match, |
| 77 | + correlation_colors.append(cluster_colors[whisky.Group[i]]) # color them by their mutual group. |
| 78 | + else: # otherwise |
| 79 | + correlation_colors.append('lightgray') # color them lightgray. |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | +source = ColumnDataSource( |
| 86 | + data = { |
| 87 | + "x": np.repeat(distilleries,len(distilleries)), |
| 88 | + "y": list(distilleries)*len(distilleries), |
| 89 | + "colors": correlation_colors, |
| 90 | + "alphas": correlations.flatten(), |
| 91 | + "correlations": correlations.flatten() |
| 92 | + } |
| 93 | +) |
| 94 | + |
| 95 | +output_file("Whisky Correlations.html", title="Whisky Correlations") |
| 96 | +fig = figure(title="Whisky Correlations", |
| 97 | + x_axis_location="above", tools="resize,hover,save", |
| 98 | + x_range=list(reversed(distilleries)), y_range=distilleries) |
| 99 | +fig.grid.grid_line_color = None |
| 100 | +fig.axis.axis_line_color = None |
| 101 | +fig.axis.major_tick_line_color = None |
| 102 | +fig.axis.major_label_text_font_size = "5pt" |
| 103 | +fig.xaxis.major_label_orientation = np.pi / 3 |
| 104 | + |
| 105 | +fig.rect('x', 'y', .9, .9, source=source, |
| 106 | + color='colors', alpha='alphas') |
| 107 | +hover = fig.select(dict(type=HoverTool)) |
| 108 | +hover.tooltips = { |
| 109 | + "Whiskies": "@x, @y", |
| 110 | + "Correlation": "@correlations", |
| 111 | +} |
| 112 | +show(fig) |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | +points = [(0,0), (1,2), (3,1)] |
| 117 | +xs, ys = zip(*points) |
| 118 | +colors = ["red", "blue", "green"] |
| 119 | + |
| 120 | +output_file("Spatial_Example.html", title="Regional Example") |
| 121 | +location_source = ColumnDataSource( |
| 122 | + data={ |
| 123 | + "x": xs, |
| 124 | + "y": ys, |
| 125 | + "colors": colors, |
| 126 | + } |
| 127 | +) |
| 128 | + |
| 129 | +fig = figure(title = "Title", |
| 130 | + x_axis_location = "above", tools="resize, hover, save") |
| 131 | +fig.plot_width = 300 |
| 132 | +fig.plot_height = 380 |
| 133 | +fig.circle("x", "y", 10, 10, size=10, source=location_source, |
| 134 | + color='colors', line_color = None) |
| 135 | + |
| 136 | +hover = fig.select(dict(type = HoverTool)) |
| 137 | +hover.tooltips = { |
| 138 | + "Location": "(@x, @y)" |
| 139 | +} |
| 140 | +show(fig) |
| 141 | + |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | + |
| 147 | +def location_plot(title, colors): |
| 148 | + output_file(title+".html") |
| 149 | + location_source = ColumnDataSource( |
| 150 | + data={ |
| 151 | + "x": whisky[" Latitude"], |
| 152 | + "y": whisky[" Longitude"], |
| 153 | + "colors": colors, |
| 154 | + "regions": whisky.Region, |
| 155 | + "distilleries": whisky.Distillery |
| 156 | + } |
| 157 | + ) |
| 158 | + |
| 159 | + fig = figure(title = title, |
| 160 | + x_axis_location = "above", tools="resize, hover, save") |
| 161 | + fig.plot_width = 400 |
| 162 | + fig.plot_height = 500 |
| 163 | + fig.circle("x", "y", 10, 10, size=9, source=location_source, |
| 164 | + color='colors', line_color = None) |
| 165 | + fig.xaxis.major_label_orientation = np.pi / 3 |
| 166 | + hover = fig.select(dict(type = HoverTool)) |
| 167 | + hover.tooltips = { |
| 168 | + "Distillery": "@distilleries", |
| 169 | + "Location": "(@x, @y)" |
| 170 | + } |
| 171 | + show(fig) |
| 172 | + |
| 173 | +region_cols = [region_colors[whisky.Region[i]] for i in range(len(whisky.Region))] |
| 174 | +location_plot("Whisky Locations and Regions", region_cols) |
| 175 | + |
| 176 | + |
| 177 | + |
| 178 | +region_cols = [region_colors[whisky.Region[i]] for i in range(len(whisky.Region))] |
| 179 | +classification_cols = [cluster_colors[whisky.Group[i]] for i in range(len(whisky.Group))] |
| 180 | + |
| 181 | +location_plot("Whisky Locations and Regions", region_cols) |
| 182 | +location_plot("Whisky Locations and Groups", classification_cols) |
| 183 | + |
| 184 | + |
0 commit comments