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

Unlock This Lesson

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

Unlock This Lesson

Multiple Glyphs and Adding a Legend

In this video you will build on top of what you learned in the last video about glyphs. You will create a visualization with multiple glyphs and learn about how to add a legend. Along the way you will practice using numpy and the methods numpy.linspace() and numpy.cumsum().

You can check out much more info about styling legends.


import numpy as np 

# Bokeh libraries
from import output_file
from bokeh.plotting import figure, show

# My word count data
day_num = np.linspace(1, 10, 10)
daily_words = [450, 628, 488, 210, 287, 791, 508, 639, 397, 943]
cumulative_words = np.cumsum(daily_words)

# Output the visualization to 
# a static HTML page my_tutorial_progress.html
output_file('my_tutorial_progress.html', title='My Tutorial Progress')

# Create a figure with a datetime type x-axis
fig = figure(title='My Tutorial Progress',
             x_axis_label='Day Number', 
             y_axis_label='Words Written',
             y_range=(0, 6000),

# The daily words will be represented as vertical bars (columns)
fig.vbar(x=day_num, bottom=0, top=daily_words,
         color='blue', width=0.75, 

# The cumulative sum will be a trend line
fig.line(x=day_num, y=cumulative_words,
         color='gray', line_width=1,

# Put the legend in the upper left corner
fig.legend.location = 'top_left'

# Let's check it out

00:00 Following up on the last tutorial, now you’re going to create a visualization with multiple glyphs. You’re also going to add a legend. Okay. Back in your editor, create a new file called MyTutorialProgress. In the process of creating the written tutorial, the author kept track of how many words that he wrote every day.

00:21 This will be a visualization based upon that. It’s also going to need a little help from NumPy. When you installed pandas, NumPy came along with it. So the first line in your script is to import numpy as np.

00:35 You’re going to import the same libraries as you’ve done before. from import output_file,

00:43 and then from the plotting library, import figure and show. In setting up this data, you’re going to do a little bit of word count data. Data across 10 days, so to create that list of 10 days, you’re going to use something from the NumPy library called linspace().

00:59 So, what linspace() will do, it’ll return evenly-spaced numbers over a specified interval. You set a start, a stop, and the number is how many samples.

01:08 So from 1 to 10, 10 actual spaces, so this is going to give the 10 days without having to type out [1, 2, 3] and so forth. So, 1 to 10.

01:19 During those 10 days, daily_words are going to be typed in as a list of numbers.

01:28 Along with that, a cumulative sort of running total is going to be created also, and we can use a nice feature of NumPy called cumsum(), which is a cumulative sum.

01:38 And so as it falls along the given axis here of all these particular arrays, it’s going to add them together and return the sum each time as each one gets added on to it. So, you can provide it daily_words, and that’s going to create a new NumPy array with a running total.

01:57 Great! So, that’s going to set up the data, sort of our x and y. Now you need to say where you are going to output this.

02:07 You’re going to make another static HTML page using output_file(). It’s going to be called my_tutorial_progress.html. I’m going to copy that so I can use it here in my next piece. Okay.

02:21 So, output_file(), first one is the name, second is the title. The title at the top of that browser is going to say 'My Tutorial Progress'.

02:31 Next step, create that figure.

02:39 So, using an instance of figure(), create a title,

02:47 set the height equal to 400,

02:51 and the width equal to 700, and an x-axis label that will be the 'Day Number', and a y-axis label of 'Words Written'.

03:07 One thing you can do with the x-axis and the y-axis is you can look at the ticks that mark the scale, and you can set a minor value of ticks with the numbers that will be displayed.

03:17 You’re going to set that to 2. And then you’re also going to set a y range, and this is for the number of words that are written. It needs to have the scale for the cumulative amount, so it’s going to go from 0 to 6000. And last, what about the toolbar? Well, in this case, set it to None again.

03:38 Great. Now it’s time for you to create the glyphs. First is going to be a vertical bar showing off the daily words.

03:49 So, how do you do that? Well, you can see all the glyphs that are available here from fig. You’re going to use .vbar(). It takes a variety of parameters into it. The first, the x value, which will be that NumPy array of day_num. For a vertical bar, you set two values, a bottom—in this case, everything’s going to start at the very bottom of 0, no words, basically—and then the tops of these vertical bars will be set to the value of daily_words, your y-axis. So in this case, 0 to 450 on day one. On day two, 0 to 628.

04:25 That’s what the vertical bar would look like. Next, set a color. In this case, use 'blue'. Next, for the vertical bar, you can set a width of the bars. Set it to 0.75.

04:39 That way, they’re not touching. There’s a little gap between each of the vertical bars, which will be nice. And last, we’ll talk more about this as we continue along here, but you’re going to set a legend. And the legend’s title for this particular element, this glyph that you’re putting in, is going to be 'Daily'. Well, legend is pretty neat. It’s going to take these other factors—the color and the style—and put that in the legend also graphically. Okay.

05:03 Here’s where the fun starts. You can have more than one thing showing up here in your figure. You’re going to create a trend line that’s going to be used to show the progress across all those days, and that’s going to be that cumulative sum. So to do that, this time the glyph’s going to be a line. The x for it will be day_num again, 1 to 10, and the y is going to equal the cumulative_words.

05:26 You’re going to set a color to 'gray' and a very thin line width of 1. In this case, the legend is going to have another entry now that will say 'Cumulative'. There!

05:38 Now you’ve done a line and the vertical bars. Next step is to put that legend on the figure in the upper left corner. So fig.legend, and we can choose a .location.

05:50 It will be the top-left corner. Last, it’s your old friend show(), and you’re going to show the fig, which again, in this case, is going to output to this file. Great!

06:05 You’re going to save,

06:10 and now run your script.

06:15 And here it is. On the left, for the y-axis you see the Words Written, and the legend over there, Daily and Cumulative with the two styles of the vertical bars and the cumulative line and their colors.

06:31 There’s your title. Great! And you can see the minor tick level here of two every other day. All right, great job! Now it’s time for a short review of all the things you learned up to this point.

Avatar image for Cooper Labenske

Cooper Labenske on Aug. 11, 2020

legend is being deprecated. For:


Instead use:


Become a Member to join the conversation.