Skip to main content
Jupyter Notebooks
CHAPTER 12 Beginner

Data Visualization in Jupyter

Updated: May 18, 2026
5 min read

# CHAPTER 12

Data Visualization in Jupyter

1. Chapter Introduction

Numbers in a DataFrame are hard to interpret. Humans process visual information much faster than raw text. One of the greatest features of Jupyter Notebooks is Inline Visualization—the ability to generate a chart and have it appear directly beneath the code cell. This chapter covers the two most popular Python visualization libraries: Matplotlib and Seaborn.

2. Matplotlib: The Foundation

Matplotlib is the original Python visualization library. It is incredibly powerful and customizable, though the syntax can be a bit verbose.

Cell 1:

python
1234567891011121314151617
import matplotlib.pyplot as plt
import numpy as np

# Data
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May']
sales = [150, 200, 180, 250, 300]

# 1. Create the plot
plt.plot(months, sales)

# 2. Customize the plot
plt.title("Monthly Sales Growth")
plt.xlabel("Month")
plt.ylabel("Sales in USD")

# 3. Show the plot inline
plt.show()

*When you run this cell, a line chart will appear instantly below it.*

3. Creating Different Chart Types

Matplotlib can create almost any standard chart type.

Cell 2:

python
123456789101112
# Bar Chart
plt.bar(months, sales, color='skyblue')
plt.title("Bar Chart Example")
plt.show()

# Scatter Plot (great for finding correlations)
ad_spend = [50, 70, 60, 90, 110]
plt.scatter(ad_spend, sales, color='red', marker='x')
plt.title("Ad Spend vs Sales")
plt.xlabel("Ad Spend ($)")
plt.ylabel("Sales ($)")
plt.show()

4. Seaborn: Beautiful Defaults

While Matplotlib is great, it can look a bit outdated by default. Seaborn is a library built *on top* of Matplotlib. It creates gorgeous, modern, statistical graphics with much less code. It also integrates perfectly with Pandas DataFrames.

Cell 3:

python
1234567891011121314
import seaborn as sns
import pandas as pd

# Create a sample DataFrame
data = pd.DataFrame({
    'Category': ['A', 'A', 'B', 'B', 'C', 'C'],
    'Values': [10, 15, 20, 25, 30, 35]
})

# Seaborn automatically makes it look modern
# We pass the DataFrame directly to the 'data' parameter
sns.boxplot(x='Category', y='Values', data=data, palette='Set2')
plt.title("Seaborn Boxplot")
plt.show()

5. Jupyter Magic: %matplotlib inline

In older versions of Jupyter, if you created a chart, it might open in a new pop-up window rather than inside the notebook.

To force charts to appear directly in the notebook (inline), data scientists traditionally placed a Magic Command at the top of their notebook: %matplotlib inline

*Note: Modern Jupyter Notebooks and JupyterLab usually do this automatically, but you will see this line in millions of older notebooks on GitHub. It's important to know what it does.*

6. Saving Visualizations

Often, you need to put the chart you generated into a PowerPoint presentation or a Word document. You can save charts directly to your hard drive using code.

Cell 4:

python
12345678
plt.plot(months, sales)
plt.title("Final Report Chart")

# Save the figure BEFORE calling plt.show()
# It saves to the same folder as your notebook
plt.savefig('sales_report_chart.png', dpi=300, bbox_inches='tight')

plt.show()

*(Check your Jupyter file browser, you will now see salesreportchart.png!)*

7. Mini Project: Sales Analytics Dashboard

Combine multiple plots into a single visual dashboard using Matplotlib's subplots feature.

Cell 5:

python
12345678910111213141516
# Create a figure with 1 row and 2 columns
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 4))

# First subplot (Line chart)
ax1.plot(months, sales, marker='o', color='blue')
ax1.set_title("Revenue Trend")
ax1.set_ylabel("$ Revenue")

# Second subplot (Bar chart)
ax2.bar(months, ad_spend, color='orange')
ax2.set_title("Marketing Spend")
ax2.set_ylabel("$ Spend")

# Adjust spacing and display
plt.tight_layout()
plt.show()

8. Common Mistakes

  • Calling plt.savefig() after plt.show(): When plt.show() runs, it displays the image and then clears the canvas. If you put plt.savefig() after it, it will save a blank white square. Always save *before* you show.
  • Not setting labels: A chart without a title, X-axis label, and Y-axis label is useless to a stakeholder. Always label your axes.

9. MCQs

Question 1

What is the standard alias for importing Matplotlib's plotting module?

Question 2

Which library is built on top of Matplotlib to provide beautiful, modern defaults and statistical graphs?

Question 3

How do you instruct Jupyter to display a completed Matplotlib chart?

Question 4

What did the magic command %matplotlib inline historically do?

Question 5

To create a bar chart in Matplotlib, you use:

Question 6

When saving a chart to an image file, why must plt.savefig() come BEFORE plt.show()?

Question 7

Which chart type is best for finding correlations between two numerical variables (like Ad Spend vs Sales)?

Question 8

What does the figsize=(12, 4) argument do when creating a figure?

Question 9

Which Seaborn function easily creates a boxplot and integrates directly with a Pandas DataFrame?

Question 10

How do you add a title to a Matplotlib chart?

10. Interview Questions

  • Q: Explain the relationship between Matplotlib and Seaborn. Why would you use one over the other?
  • Q: If you want to place two charts side-by-side in a single image in Jupyter, how do you approach it architecturally in Matplotlib?

11. Summary

Jupyter Notebooks are powerful because they mix code, text, and visual charts in one document. Use matplotlib.pyplot for foundational charting (lines, bars, scatters). Use seaborn for modern aesthetics and complex statistical plots (like boxplots) directly from Pandas DataFrames. Remember to label your axes and always call plt.savefig() before plt.show() if you want to export your work.

12. Next Chapter Recommendation

In Chapter 13: Interactive Widgets in Jupyter, we take visualization to the next level by adding interactive sliders, dropdowns, and buttons that allow users to play with data in real-time.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·