Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Drawing Data With Glyphs

An empty figure isn’t all that exciting, so in this video you will explore glyphs: the building blocks of Bokeh visualizations. A glyph is a vectorized graphical shape or marker that is used to represent your data.

A Few Categories of Glyphs:

  • Marker: Shapes like circles, diamonds, squares and triangles. Effective for scatter and bubble charts.
  • Line: Single, step and multi-line shapes. For building line charts.
  • Bar/Rectangle: Traditional or stacked bar (hbar) and column (vbar) charts as well as waterfall or gantt charts.

More examples can be found in the Bokeh gallery. After you create your figure, you are given access to a bevy of configurable glyph methods.

File: FirstGlyphs.py

Python
# Bokeh Libraries
from bokeh.io import output_file
from bokeh.plotting import figure, show

# My x-y coordinate data
x = [1, 2, 1]
y = [1, 1, 2]

# Output the visualization to a static HTML file - first_glyphs.html
output_file('first_glyphs.html', title='First Glyphs')

# Create a figure with no toolbar and axis ranges [0, 3]
fig = figure(title='My Coordinates',
             plot_height=300, plot_width=300,
             x_range=(0, 3), y_range=(0, 3),
             toolbar_location=None)

# Draw the coordinate as circles
fig.circle(x=x, y=y, 
           color='green', size=10, alpha=0.5)

# Show plot 
show(fig)

00:00 Now it’s time to draw your data with some glyphs. So, what are glyphs? Glyphs are the building blocks of Bokeh visualizations. A glyph is a vectorized graphical shape or marker that is used to represent your data. Here are a few different categories of glyphs.

00:15 They can be a marker—shapes like circles, diamonds, squares, and triangles. They’re very effective for things like scatter or bubble charts. It can be a line—single, step, and multiline shapes for building line charts.

00:30 A bar or rectangle—these would be used to create traditional or stacked bar charts as well as waterfall or Gantt charts. Here’s a look at the Bokeh gallery, with all sorts of different types of glyphs that are available. Now it’s time to make your own.

00:47 Let’s start with a new basic example, drawing a few points on an x-y coordinate grid. Okay. To do that, you’re going to create a new file.

00:57 This new file is going to be called FirstGlyphs.py. You’re going to start it out very similarly by bringing in the same libraries we did before from Bokeh libraries.

01:09 from bokeh.io import output_file,

01:15 and from bokeh.plotting import figure, show. Next up, you need to set up your data. In this case, you’re going to set up an x-y coordinate.

01:28 x will be a list with integers 1, 2, and 1. y will be a list with the integers 1, 1, 2. So, these are the coordinates.

01:37 The first point will be at (1, 1), the second will be at (2, 1), and the third will be at (1, 2). So again, you’re going to create a static HTML file. Its name will be first_glyphs.html. How to do that?

01:50 You’re going to use that same method that we did previously, output_file(). And then the parameters are going to be the filename, this filename will be 'first_glyphs.html'.

02:05 It will have a title in your browser of 'First Glyphs'. Great. Now you need to create that background again, the canvas that you’re going to use, called a figure.

02:16 This figure you’re going to set up to have no toolbar and then the axis ranges are going to be from 0 to 3. So fig will be a figure(), call it with these parameters, title='My Coordinates'.

02:35 On the next line, set up a plot_height equal to 300, and a plot_width actually to the same.

02:45 x_range will be from 0 to 3,

02:50 and then y_range will be the same.

02:54 Next, you’ll do a nice piece of customization for the toolbar. Its location can be set to None. That’ll make it disappear. Now that you’ve built your figure—your canvas, if you will—now you can start drawing the glyphs. You’re going to have them be circles, so fig.circle(), and now you set the parameters for the circles.

03:17 x will be equal to the x list and y will be set to the y list. Next up, we’ll have a color and it’ll be 'green',

03:28 a size of 10 and a transparency_alpha of 0.5. Okay! Now you can show your plot by showing fig. I’m going to save.

03:41 This all looks good. Okay. FirstGlyphs.py, and there we go. There’s our title, My Coordinates. You can see the scale being set from 0 to 3 for the x- and the y-axis.

03:54 They don’t have labels, they’re simply just showing the values. And there’s our different coordinates. Great! And you can see the transparency, seeing the grid lines below it.

04:05 That’s that alpha. Awesome! Now, let me have you do something a little more advanced—something with multiple glyphs in it. That’s up next.

Become a Member to join the conversation.