Want to learn more? Take the full course at https://learn.datacamp.com/courses/in... at your own pace. More than a video, you'll learn hands-on coding & quickly apply skills to your daily work.
---
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 multi-dimensional 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 zero and ten. These will be our "x-values" Then we use the NumPy "sin" and "random" functions to compute a noisy sin curve based on the x-values.
Just like with python lists, these NumPy arrays can passed directly to the circle glyph method to specify the x and y coordinates.
Another common Python library for data science is Pandas. Panda provides a DataFrame structure (analogous to the R data frame) that is efficient for working with tabular data sets and time series.
A Pandas dataframe is comprised of several columns, each with a unique name. The 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" data set, which contains measurements of three different species of iris flowers, as a pandas dataframe. We can show a scatter plot of petal length vs 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 extensivey in Bokeh. It what takes care of getting your data from Python to the final JavaScript and HTML document that is displayed to your users.
Essentially it is a stripped down, simpler version of a data frame: it has a .data attribute that is python dictionary, that 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 = "list 1 2 3" to the circle method, Bokeh creates a column ata source for you behind the scenes and maps the name "x" to the list 1 2 3
But there are times when it is useful to be able to create or use column data sources directly:
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 2.
It's also possible to add extra columns to column data sources that can be displayed in hover tooltips or by other tools. We will come back to this in chapter 2 as well.
Let's take a quick look at the details of column data sources. First note that to use them directly we must import ColumnDataSource from bokeh.models.
Then we can create an instance of column data source by passing a data dictionary to the initializer. The dictionary should have string keys, and sequences (such as lists or arrays) as 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" dataset 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 source can be passed to any glyph method.
As menrioned earlier, the iris data set has various measurements of flowers for three different iris species. It iss a common data set used in demonstrations and we will return to this data set again in later examples.
#PythonTutorial #Python #DataCamp #Interactive #Data #Visualization #Bokeh #glyphs
In questa pagina del sito puoi guardare il video online Python Tutorial: Data formats della durata di ore minuti seconda in buona qualità , che l'utente ha caricato DataCamp 15 marzo 2020, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 241 volte e gli è piaciuto 4 spettatori. Buona visione!