Python uses Bokeh for visualization

Source: Internet
Author: User
Tags virtual environment jupyter notebook

The Bokeh package is an interactive visualization library. It uses a web browser for presentation. The goal is to draw a pattern in the D3.js style. This will make the image look beautiful and easy to construct. Bokeh supports a large number of streaming datasets. You can use this library to create various charts/graphs. One of its main competitors may be Plotly.

Note: D3.js is a JavaScript library that can be used to create Data Driven Documents. For details, see here
Note: This is not an in-depth tutorial on the Bokeh library, because it can draw too many different charts and visual images. Therefore, the purpose of this article is to show readers the richness of the Bokeh library and see what interesting things it can do.

Let's spend some time installing Bokeh. The simplest way is to use pip or conda, for example, pip:

Pip install bokeh
This command installs Bokeh and all its dependent packages. For this reason, you may want to install Bokeh in a virtual environment, but it depends entirely on you. Now, let's use a simple example to check whether the installation is successful. Save the following code to a file. The file name is as you like.

From bokeh. Plow.import figure, output_file, show

Output_file ("/path/to/test.html ")

X = range (1, 6)
Y = [10, 5, 7, 1, 6]
Plot = figure (title = 'line example ', x_axis_label = 'x', y_axis_label = 'y ')
Plot. line (x, y, legend = 'test', line_width = 4)
Show (plot)
Here, we only imported some entries from the Bokeh library, and only explained where to save the output. You will notice that the output is an HTML document. Then we generate some values for the x and y axes for creating charts. Then, we created a figure object and set the title and the labels of the two coordinate axes. Finally, we draw this line, give the legend, set the line width, and display it. The show command automatically opens your default browser and displays charts in it. In the end, you will see the following:

 

Bokeh also supports Jupyter Notebook. The only thing to modify is to replace output_file with output_notebook.

Note: output_notebook (). Parameters are no longer required.
Bokeh's quick start guide provides a concise example of drawing a series of sine waves on a grid line. I slightly reduced the code and left only a sine wave. Note: To make the following example run properly, you need to install NumPy first.

Import numpy as np

From bokeh. layouts import gridplot
From bokeh. Plow.import figure, output_file, show

N = 100
X = np. linspace (0, 4 * np. pi, N)
Y0 = np. sin (x)

Output_file('sinewave.html ')

Sine = figure (width = 500, plot_height = 500, title = 'sine ')
Sine. circle (x, y0, size = 10, color = "navy", alpha = 0.5)

P = gridplot ([sine], toolbar_location = None)

Show (p)
The main difference between this example and the previous one is that we use NumPy to generate data points, and we place the graph inside the gridline, instead of drawing the graph itself. When you run this code, the final chart is as follows:

 

If you do not like a circle, Bokeh also supports other shapes that you like, such as squares, triangles, and various other shapes.

Summary

The Bokeh project is really interesting. It provides easy-to-use APIs for creating patterns, charts, and other data visualization forms. Bokeh documents are well organized. It contains a large number of examples to show what you can do with it. It is worth browsing its documents so that you can see what other charts look like and how short the code is to generate such beautiful results.

My only complaint is that Bokeh does not provide a way to save images by programming. Over the past two years, this has been a bug they are committed to modifying. The good news is that they have found a way to support this feature soon. In addition, I think it's cool.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.