2016-04-19 33 views
6

Sono nuovo a bokeh e sto cercando di capire cosa fa columnDataSource. Appare in molti posti ma sono incerto sul suo scopo e su come funziona. Qualcuno può illuminare? Mi scuso se questa è una domanda stupida ...Scopo di columnDataSource in bokeh

+2

Se si ha familiarità con R o Panda 'DataFrame' oggetti, il' ColumnDataSource' è fondamentalmente una versione più semplice di questo. È una raccolta di matrici di dati (colonne) a cui è possibile fare riferimento per nome. L'attuale struttura interna è proprio questa: un dizionario che mappa le stringhe in liste/array. È il modo principale in cui i dati vengono spostati da Python alla libreria del browser BokehJS. – bigreddot

risposta

4

ColumnDataSource è l'oggetto in cui sono memorizzati i dati di un grafico Bokeh. Puoi scegliere di non usare ColumnDataSource e alimentare il tuo grafico direttamente con i dizionari Python, i datafram pandas, ecc, ma per alcune funzionalità come avere una finestra popup che mostra le informazioni sui dati quando l'utente passa il mouse sui glifi, sei costretto a usare un ColumnDataSource altrimenti la finestra popup non sarà in grado di ottenere i dati. Altri usi sarebbero quando lo streaming dei dati.

È possibile creare un oggetto ColumnDataSource da dizionari e pandas dataframes e quindi utilizzare ColumnDataSource per creare i glifi.

+1

Potresti per favore aggiungere un piccolo esempio su come fare ciò che hai scritto nella risposta? Dire una serie temporale in cui si desidera vedere i dati quando si posiziona il mouse sopra il grafico – famargar

1

questo dovrebbe funzionare:

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à.