Bokeh: Quick Start
Sumber: http://bokeh.pydata.org/en/latest/docs/quickstart.html
Quick Installation
There are a few different ways to install Bokeh.
If you are using the Anaconda Python distribution (which is recommended), enter this command at a Bash or Windows command prompt:
conda install bokeh
This installs all the dependencies that you need to be ready to run Bokeh and we strongly recommend using it. It reduces the installation effort to nearly zero on any platform and configuration (including Windows). It also installs the examples into the examples/ subdirectory of your Anaconda (or miniconda) installation directory.
If you are confident that you have dependencies such as NumPy, Pandas, and Redis installed, then you can also use pip at the command line:
pip install bokeh
Note
The pip method does not install the examples. Clone the Git repository and look in the examples/ directory of the checkout to see examples.
Windows users, see Notes for Windows Users in the Installation section. Getting Started
Bokeh is a large library that exposes many capabilities, so this section is only a quick tour of some common Bokeh use-cases and workflows. For more detailed information please consult the full User Guide.
Let’s begin with some examples.
Plotting some data in basic Python lists as a line chart including zoom, pan, resize, save, and other tools is simple and straightforward:
from bokeh.plotting import figure, output_file, show
- prepare some data
x = [1, 2, 3, 4, 5] y = [6, 7, 2, 4, 5]
- output to static HTML file
output_file("lines.html", title="line plot example")
- create a new plot with a title and axis labels
p = figure(title="simple line example", x_axis_label='x', y_axis_label='y')
- add a line renderer with legend and line thickness
p.line(x, y, legend="Temp.", line_width=2)
- show the results
show(p)
When you execute this script, you will see that a new output file "lines.html" is created, and that a browser automaticaly opens a new tab to display it. (For presentation purposes we have included the plot output directly inline in this document.)
The basic steps to creating plots with the bokeh.plotting interface are:
Prepare some data (in this case plain python lists). Tell Bokeh where to generate output (in this case using output_file(), with "lines.html" as the filename to save as). Call figure() to create a plot with some overall options like title, tools and axes labels. Add renderers (in this case, Figure.line) for our data, with visual customizations like colors, legends and widths to the plot. Ask Bokeh to show() or save() the results.
Steps three and four can be repeated to create more than one plot. See some examples of this below.
The bokeh.plotting interface is also quite handy if we need to customize the output a bit more by adding more data series, glyphs, logarithmic axis, etc. It’s also possible to easily combine multiple glyphs together on one plot as shown below:
from bokeh.plotting import figure, output_file, show
- prepare some data
x = [0.1, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0] y0 = [i**2 for i in x] y1 = [10**i for i in x] y2 = [10**(i**2) for i in x]
- output to static HTML file
output_file("log_lines.html")
- create a new plot
p = figure(
tools="pan,box_zoom,reset,save", y_axis_type="log", y_range=[0.001, 10**11], title="log axis example", x_axis_label='sections', y_axis_label='particles'
)
- add some renderers
p.line(x, x, legend="y=x") p.circle(x, x, legend="y=x", fill_color="white", size=8) p.line(x, y0, legend="y=x^2", line_width=3) p.line(x, y1, legend="y=10^x", line_color="red") p.circle(x, y1, legend="y=10^x", fill_color="red", line_color="red", size=6) p.line(x, y2, legend="y=10^x^2", line_color="orange", line_dash="4 4")
- show the results
show(p)
Jupyter Notebooks
At this point we should mention Jupyter (formerly IPython) notebooks.
Jupyter notebooks are a fantastic tool for exploratory data analysis, and they are widely used across the “PyData” community. Bokeh integrates seamlessly with Jupyter notebooks. To view the above examples in a notebook, you would only change output_file() to a call to output_notebook() instead.
A large number of static examples may be viewed directly online at the Bokeh NBViewer Gallery.
The Bokeh GitHub repository also has a number of example notebooks in the examples/plotting/notebook/ directory. After cloning the repository, navigate there and run:
ipython notebook
You can open and interact with any of the notebooks listed in the index page that automatically opens up. In particular, you might check out the interact_basic and interact_numba examples that show how Bokeh can be used together with Jupyter interactive widgets.