Recently, I've been watching a video at the SCIPY 2015 meeting in Texas Austin, USA – "Creating Python data applications with Blaze and bokeh" and can't help but rethink the power of these two libraries to the data scientists who use python around the world. In this article, I'll take you through the various possible ways to visualize data using bokeh, and why Bokeh is a must-have artifact for every data scientist.
What is Bokeh?
Bokeh is an interactive visual Python library dedicated to the rendering capabilities of Web browsers. This is the core difference between bokeh and other visual libraries. As shown, it illustrates the process by which bokeh data is presented to a Web browser.
As you can see, bokeh is bundled with multiple languages (Python, R, Lua, and Julia). These bundled languages produce a JSON file that acts as an input to the BOKEHJS (a Java library) and then presents the data to a modern web browser.
Bokeh can create simple and beautiful interactive visualizations like D3.js, even for very large or streaming datasets that can interact efficiently. Bokeh can help everyone quickly and easily create interactive charts, dashboards, and data applications.
What can bokeh provide for data scientists like me?
I started my data science journey as a Business intelligence practitioner (BI Professional), and then gradually learned predictive modeling, data science, and machine learning. I mainly use Qlikview and tableau for data visualization, using SAS and Python for predictive analytics and data analysis. I have hardly ever used java.
So, for all of my previous data products or ideas, I had to either outsource it or show it to others through the website wireframe, neither of which would be suitable for creating a quick prototype. Now, with bokeh, I can continue to use Python and quickly create these prototypes.
Advantages of Bokeh:
-
Bokeh allows you to quickly create complex charts with simple instructions.
-
Bokeh provides output to a variety of media, such as Html,notebook documents and servers
-
• We can also embed bokeh visualizations into flask and Django programs
-
Bokeh can convert visualizations written in other libraries (such as Matplotlib, Seaborn, and Ggplot)
-
· Bokeh has the flexibility to use interactive applications, layouts, and different styling options for visualization
Combining the advantages of bokeh and its challenges, bokeh is the ideal tool for the rapid development of prototype products. However, if you want to make something new in the context of the product, D3.js may still be your best bet.
The challenges facing bokeh:
-
As with any upcoming open source Library, Bokeh is undergoing constant change and development. Therefore, the code you write today may not be fully reused in the future.
-
Bokeh has relatively few visualization options compared to d3.js. Therefore, short-term bokeh can not challenge the supremacy of d3.js.
Combining the advantages of bokeh and its challenges, bokeh is the ideal tool for the rapid development of prototype products. However, if you want to make something new in the context of the product, D3.js may still be your best bet.
Visualize with Bokeh
Bokeh provides powerful and flexible functionality that makes it simple to operate and highly customizable. It provides users with a number of visual interfaces, as shown in:
-
Chart (Charts): An advanced Interface (high-level interface) for simple and fast building of complex statistical charts.
-
Drawing (plotting): An intermediate interface (Intermediate-level interface) to build various visual symbols as the core.
-
Module (Models): A low-level interface (low-level interface) that provides maximum flexibility for application developers.
In this article, we only deal with the first two interfaces, charts (Charts) and plots (plotting).
Chart
As mentioned above, it is an advanced interface for presenting information in a standard visual way. These include box plots, histograms, area charts, heat map, doughnut charts, and many other graphics. You can generate these graphs by simply entering data frames, numpy arrays, or dictionaries.
Let's look at a common way to create a chart:
1. Importing libraries and functions/methods
2. Preparing the data
3. Set the output mode (notebook document, Web browser or server)
4. Create a chart and select a chart style (if required)
5. Visualize Charts
To better understand these steps, let me illustrate with the following example:
Chart Example 1: use bokeh to create a bar chart and display it on a Web browser
We will follow the steps listed above to create a chart:
#Import library functions
From bokeh.charts import Bar, output_file, show
Visualize Data #Use Output_notebook on your computer screen
#Prepare data (analog data)
data = {"Y": [1, 2, 3, 4, 5]}
#Output to Line. HTML
Output_file ("lines.html", title = "line plot example")
#Create a new window with title and axis labels online window
p = Bar (data, title = "line Chart Example", xlabel = 'x', ylabel = 'values', width = 400, height = 400)
#Show results
Show (P)
In the chart above, you can see the tool options at the top (zoom, resize, reset, rotate zoom), which can help you interact with the chart. You can also see multiple chart options (legend, x-axis name callout, y-axis name callout, coordinate grid line, width, height, etc.) and examples of various charts.
Chart Example 2: in the notebook document, the distribution of the length of sepals (sepal length) and petal length (petal) in the iris dataset is compared with a box plot
To create this visual chart, I will first import the iris dataset using the Sklearn library. Then, follow the steps above to visualize the chart in the Ipython notebook document.
#IRIS Data Set
From sklearn.datasets import Load_iris
Import Pandas as PD
Iris = Load_iris ()
DF=PD. DataFrame (Iris.data)
df.columns=[' petal_width ', ' petal_length ', ' sepal_width ', ' sepal_length ']
#Import library functions
From bokeh.charts import BoxPlot, Output_notebook, show
data = df [['petal_length', 'sepal_length']
#Output to computer screen
Output_notebook ()
#Create a new window with title and axis labels online window
p = boxplot (data, width=400, height=400)
# Show Results
Show (P)
Chart Example-3: Create a line graph to bokeh server
Before you can draw a visual diagram to a bokeh server, you need to run the server first.
If you are using the Conda package, you can use the Run command "Bokeh-server" in any directory. If not, "Python./bokeh-server" is usually also available.
There are several advantages to visualizing a drawing on a bokeh server:
-
The chart has more audiences
-
Enables interactive visualization of large data sets
-
Automatically update charts based on data flow
-
Creating Control panels and applications
Before I start drawing on the bokeh server, I run the "bokeh-server" directive to initialize it and then run the related instructions for the visualization.
From bokeh.plotting import figure, Output_server, show
Output_server ("line")
p = figure (plot_width=400, plot_height=400)
# Add Line Rendering
P.line ([5, 2, 3, 4, 5], [5, 7, 2, 4, 5], line_width=2)
Show (P)
Drawing
Plotting is an intermediate interface, which is the core of building visual symbols. Here, you can combine various visual elements (dots, circles, lines, patches, and many other elements) and tools (hover, zoom, save, Reset, and other tools) to create visualizations.
A chart created with the plotting interface of Bokeh comes with a set of default tools and visual effects. The drawing can be done in the following steps:
1. Importing libraries, methods, or functions
2. Select the output mode (notebook document, Web browser, server)
3. Activation diagram (similar to matplotlib)
4. Perform subsequent drawing operations, which will affect the graphs that have been generated.
5. Visualization of graphs
To better understand these steps, let me illustrate:
Drawing Example 1: creating a two-dimensional scatter plot (square marker) in a notebook document
From bokeh.plotting import figure, Output_notebook, show
# Output to the computer screen
Output_notebook ()
p = figure (plot_width=400, plot_height=400)
#Add a rectangular box, marked with size, color, alpha value
P.square ([2, 5, 6, 4], [2, 3, 2, 1, 2], size = 20, color = "Navy")
#Show results
Show (P)
Similarly, you can create a variety of other types of graphs: lines, corners and arcs, ellipses, images, patches, and many other diagrams.
example of drawing-2: merging two visual elements into one picture
From bokeh.plotting import figure, Output_notebook, show
# Output to the computer screen
Output_notebook ()
p = figure (plot_width=400, plot_height=400)
#Add a rectangle, marked with size, color, alpha value
P.square ([2, 5, 6, 4], [2, 3, 2, 1, 2], size=20, color= "Navy")
P.line ([1, 2, 3, 4, 5], [1, 2, 2, 4, 5], line_width=2) #added a line plot to existing figure
# Show Results
Show (P)
Drawing Example 3: to add a hover tool and axis labels
From bokeh.plotting import figure, Output_notebook, show
From bokeh.models import Hovertool, Boxselecttool #For enabling tools
# Output to the computer screen
Output_notebook ()
#Add hover tool
TOOLS = [Boxselecttool (), Hovertool ()]
p = figure (plot_width = 400, plot_height = 400, Tools = tools)
#Add a rectangular box, marked with size, color, alpha value
P.square ([2, 5, 6, 4], [2, 3, 2, 1, 2], size = 20, color = "Navy", alpha = 0.5)
#Show axis labels
P.xaxis.axis_label = "X-axis"
P.yaxis.axis_label = "Y-axis"
# Show Results
Show (P)
Drawing Example 4: map India using latitude and longitude data
Note: I already have a CSV format for the latitude and longitude polygon data of the Indian border. I'll use that data to draw.
Here we will use patch drawing, let's take a look at the following command:
#Import library functions
Import Pandas as PD
From bokeh.plotting import figure, Show, Output_notebook
#Import latitude and longitude coordinates
India = pd.read_csv ('E: /india.csv')
Del india ['ID']
india.index = ['IN0', 'IN1', 'IN2', 'IN3', 'IN4', 'IN5']
#If the coordinates in the data frame are character type conversion character value is a floating point value
For j in Range (0, len (India)):
A = india ['lats'] [j]
india ['lats'] [j] = [Float (i) for I in A [1: len (a) -1] .split (",")]
For j in Range (0, len (India)):
A = india ['Lons'] [j]
india ['Lons'] [j] = [Float (i) for I in A [1: len (a) -1] .split (",")]
#Output to computer screen
Output_notebook ()
# Create Artboards
p = figure (plot_height = 400, plot_width = 400, toolbar_location = "right", X_axis_type = none, Y_axis_type = none)
P.patches (xs = india ['lons'), ys = india ['lats'], fill_color = "white", line_color = "Black", line_width = 0.5)
#Visualization icon
Show (P)
Micro bo book http://www.biyinjishi.com/products/a65-b6580/d100147/
genealogy http://www.biyinjishi.com/products/a65-b6580/d100149/
Logo Design http://www.biyinjishi.com/products/a70-b7010/
Business Card Design http://www.biyinjishi.com/products/a70-b7015/
Leaflet Design http://www.biyinjishi.com/products/a70-b7020/
Propaganda album design http://www.biyinjishi.com/products/a70-b7025/
Repair film Color http://www.biyinjishi.com/products/a70-b7050/
Typing input http://www.biyinjishi.com/products/a70-b7060/
Document Snapshot http://www.biyinjishi.com/products/a99-b9910/
send and receive fax http://www.biyinjishi.com/products/a99-b9915/
binding and binding http://www.biyinjishi.com/products/a99-b9920/
Lettering Engraved http://www.biyinjishi.com/products/a99-b9925/
Disc Burning http://www.biyinjishi.com/products/a99-b9960/
Enable interactive data visualization in Python