Interactive plotting with Bokeh
As a JupyterLab heavy user, I like using Bokeh for plotting because of its interactive plots. JupyterLab also offers an extension for interactive matplotlib , but it is slow and it crashes with bigger datasets.
A thing I don’t like about Bokeh is its overwhelming documentation and complex examples. Sometimes I want to make a simple line plot and I struggle with 10 or more lines of Bohek specific code. But Bokeh code can be very concise as I am going to show below. This is also the main goal, to show a few useful data visualizations with as little code as possible.
Haven’t heard of Bokeh ? Description from its website sums it up nicely:
Bokeh is an interactive visualization library that targets modern web browsers for presentation. Its goal is to provide elegant, concise construction of versatile graphics, and to extend this capability with highperformance interactivity over very large or streaming datasets. Bokeh can help anyone who would like to quickly and easily create interactive plots, dashboards, and data applications.
You can run this code by downloading thisJupyter notebook.
Requirements
import bokeh import numpy as np from bokeh.models import Circle, ColumnDataSource, Line, LinearAxis, Range1d from bokeh.plotting import figure, output_notebook, show from bokeh.core.properties import value output_notebook() # output bokeh plots to jupyter notebook np.random.seed(42)
print("bokeh", bokeh.__version__) print("numpy", np.__version__)
bokeh 1.0.4 numpy 1.15.1
Generata data
Let us generate some random data using numpy. Bokeh has its own data structure (ColumnDataSource) for data representation. I am not sure why they developed their own data structure as pandas and numpy are De facto standard in Python analytics world (enlighten me in the comments below if you know). But luckily it works also with pandas. For this blog post, I decided to write examples on Bokeh way with its data structures.
N = 100 data_source = ColumnDataSource( data=dict( x0=np.arange(N), x1=np.random.standard_normal(size=N), x2=np.arange(10, N + 10), x3=np.random.standard_normal(size=N), ) )
Simple line plot
To make a simple line plot in Bohek we need 3 lines of code. That’s not too bad. Note that the plot is interactive of the box, we can zoom in and move around, which is very useful with bigger datasets.
p = figure() p.line("x0", "x1", source=data_source) show(p)
Line plot with two axes
To visualize two data columns with different ranges on a plot we can use two separate yaxes. We can set yaxis ranges but that’s not required. I used the min and max values of a data column as a yaxis limit. To visually separate data columns we can add a legend and set the color.
p = figure() column1 = "x1" column2 = "x2" # FIRST AXIS p.line("x0", column1, legend=value(column1), color="blue", source=data_source) p.y_range = Range1d(data_source.data[column1].min(), data_source.data[column1].max()) # SECOND AXIS column2_range = column2 + "_range" p.extra_y_ranges = { column2_range: Range1d( data_source.data[column2].min(), data_source.data[column2].max() ) } p.add_layout(LinearAxis(y_range_name=column2_range), "right") p.line("x0", column2, legend=value(column2), y_range_name=column2_range, color="green",source=data_source) show(p)
Combine line and scatter plot
This is where Bokeh really shines. You can simply define multiple elements and Bokeh renders them on the plot.
p = figure() p.line(x="x0", y="x1",color="blue", source=data_source ) p.circle(x="x0", y="x3",color='green', source=data_source) show(p)
Final thoughts
Bokeh is very customizable. You can tweak all the things you would expect from a plotting library, like line width, colors, multiple plots on a grid, etc. It offers special plots like candlesticks for financial data, Burtin visualization and you can even make a periodic table. Unique to Bokeh (at least to my knowledge) is an option to export the plot to javascript code which enables you to directly embed the plot to a webpage with all of its interactive capabilities.
What do you think about Bokeh? Let me know in the comments below.
Share this:
猜你喜欢

7
Bokeh prides itself on being a library for interactive data visualization. Unlike popular counterparts in the Python visualization space, like Matplotlib and Seaborn, Bokeh renders its graphics using H...

48
pdvega  Interactive plotting for Pandas using VegaLite

50

68
Go is an open source programming language that makes it easy to build simple, reliable, and efficient software. It provides an expressive syntax with its lightweight type system and comes with…

28
glot  Glot is a plotting library for Golang built on top of gnuplot.

18
A repository for plotting and visualizing data

9
README.md pyecharts

9
作者简介 taoyan：R语言中文社区特约作家，伪码农，R语言爱好者，爱开源。 个人博客: https://ytlogos.github.io/

10
In this tutorial, you’ll be equipped to make productionquality, presentationready Python histogram plots with a range of choices and features. If you have introductory to intermediate knowledge in Python and st...

14
README.md ttyplot a realtime plotting utility for terminal with data input from stdin takes data from stdin, most commonly unix pipeline and plo...
 上一页
 下一页
关于头条
聚合每日国内外有价值，有趣的链接。