Hello! Welcome to the third lesson in our "Introduction to Data Science" course.
In the previous lessons, we built a solid foundation for data manipulation. We started with NumPy for efficient numerical operations and then moved to Pandas, where you learned to load, clean, and transform tabular data into a pristine state. This process is essential, but data tables, no matter how clean, often hide the very patterns and insights we seek.
This lesson bridges that gap. We will explore Matplotlib, the cornerstone of data visualization in Python. Our goal is to translate your clean data into clear, informative visuals. You will learn to create the three most fundamental static plots:
- Line charts to track trends over time.
- Scatter plots to investigate relationships between variables.
- Histograms to understand the distribution of your data.
Mastering visualization is like learning to speak the language of data. It's the primary way we perform Exploratory Data Analysis (EDA), build intuition about our datasets, and communicate findings to others.
1. The Anatomy of a Matplotlib Plot
Before we start plotting, it's crucial to understand how Matplotlib structures its visuals. There are two primary ways to use Matplotlib: a state-based interface (often called the MATLAB-style) and a more powerful object-oriented (OO) interface. While the former is quick for simple plots, the OO interface gives you much greater control, which is essential for creating complex, publication-quality figures. We will focus on the OO approach as it is more robust and scalable.
Visualization with Matplotlib | Python Data Science Handbook
The 'Python Data Science Handbook' provides an excellent explanation of these two interfaces and the basic structure of a Matplotlib plot. Understanding this distinction is key to writing effective plotting code.
Please read the section titled 'Two Interfaces for the Price of One'. Pay close attention to the code examples for the 'MATLAB-style Interface' and the 'Object-oriented interface' to see how they differ. We will primarily be using the object-oriented style, which typically starts with a line like fig, ax = plt.subplots().
In the object-oriented approach, you explicitly create Figure and Axes objects.
- The
Figureis the outermost container for all the elements that make up your plot. You can think of it as the entire canvas. - The
Axes(note the 'e', it's not the plural of 'Axis') is the actual plot itself—the area where the data is plotted with x- and y-axes, ticks, and labels. A single figure can contain multipleAxesobjects (subplots).
This structure allows you to precisely control every element of your visual.
2. Line Plots: Visualizing Trends
A line plot is ideal for showing how a continuous variable changes over a sequence, most commonly time.
For a comprehensive guide on creating and customizing plots, the following video is tailored for an audience with a background in science and engineering. It demonstrates a professional workflow for building plots from the ground up.
Matplotlib Tutorial: For Physicists, Engineers, and Mathematicians
This tutorial from 'Mr. P Solver' focuses on creating professional plots. We'll start with the section on line plots, which covers everything from basic plotting to advanced customization.
Watch the video from the beginning to 09:11. Focus on: The basic plot(x, y) command. Customizing the line's appearance: color, linestyle, linewidth, and marker. Setting the figure size with plt.figure(figsize=...). Adding essential labels (xlabel, ylabel) and a legend. Plotting multiple lines on the same axes to compare data, such as experimental data versus a theoretical curve.
As you saw, a few lines of code can produce a detailed and informative plot. The key is to build it up layer by layer: plot the data, then add labels, a title, a legend, and customize styles until it clearly communicates your message.
3. Histograms: Understanding Distributions
While line plots show trends, histograms reveal the underlying distribution of a single variable. They group data into "bins" and plot the frequency of data points falling into each bin, giving you a sense of the data's shape, central tendency, and spread.
Matplotlib Tutorial (Part 6): Histograms
Corey Schafer's tutorial on histograms provides a clear, step-by-step introduction to their purpose and creation.
Watch this video from the beginning to 13:38. Pay close attention to: The core concept of a histogram and how it differs from a bar chart. The use of the bins parameter to control the grouping of data. Plotting a histogram from real-world data loaded with Pandas. Using a logarithmic scale (log=True) to better visualize skewed data. Adding a vertical line (axvline) to mark a key statistic like the median.
Histograms are a cornerstone of EDA. Being able to quickly plot one for a variable of interest is a critical skill. For comparing the distributions of two or more variables, a 'step' histogram is often more effective than overlapping bars.
Matplotlib Tutorial: For Physicists, Engineers, and Mathematicians
Let's return to the 'Mr. P Solver' video to see a more advanced way of plotting histograms that is excellent for comparison.
Watch the segment from 09:11 to 12:31. Note the use of histtype='step'. This is a very useful technique when you need to overlay multiple histograms without them obscuring each other.
4. Scatter Plots: Exploring Relationships
Scatter plots are the go-to tool for visualizing the relationship between two numerical variables. Each point on the plot represents a single observation (a row in your DataFrame), positioned according to its values on the x and y axes.
Creating a basic scatter plot is straightforward. The PYnative exercises provide a concise example of plotting toothpaste sales against month number.
Python Matplotlib Exercise 4: Scatter Plot
This exercise demonstrates how to create a scatter plot with a grid to visualize the relationship between two variables from a sales dataset.
Review the code provided in 'Exercise 4'. Notice the use of plt.scatter() to create the plot and plt.grid() to add a grid, which can help in reading the values.
In a scatter plot, you look for patterns:
- A tight cluster of points sloping upwards suggests a positive correlation.
- A cluster sloping downwards suggests a negative correlation.
- A random cloud of points suggests no correlation.
5. Putting It All Together: A Professional Workflow
Knowing how to create individual plots is good, but combining them into a single, coherent figure is even better. This is where the object-oriented API truly shines. The following demonstration shows how to build a multi-panel figure that effectively compares data from two different trials. This workflow is an excellent template for your own analysis projects.
Matplotlib Tutorial: For Physicists, Engineers, and Mathematicians
We will now watch a detailed walkthrough of creating a professional, multi-panel plot. This example integrates many of the concepts we've discussed and showcases the power of the object-oriented API.
Watch carefully from 18:32 to 27:05. This is the most important segment of the lesson. Observe how the presenter: Initializes a figure with two Axes objects: fig, axes = plt.subplots(1, 2, ...). Works on each Axes object (axes[0], axes[1]) sequentially to build the plot. Adds text annotations with LaTeX formatting and a bounding box for clarity. Customizes the legend and removes redundant labels to create a clean, professional look. Uses fig.text() to add a single, shared x-label for the entire figure.
Self-Test and Practice
Now it's your turn to apply these concepts. Using the skills you've just learned, complete the following exercise. You will need the sales_data.csv file, which you can find and download by following the links in the PYnative exercises resource.
Task: Create a figure that contains two subplots arranged vertically.
-
Top Subplot (Line Chart):
- Plot the sales data for 'facecream' and 'facewash' over all months.
- Each product should have its own line with a distinct marker style.
- Add a title "Face Cream vs. Face Wash Sales".
- Include a legend to identify the two product lines.
- Label the Y-axis as "Sales units in number".
-
Bottom Subplot (Histogram):
- Plot a histogram of the 'bathingsoap' sales data.
- Experiment with the number of
bins(e.g., try 10) to get a clear distribution. - Add a title "Bathing Soap Sales Distribution".
- Label the X-axis as "Sales units in number" and the Y-axis as "Frequency".
Bonus: Use fig.tight_layout() at the end of your script to automatically adjust subplot params so that subplots are nicely fit in the figure.
This exercise will test your ability to use the object-oriented API to create and customize multiple plot types within a single figure.
Conclusion
In this lesson, you've completed your foundational Python toolkit for data science. You can now not only process and clean data with NumPy and Pandas but also visually explore it with Matplotlib.
Key Takeaways:
- Matplotlib's object-oriented interface (
fig, ax = plt.subplots()) provides a powerful and flexible way to create complex figures. - Line plots (
ax.plot()) are for tracking continuous data over a sequence. - Scatter plots (
ax.scatter()) are for investigating the relationship between two numeric variables. - Histograms (
ax.hist()) are for understanding the distribution of a single numeric variable. - Effective visualization requires careful customization, including labels, titles, legends, and colors, to ensure your plots are clear and interpretable.
You are now fully equipped for the first major milestone of this course. In the next lesson, we will begin our first project, where you will integrate everything you've learned—NumPy, Pandas, and Matplotlib—to conduct a complete exploratory data analysis on a real-world dataset from start to finish.