Data Visualization in Jupyter
# 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:
*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:
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:
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:
*(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:
8. Common Mistakes
-
Calling
plt.savefig()afterplt.show(): Whenplt.show()runs, it displays the image and then clears the canvas. If you putplt.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
What is the standard alias for importing Matplotlib's plotting module?
Which library is built on top of Matplotlib to provide beautiful, modern defaults and statistical graphs?
How do you instruct Jupyter to display a completed Matplotlib chart?
What did the magic command %matplotlib inline historically do?
To create a bar chart in Matplotlib, you use:
When saving a chart to an image file, why must plt.savefig() come BEFORE plt.show()?
Which chart type is best for finding correlations between two numerical variables (like Ad Spend vs Sales)?
What does the figsize=(12, 4) argument do when creating a figure?
Which Seaborn function easily creates a boxplot and integrates directly with a Pandas DataFrame?
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. Usematplotlib.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.