DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > Python: Visualization With Bokeh

Python: Visualization With Bokeh

Mike Driscoll introduces the Bokeh visualization library with some basic examples.

Mike Driscoll user avatar by
Mike Driscoll
·
Aug. 01, 16 · Web Dev Zone · Opinion
Like (3)
Save
Tweet
4.50K Views

Join the DZone community and get the full member experience.

Join For Free

the bokeh package is an interactive visualization library that uses web browsers for its presentation. its goal is to provide graphics in the vein of d3.js that look elegant and are easy to construct. bokeh supports large and streaming datasets. you will probably be using this library for creating plots / graphs. one of its primary competitors seems to be plotly .

note: this will not be an in-depth tutorial on the bokeh library as the number of different graphs and visualizations it is capable of is quite large. instead, the aim of the article is to give you a taste of what this interesting library can do.

let’s take a moment and get it installed. the easiest way to do so is to use pip or conda. here’s how you can use pip:

pip install bokeh

this will install bokeh and all its dependencies. you may want to install bokeh into a virtualenv because of this, but that’s up to you. now let’s check out a simple example. save the following code into a file with whatever name you deem appropriate.

from bokeh.plotting 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 just import a few items from the bokeh library. we just tell it where to save the output. you will note that the output is html. then we create some values for the x and y axes so we can create the plot. then we actually create the figure object and give it a title and labels for the two axes. finally we plot the line, give it a legend and line width and show the plot. the show command will actually open your plot in your default browser. you should end up seeing something like this:

bokeh_line

bokeh also supports the jupyter notebook with the only change being that you will need to use output_notebook instead of output_file .

the bokeh quick start guide has a neat example of a series of sine waves on a grid plot. i reduced the example down a bit to just one sine wave. note that you will need numpy installed for the following example to work correctly:

import numpy as np

from bokeh.layouts import gridplot
from bokeh.plotting 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 are using numpy to generate the data points and we’re putting our figure inside of a gridplot instead of just drawing the figure itself. when you run this code, you should end up with a plot that looks like this:

bokeh_sine_wave

if you don’t like circles, then you’ll be happy to know that bokeh supports other shapes, such as square, triangle and several others.

wrapping up

the bokeh project is really interesting and provides a simple, easy-to-use api for creating graphs, plots and other visualizations of your data. the documentation is quite well put together and includes lots of examples that showcase what this package can do for you. it is well worth just skimming the documentation so you can see what some of the other graphs look like and how short the code examples are that generate such nice results. my only gripe is that bokeh doesn’t have a way to save an image file programmatically. this appears to be a long term bug that they have been trying to fix for a couple of years now. hopefully they find a way to support that feature soon. otherwise, i thought it was really cool!

Visualization (graphics) Python (language)

Published at DZone with permission of Mike Driscoll, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Everything You Need to Know About Web Pentesting: A Complete Guide
  • Kubernetes Data Simplicity: Getting Started With K8ssandra
  • How Do You Know If a Graph Database Solves the Problem?
  • Everything You Need to Know About Cloud Automation in 2022

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo