Python Tutorial: Data formats

Learn which Python data structures work with Bokeh glyphs: lists, NumPy arrays, Pandas DataFrames, and ColumnDataSource. ColumnDataSource is Bokeh's core data abstraction, a dictionary mapping column names to equal-length sequences that can be shared across glyphs for linked selections.
Chapters
Now that we have some practice using basic glyph methods, let's take a closer look at what kinds of data formats can be easily used to drive glyph properties. We have already seen that standard Python lists work just fine and can be passed as a value for any glyph property. Another common possibility is NumPy arrays. NumPy is a python library for dealing with multidimensional arrays of data efficiently. It is the foundation of the scientific Python stack, and many of the powerful Python packages for analytics and data science rely on it directly or indirectly. Here we use the numpy function linspace to create an array of 1000 values evenly spaced between 0 and 10. These will be our x values. Then we use the numpy sine and random functions to compute a noisy sine curve based on those x values. Just like with Python lists, these numpy arrays can be passed directly to the line glyph method to specify the x and y coordinates. Another common Python library for data science is Pandas. Pandas provides a data frame structure analogous to the R data frame. It's efficient for working with tabular sets and time series. A Pandas dataframe is comprised of several columns, each with a unique name. These columns in a dataframe are accessed using square brackets, similar to a Python dictionary. As it happens, the underlying implementation of Pandas Columns uses NumPy arrays, so passing Pandas dataframe columns works exactly as you would expect. In this example, we have loaded the Flowers dataset, which contains measurements of three different species of Iris flowers. As a Pandas dataframe, we can show a scatter plot of petal length versus sepal length by passing the columns for petal length and sepal length to the circle function. Now it's time to talk about a data structure that is central to bokeh, the column data source. This data structure is used extensively in bokeh. It is what takes care of getting your data from Python to the final JavaScript and HTML document that is displayed to users. Essentially, it's a stripped down, simpler version of a data frame. It has a data attribute that is a Python dictionary which maps string names to sequences of data. Column data sources are often created for you, and in all the examples we have seen so far this has been the case. When you Pass in X List123 to the circle method, Bokeh creates a column data source for you behind the scenes and maps the name X to the list 123. But there are times when it can be useful to create or use column data sources directly. For instance, column data sources can be shared between glyphs and this enables selections between plots to be automatically linked together. We will see more about this in chapter two. It's also possible to add extra columns to column data sources that can be displayed in hover tooltips or used by other tools. We will come back to this in chapter two as well. Lets take a quick look at the details of column data sources. First, note that to use them directly we must import column data source from Bokeh models. Then we can create an instance of column data source by passing a data dictionary to the initializer. This dictionary should have string keys and sequences such as arrays or lists as the values. It is very important to note all the columns in a column data source must be the same length. The last thing to mention about column data sources is that they are easy to create directly from Pandas data frames. Here we have imported the flowers data set from Bokeh Sampledata iris. The data is stored in a Pandas data frame which we examine with the HEAD method. To create a column data source from this data frame, we simply pass the data frame to the column data source initializer. Now the data source can be passed to any glyph method. As mentioned earlier, the IRIS dataset has various measurements of flowers for three different iris species. It is a common dataset used in demonstrations and we will return to this dataset again in later examples.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







