Generating Your First Figure to a Jupyter Notebook
This video shows how to generate a Bokeh visualization inline within a Jupyter notebook. To follow along with the example, you will need Jupyter installed.
More information about output_notebook()
can be found in the Bokeh official docs.
# Bokeh Libraries
from bokeh.io import output_notebook
from bokeh.plotting import figure, show
# The figure will be right in my Jupyter Notebook
output_notebook()
# Set up a generic figure() object
fig = figure()
# See what it looks like
show(fig)
00:01 How would this work in a Jupyter Notebook? For my virtual environment, I’m going to launch Jupyter Notebook.
00:17
The next thing that you need to do is create a new file, and then click on Untitled to rename the Notebook. For this one, I’ll give it the name FirstFigureNotebook
. The code is actually really, really similar. In fact, to save some time from your original script copy and then enter into the first cell and paste. What’s going to be different?
00:44
Well, you’re going to import bokeh.io
, but instead of output_file
, that was that change to output_notebook
. Here,
00:56
you don’t need to name a particular file. You’re going to call output_notebook()
. The rest pretty much looks the same. So again, from bokeh.io import output_notebook
. From bokeh.plotting
, same imports. The figure will be rendered right in my Jupyter Notebook by calling this method, and then you’re setting up your figure and you’re showing the figure. Okay, it looks like you’ve set everything up. Now run this cell by pressing Shift + Enter.
01:24 You’ll see that Bokeh gets loaded, and this very simple—well, in this case, empty—figure gets created. This could be useful, depending on how you want to work, showing all your work inline.
01:38
A lot of data scientists like to show their work as they’re going along and showing multiple figures. That’s possible here. For this tutorial to explain what’s going on inside of Bokeh, I’m going to continue using Visual Studio Code, but this gives you an example, by using output_notebook()
instead of output_file()
, of how you can continue forward. Next up, let’s make something a little more interesting.
R morel on May 20, 2020
come = ‘23’ Ncome = come + 23 print(new_come)
CSW on Jan. 27, 2021
I did exactly and bumm does not work, luv that. Any ideas?
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-3-fcf258cf19f6> in <module>
3
4
----> 5 output_notebook()
6
7 fig = figure()
NameError: name 'output_notebook' is not defined
Chris Bailey RP Team on Jan. 27, 2021
Hi CSW, I have followed along and I’m not seeing that error when I run this in a Jupyter Notebook. It does say there are no renderers which is true, its an empty figure.
The error you are showing has several additional numbers in it that seem odd. They sort of look like line numbers. Were you able to get the empty figure to appear in the previous lesson?
Bartosz Zaczyński RP Team on Jan. 28, 2021
@CSW Did you remember to change the import statement at the top of your notebook cell?
from bokeh.io import output_notebook
Become a Member to join the conversation.
john09 on June 19, 2019
Great tutorial. I can run Python scripts directly, no problem. Unfortunately, I can’t get the Jupyter notebook to work. I am using Pycharm and started Jupyter notebook as you suggested from the PyCharm terminal. In the web page, the Python syntax is not highlighted and when I try to run, I just get another “In[ ]” box. I don’t know anything about Jupyter notebook. I do see “Connecting to Kernel” in yellow box. I’m guessing that this webpage needs to connect to some kind of Python server. (I’ve got 40 years of coding experience in many languages but am very new to Python).