Python plotly Graph Using Jupyter Notebook (Example)

 

Hi! This tutorial will demonstrate how to use the Python plotly library inside Jupyter Notebook.

Here is an overview:

Let’s get right into it!

 

What is Jupyter Notebook?

Jupyter Notebook is an open-source web application that allows you to create and share documents that contain live code, equations, visualizations, and narrative text. It’s particularly popular among data scientists and researchers for its ability to combine code execution, text, mathematical equations, and visualizations all in one document.

The key features of Jupyter Notebook include:

  • Support for Multiple Programming Languages: Though it originated as a tool for Python, Jupyter now supports various programming languages including R, Julia, and Scala.
  • Interactive Computing: You can write and execute code in real-time within the notebook, allowing for interactive data exploration and analysis.
  • Rich Output Formats: Jupyter can display various types of output including HTML, images, videos, LaTeX, and more, making it suitable for generating reports, presentations, and academic papers.
  • Integration with Data Visualization Libraries: Jupyter works seamlessly with popular data visualization libraries like Matplotlib, seaborn, and plotly, enabling users to create compelling visualizations alongside their analysis.
  • Shareability: Notebooks can be easily shared with others, either as static documents or as interactive web applications, making collaboration and reproducibility easier.

 

Install & Launch Jupyter Notebook

To install and launch Jupyter Notebook, run the following lines of code in your terminal or command line:

# install Jupyter Notebook
pip install notebook
 
# launch Jupyter Notebook
jupyter notebook

After running jupyter notebook, it will start the local server and, if all goes well, you should see an interface that looks like this open up in your browser:

 

Jupyter Notebook startup

 
Click on New, then select Notebook. It will launch a new untitled Notebook where you will be prompted to select a Kernel. If not selected by default, select the Python 3 (ipykernel) option and click Select.

Sometimes, you may need to upgrade your version of pip before you can properly install Jupyter Notebook on your computer. If you are using a PC, open your command prompt or terminal as an administrator by right-clicking on it and selecting Run as administrator. Then upgrade pip by running python -m pip install --upgrade pip.

Next, install Jupyter Notebook by running python -m pip install jupyter, and then launch the Notebook with jupyter notebook. You should see the same interface as above open up in your browser.

Another option for installing and launching Jupyter Notebook is to use the Anaconda distribution of the Notebook. Anaconda is an open-source software, which distributes Jupyter Notebook, Spyder, RStudio, and other IDEs.

You will first need to download and install the Anaconda navigator. When that is done, you can then install Jupyter Notebook by clicking the Install button. Next, launch the Notebook by clicking the Launch button. You will now have the local Jupyter Notebook server running on your computer.
 

Install & Import plotly

With Jupyter Notebook installed and launched, we can now install and import plotly. To do so, run the lines of code below:

# install plotly
pip install plotly
 
# import plotly
import plotly.express as px

Now that we have installed and imported plotly into the Python programming environment of our Jupyter Notebook, we can now build interactive visualizations. First, we have to create the example dataset.
 

Create Example Dataset

We will use the tips dataset that comes preloaded in plotly as our example dataset. To load and preview the first 10 rows of the dataset, run the lines of code below:

df = px.data.tips()
 
print(type(df))
 
# <class 'pandas.core.frame.DataFrame'>
 
print(df.head(10))
 
#   total_bill   tip     sex  smoker day    time   size
#0       16.99  1.01  Female     No  Sun  Dinner     2
#1       10.34  1.66    Male     No  Sun  Dinner     3
#2       21.01  3.50    Male     No  Sun  Dinner     3
#3       23.68  3.31    Male     No  Sun  Dinner     2
#4       24.59  3.61  Female     No  Sun  Dinner     4
#5       25.29  4.71    Male     No  Sun  Dinner     4
#6        8.77  2.00    Male     No  Sun  Dinner     2
#7       26.88  3.12    Male     No  Sun  Dinner     4
#8       15.04  1.96    Male     No  Sun  Dinner     2
#9       14.78  3.23    Male     No  Sun  Dinner     2

With the example dataset loaded and previewed, we can now build visualizations.
 

Example: Build Interactive Box Plot

We will build a simple box plot visualizing the tipping habits of male and female diners on different days of the week.

Run the lines of code below to build the box plot:

import plotly.io as pio
 
pio.renderers.default = 'iframe'
 
fig = px.box(df, x = "sex", y = "tip", color = "day")
 
fig.show()

In the above example, we needed to first import plotly.io as pio and then set pio.renderers.default = "iframe" before we built the box plot. This is necessary because in Jupyter Notebook, plotly visualizations are not automatically displayed.

This seems to be a drawback for using plotly in Jupyter Notebook. Nevertheless, the above solution works well, and enables plotly graphs to be displayed inside the Notebook. You can find other similar recommendations here.

Having set the visualization to display inside the Notebook, we then used the px.box() method to build the box plot wherein we passed the DataFrame df, and defined the x-axis as the sex column; the y-axis as the tip column, and then grouped the box plots by day. Lastly, we displayed the plot using fig.show().

You can build and display other interactive plotly visualizations inside your Jupyter Notebook as well.
 

Video, Further Resources & Summary

Do you need more explanations on how to use Python plotly library inside Jupyter Notebook? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to use Python plotly library inside Jupyter Notebook.

 

The YouTube video will be added soon.

 

With that, we have demonstrated how to use Python plotly library inside Jupyter Notebook. Furthermore, you could have a look at some of the other interesting plotly in Python tutorials on Statistics Globe, starting with these:

This post has shown how to use Python plotly library inside Jupyter Notebook. I hope you found it helpful! In case you have further questions, you may leave a comment below.

 

R & Python Expert Ifeanyi Idiaye

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top