def create_legend(x_pos, y_pos, color_dict, figure, all_failures, num_fails_to_list): #Create the Legend for the types of failures we see day to day for legend,color in color_dict.iteritems() : figure.circle(x=x_pos, y=y_pos, radius=0, color=color, legend=legend) figure.legend.orientation = "bottom_left"
def step(counts, bins, label, color, figure): ''' Add a step plot to 'figure', inplace of a bar plot Input: - counts : array-like Y-axis values - bins : array-like Limiting values for x-axis bins - label : string Label for this distribution - color : string Code defining a color; see ~booq.plot.Colors - figure : ~pandas.plotting.figure Output: - Figure : ~bokeh.plotting.figure ''' _y,_x = histogram2stepfunction(counts,bins) figure.line(x=_x, y=_y, #line_color="#D95B43",line_width=2, line_color=color,line_width=2, legend='label') import numpy as np _x = np.diff(bins)/2+bins[:-1] figure.circle(x=_x, y=_y, #line_color="#D95B43",line_width=2, line_color=color,line_width=2, fill_color="white",fill_alpha=1, size=9, legend='label') return figure
def add_multiline_and_circle_glyph_to_plot(figure: 'Bokeh Figure', legend: tuple, column_data_source: 'CDS for Bokeh figure', circle_size: int = 8, line_1_color: str='#CC0000', line_2_color: str='orange') -> 'Bokeh Figure': """Adds multiple lines + circle glyphs on Bokeh figure, initializes legend and return Bokeh figure.""" figure = DataVisualizationUtils.add_multiline_to_plot(figure, legend, column_data_source) figure.circle(x='x', y='y1', fill_color=line_1_color,line_color=line_1_color, size=circle_size, source=column_data_source) figure.circle(x='x', y='y2', fill_color=line_2_color, line_color=line_2_color, size=circle_size, source=column_data_source) return figure
def bokeh_draw_court(figure, line_width=1, line_color='gray'): """Returns a figure with the basketball court lines drawn onto it""" # hoop figure.circle(x=0, y=0, radius=7.5, fill_alpha=0, line_color=line_color, line_width=line_width) # backboard figure.line(x=range(-30,31), y=-7.5, line_color=line_color) # The paint # outerbox figure.rect(x=0, y=47.5, width=160, height=190,fill_alpha=0, line_color=line_color, line_width=line_width) # innerbox # left inner box line figure.line(x=-60, y=np.arange(-47.5, 143.5), line_color=line_color, line_width=line_width) # right inner box line figure.line(x=60, y=np.arange(-47.5, 143.5), line_color=line_color, line_width=line_width) # Restricted Zone figure.arc(x=0, y=0, radius=40, start_angle=pi, end_angle=0, line_color=line_color, line_width=line_width) # top free throw arc figure.arc(x=0, y=142.5, radius=60, start_angle=pi, end_angle=0, line_color=line_color) # bottome free throw arc figure.arc(x=0, y=142.5, radius=60, start_angle=0, end_angle=pi, line_color=line_color, line_dash="dashed") # Three point line # corner three point lines figure.line(x=-220, y=np.arange(-47.5, 92.5), line_color=line_color, line_width=line_width) figure.line(x=220, y=np.arange(-47.5, 92.5), line_color=line_color, line_width=line_width) # # three point arc figure.arc(x=0, y=0, radius=237.5, start_angle=3.528, end_angle=-0.3863, line_color=line_color, line_width=line_width) # add center court # outer center arc figure.arc(x=0, y=422.5, radius=60, start_angle=0, end_angle=pi, line_color=line_color, line_width=line_width) # inner center arct figure.arc(x=0, y=422.5, radius=20, start_angle=0, end_angle=pi, line_color=line_color, line_width=line_width) # outer lines, consistting of half court lines and out of bounds # lines figure.rect(x=0, y=187.5, width=500, height=470, fill_alpha=0, line_color=line_color, line_width=line_width) return figure
def addPlotsToFigure(figure, section): """ Add lines or scatter to the figure """ try: figure.xaxis.ticker.desired_num_ticks = 30 except AttributeError as err: logging.info(f"AttributeError: {err}") print("bokeh package needs to be updated (pip install bokeh -U)") sys.exit() for i, column in enumerate(plotObject.plots[section]["columns"]): # assert that it is present in dataframe if column not in plotObject.data.columns: logging.info(f"Column not found: {column}") continue # get boolean with rows without na. Only those will be plotted bool_not_na = (~pd.isna(plotObject.data.loc[:, column])).to_list() if re.search(r"^line$", plotObject.plots[section]['plotType'][i], re.IGNORECASE): # If line... figure.line( plotObject.data.loc[bool_not_na, plotObject.plots[section]['x_axis']], plotObject.data.loc[bool_not_na, column], line_width=2, color=d3['Category20'][20][plotObject.color], legend_label=column, name=column) elif re.search(r"^scatter$", plotObject.plots[section]['plotType'][i], re.IGNORECASE): # If scatter... figure.circle( plotObject.data.loc[bool_not_na, plotObject.plots[section]['x_axis']], plotObject.data.loc[bool_not_na, column], size=1, color=d3['Category20'][20][plotObject.color], legend_label=column, name=column) # Change color for the next plot plotObject.color += 2 if plotObject.color < 18 else 17 # plot threshold figure = plot_threshold(figure, section) # plot peaks of interest figure = plot_pleak(figure, section) return figure
def plot_point(figure, lat, lon, alpha=0.5, color="magenta", conver_to_mercator=True): if conver_to_mercator: lat, lon = gps_loc_to_web_mercator(lat, lon) figure.circle(x=lat, y=lon, size=4, alpha=alpha, color=color) return figure
def circle_values(figure, xdata, ydata): """ Args: figure = basic figure generated with figure_generator xdata = dataset for x axis ydata = dataset for y axis Return: circle glyph """ return figure.circle(x=xdata, y=ydata)
def __plot_stem_and_bulb(figure, series, color, name): figure.circle(x="index", y=name, source=ColumnDataSource(series.to_frame()), alpha=0.5, size=10, color=color) figure.segment(x0='index', x1='index', y0=0, y1=name, source=ColumnDataSource(series.to_frame()), line_width=1, color=color) return figure
def x_values(figure, xdata, ydata): """ Args: figure = basic figure generated with figure_generator xdata = dataset for x axis ydata = dataset for y axis Return: x glyph overlayed onto circle glyph figure """ return figure.circle(x=xdata, y=ydata)
def add_calculated_centroid_figure_mouseover(figure, centroid, lat_col="latitude", lon_col="longitude", legend=None, point_color="magenta", point_size=3, fill_color="magenta", cluster_alpha=0.3, to_mercator=True): if to_mercator: lon, lat = gps_loc_to_web_mercator(lat=centroid[lat_col], lon=centroid[lon_col]) centroid_circle = figure.circle(lon, lat) else: centroid_circle = figure.circle(lon=centroid[lon_col], lat=centroid[lat_col]) centroid_mercator = gps_loc_to_web_mercator(centroid[lat_col], centroid[lon_col]) sr_source = ColumnDataSource(data=dict(lon=centroid_mercator[0], lat=centroid_mercator[0], sr_id=centroid["sr_id"])) figure.circle("lon", "lat", color=point_color, size=point_size, legend=legend, source=sr_source) glyph = centroid_circle.glyph glyph.size = 20 glyph.fill_alpha = cluster_alpha glyph.fill_color = fill_color glyph.line_alpha = cluster_alpha glyph.line_color = "firebrick" glyph.line_dash = [6, 3] glyph.line_width = 1
def plot_poi(data, title, lat_col="latitude", lon_col="longitude", width=800, height=600, color=pick_random_color(), figure=None): if figure is None: figure = mercator_fig(title, point_mercator1=None, point_mercator2=None, width=width, height=height) mercator_loc_df = pd.DataFrame( user_data_gps_to_web_mercator(data, lat_col=lat_col, lon_col=lon_col)) figure.circle(x=mercator_loc_df[0], y=mercator_loc_df[1], size=5, alpha=0.3, color=color) return figure
def add_calculated_centroid_figure(figure, centroid, lat_col="latitude", lon_col="longitude", legend=None, point_color="magenta", point_size=3, fill_color="magenta", cluster_alpha=0.3, to_mercator=True): if to_mercator: lon, lat = gps_loc_to_web_mercator(lat=centroid[lat_col], lon=centroid[lon_col]) centroid_circle = figure.circle(lon, lat) else: centroid_circle = figure.circle(lon=centroid[lon_col], lat=centroid[lat_col]) centroid_mercator = gps_loc_to_web_mercator(centroid[lat_col], centroid[lon_col]) if legend is None: figure.circle(centroid_mercator[0], centroid_mercator[1], color=point_color, size=point_size) else: figure.circle(centroid_mercator[0], centroid_mercator[1], color=point_color, size=point_size, legend=legend) glyph = centroid_circle.glyph glyph.size = 20 glyph.fill_alpha = cluster_alpha glyph.fill_color = fill_color glyph.line_alpha = cluster_alpha glyph.line_color = "firebrick" glyph.line_dash = [6, 3] glyph.line_width = 1
def bokeh_draw_court(figure, line_color='gray', line_width=1): """Returns a figure with the basketball court lines drawn onto it This function draws a court based on the x and y-axis values that the NBA stats API provides for the shot chart data. For example the center of the hoop is located at the (0,0) coordinate. Twenty-two feet from the left of the center of the hoop in is represented by the (-220,0) coordinates. So one foot equals +/-10 units on the x and y-axis. Parameters ---------- figure : Bokeh figure object The Axes object to plot the court onto. line_color : str, optional The color of the court lines. Can be a a Hex value. line_width : float, optional The linewidth the of the court lines in pixels. Returns ------- figure : Figure The Figure object with the court on it. """ # hoop figure.circle(x=0, y=0, radius=7.5, fill_alpha=0, line_color=line_color, line_width=line_width) # backboard figure.line(x=range(-30, 31), y=-12.5, line_color=line_color) # The paint # outerbox figure.rect(x=0, y=47.5, width=160, height=190, fill_alpha=0, line_color=line_color, line_width=line_width) # innerbox # left inner box line figure.line(x=-60, y=np.arange(-47.5, 143.5), line_color=line_color, line_width=line_width) # right inner box line figure.line(x=60, y=np.arange(-47.5, 143.5), line_color=line_color, line_width=line_width) # Restricted Zone figure.arc(x=0, y=0, radius=40, start_angle=pi, end_angle=0, line_color=line_color, line_width=line_width) # top free throw arc figure.arc(x=0, y=142.5, radius=60, start_angle=pi, end_angle=0, line_color=line_color) # bottome free throw arc figure.arc(x=0, y=142.5, radius=60, start_angle=0, end_angle=pi, line_color=line_color, line_dash="dashed") # Three point line # corner three point lines figure.line(x=-220, y=np.arange(-47.5, 92.5), line_color=line_color, line_width=line_width) figure.line(x=220, y=np.arange(-47.5, 92.5), line_color=line_color, line_width=line_width) # # three point arc figure.arc(x=0, y=0, radius=237.5, start_angle=3.528, end_angle=-0.3863, line_color=line_color, line_width=line_width) # add center court # outer center arc figure.arc(x=0, y=422.5, radius=60, start_angle=0, end_angle=pi, line_color=line_color, line_width=line_width) # inner center arct figure.arc(x=0, y=422.5, radius=20, start_angle=0, end_angle=pi, line_color=line_color, line_width=line_width) # outer lines, consistting of half court lines and out of bounds lines figure.rect(x=0, y=187.5, width=500, height=470, fill_alpha=0, line_color=line_color, line_width=line_width) return figure
def _draw_court(self, figure, line_width=1): import numpy as np pi = 3.14 # hoop figure.circle(x=0, y=0, radius=7.5, fill_alpha=0, line_color=self.f_color, line_width=line_width) # backboard figure.line(x=range(-30, 31), y=-12.5, line_color=self.f_color) # The paint # outerbox figure.rect(x=0, y=47.5, width=160, height=190, fill_alpha=0, line_color=self.f_color, line_width=line_width) # innerbox # left inner box line figure.line(x=-60, y=np.arange(-47.5, 143.5), line_color=self.f_color, line_width=line_width) # right inner box line figure.line(x=60, y=np.arange(-47.5, 143.5), line_color=self.f_color, line_width=line_width) # Restricted Zone figure.arc(x=0, y=0, radius=40, start_angle=pi, end_angle=0, line_color=self.f_color, line_width=line_width) # top free throw arc figure.arc(x=0, y=142.5, radius=60, start_angle=pi, end_angle=0, line_color=self.f_color) # bottome free throw arc figure.arc(x=0, y=142.5, radius=60, start_angle=0, end_angle=pi, line_color=self.f_color, line_dash="dashed") # Three point line # corner three point lines figure.line(x=-220, y=np.arange(-47.5, 92.5), line_color=self.f_color, line_width=line_width) figure.line(x=220, y=np.arange(-47.5, 92.5), line_color=self.f_color, line_width=line_width) # # three point arc figure.arc(x=0, y=0, radius=237.5, start_angle=3.528, end_angle=-0.3863, line_color=self.f_color, line_width=line_width) return figure
def bdot(self, dataname, figurename, width=1, color=1, mode='dot', set_colormap=True): """----------------------------------------------------------------------------------------- Bokeh plot of dots in the main panel, and colorbar in the legend panel :param dataname: string, name of a dataframe in self.frame :param figurename: string, a figure defined in setupBokeh and stored in self.figure :param width: boolean, scale size of markers by the score :param color: boolean, scale the color of the markers by the score :param mode: string, if dot use the circle renderer, otherwise segment renderer :param set_colormap: boolean, set the colormap based on score range, turn off for second plot to use the same scale :return: True -----------------------------------------------------------------------------------------""" data = self.frame[dataname] figure = self.figure[figurename] legend = self.figure['legend'] window = self.window threshold = self.threshold alpha = self.alpha scoremin, scoremax = self.valueMinMax(data['score']) if width == 1: self.scaleColumn('dots', 'score', 'size', (threshold - 1, scoremax), (self.mindotsize, self.maxdotsize)) else: data['size'] = [self.mindotsize for _ in range(len(data['score']))] if color == 1: pass else: data['score'] = [scoremax for _ in range(len(data['score']))] if set_colormap: if color == 1: cmap = LinearColorMapper(self.palette, low=max(threshold - 1.0, scoremin - 1), high=scoremax) else: cmap = LinearColorMapper(self.palette, low=threshold - 0.1, high=threshold) self.cmap = cmap else: cmap = self.cmap source = ColumnDataSource(data) if mode == 'dot': figure.circle(source=source, x='x', y='y', size='size', line_color=transform('score', cmap), line_alpha=alpha, fill_color=transform('score', cmap), fill_alpha=alpha) else: # line mode figure.segment(source=source, x0='x', x1='x1', y0='y', y1='y1', line_width='size', line_color=transform('score', cmap), alpha=alpha) # color bar is in a separate window, self.legend, so it doesn't disturb the # aspect ratio if color: color_bar = ColorBar(color_mapper=cmap, label_standoff=3, bar_line_color='black', scale_alpha=alpha, width=20, margin=0, location=(0, 0), major_tick_in=20, major_tick_out=5, major_tick_line_color='black') legend.add_layout(color_bar, 'left') return True
def first_limit(H_file, H_fit1_df, figure=None): for value_v in iterate_on_v: # Selecting each subset of the dataframe corresponding to the desired v value. H_subset = H_file[H_file['v_value'] == v_serie[value_v]] # Shifting to numpy for the fit. H_np_subset = H_subset.to_numpy() # Building all the Xs values for the fits. Repetition of the same array, as the X values are all the same. Xs = H_np_subset[:, 1] # Size values are the 2nd column, index = 1 Xs = 1 / Xs # Inverting the values as the graph shows 1/size poly_X = np.insert( Xs, 0, 0) # Adding a 0 to the X set to have the value at this point # Hard coded indexes to take all the T_values, should be ok as long as I do not change the dataframe structure H_Ys = H_np_subset[:, 2:np.shape(H_np_subset)[1]] # Creating a figure for the desired v value. if (v_serie[value_v] == watch_v and figure != None): ListY = H_Ys.T.tolist() for plots in np.arange(0, len(ListY), 1): figure.circle(Xs, ListY[plots], legend_label="All T For v=" + str(watch_v), line_width=1, color='black', size=10, fill_color='white') #This is the main loop, populating the array with the fitted values for T in np.arange(0, len(T_serie), 1): if len(s_serie ) > 1: # Fit only if there are enough points to do so. H_coeff_poly = np.polyfit( Xs, H_Ys[:, T], fit_order_1) # Fitting a polynom on the data H_poly_Y = np.polyval( H_coeff_poly, poly_X ) # Calculating the values for each point and get the value at 0 # The point at X=0 is at the beginning of the array, putting it at the right place in the dataframe. H_fit1_df.iloc[[value_v], [T]] = H_poly_Y[0] else: # If only one point, just take the value of the point. H_fit1_df.iloc[[value_v], [T]] = H_Ys[:, T] H_poly_Y = H_Ys[:, T] # Showing the fit for the desired v_value if (v_serie[value_v] == watch_v and figure != None): figure.line( poly_X, H_poly_Y, # Adding points to check the quality of the fit. legend_label="Fits", line_width=2, color=colors[T]) figure.square(0, H_poly_Y[0], line_width=1, color='black', size=10, fill_color='white') return H_fit1_df