Assigning columnDataSource in Bokeh

I am new to bokeh and trying to figure out what columnDataSource does. He appears in many places, but I'm not sure of his purpose and how it works. Can someone light up? Sorry if this is a stupid question ...

+6
source share
2 answers

ColumnDataSource is an object that stores Bokeh graph data. You can not use the ColumnDataSource and directly transfer your graph using Python dictionaries, pandas frames, etc., but for some functions, such as the presence of a pop-up window displaying information about the data, when the user hovers over the glyphs, you are forced to use ColumnDataSource, otherwise the popup will not be able to receive data. Other uses may include streaming data.

You can create a ColumnDataSource from dictionaries and pandas dataframes, and then use ColumnDataSource to create glyphs.

+4
source

This should work:

import pandas as pd import bokeh.plotting as bp from bokeh.models import HoverTool, DatetimeTickFormatter # Create the base data data_dict = {"Dates":["2017-03-01", "2017-03-02", "2017-03-03", "2017-03-04", "2017-03-05", "2017-03-06"], "Prices":[1, 2, 1, 2, 1, 2]} # Turn it into a dataframe data = pd.DataFrame(data_dict, columns = ['Dates', 'Prices']) # Convert the date column to the dateformat, and create a ToolTipDates column data['Dates'] = pd.to_datetime(data['Dates']) data['ToolTipDates'] = data.Dates.map(lambda x: x.strftime("%b %d")) # Saves work with the tooltip later # Create a ColumnDataSource object mySource = bp.ColumnDataSource(data) # Create your plot as a bokeh.figure object myPlot = bp.figure(height = 600, width = 800, x_axis_type = 'datetime', title = 'ColumnDataSource', y_range=(0,3)) # Format your x-axis as datetime. myPlot.xaxis[0].formatter = DatetimeTickFormatter(days='%b %d') # Draw the plot on your plot object, identifying the source as your Column Data Source object. myPlot.circle("Dates", "Prices", source=mySource, color='red', size = 25) # Add your tooltips myPlot.add_tools( HoverTool(tooltips= [("Dates","@ToolTipDates"), ("Prices","@Prices")])) # Create an output file bp.output_file('columnDataSource.html', title = 'ColumnDataSource') bp.show(myPlot) # et voilà. 
+1
source

All Articles