<jupyter_start><jupyter_text><img                src="assets/bokeh-transparent.png"                style="width:50px"           >                                        Bokeh Tutorial          04. Data Sources and Transformations<jupyter_code>from bokeh.io import output_notebook, show
from bokeh.plotting import figure
output_notebook()<jupyter_output><empty_output><jupyter_text>OverviewWe've seen how Bokeh can work well with Python lists, NumPy arrays, Pandas series, etc. At lower levels, these inputs are converted to a Bokeh `ColumnDataSource`. This data type is the central data source object used throughout Bokeh. Although Bokeh often creates them for us transparently, there are times when it is useful to create them explicitly.In later sections we will see features like hover tooltips, computed transforms, and CustomJS interactions that make use of the `ColumnDataSource`, so let's take a quick look now.   Creating with Python DictsThe `ColumnDataSource` can be imported from `bokeh.models`:<jupyter_code>from bokeh.models import ColumnDataSource<jupyter_output><empty_output><jupyter_text>The `ColumnDataSource` is a mapping of column names (strings) to sequences of values. Here is a simple example. The mapping is provided by passing a Python `dict` with string keys and simple Python lists as values. The values could also be NumPy arrays, or Pandas sequences.***NOTE: ALL the columns in a `ColumnDataSource` must always be the SAME length.***<jupyter_code>source = ColumnDataSource(data={
    'x' : [1, 2, 3, 4, 5],
    'y' : [3, 7, 8, 5, 1],
})<jupyter_output><empty_output><jupyter_text>Up until now we have called functions like `p.circle` by passing in literal lists or arrays of data directly, when we do this, Bokeh creates a `ColumnDataSource` for us, automatically. But it is possible to specify a `ColumnDataSource` explicitly by passing it as the `source` argument to a glyph method. Whenever we do this, if we want a property (like `"x"` or `"y"` or `"fill_color"`) to have a sequence of values, we pass the ***name of the column*** that we would like to use for a property:<jupyter_code>p = figure(plot_width=400, plot_height=400)
p.circle('x', 'y', size=20, source=source)
show(p)
# Exercise: create a column data source with NumPy arrays as column values and plot it<jupyter_output><empty_output><jupyter_text>Creating with Pandas DataFramesIt's also simple to create `ColumnDataSource` objects directly from Pandas data frames. To do this, just pass the data frame to  `ColumnDataSource` when you create it:<jupyter_code>from bokeh.sampledata.iris import flowers as df

source = ColumnDataSource(df)<jupyter_output><empty_output><jupyter_text>Now we can use it as we did above by passing the column names to glhph methods:<jupyter_code>p = figure(plot_width=400, plot_height=400)
p.circle('petal_length', 'petal_width', source=source)
show(p)
# Exercise: create a column data source with the autompg sample data frame and plot it

from bokeh.sampledata.autompg import autompg_clean as df<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07730101539469375, 'fraction_numerical': 0.00917130691123485, 'mean_word_length': 4.988235294117647, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '176540', 'license': 'MIT', 'n_tokens_mistral': 898, 'n_tokens_neox': 860, 'n_words': 372, 'path': 'bokeh/tutorial/04 - Data Sources and Transformations.ipynb', 'repo_name': 'aagrosch/jupyter4jsc', 'size': '3053'}