Matplotlib

Matplotlib Tutorial – Chapter 15: Plotting with Pandas and NumPy

Chapter 15 – Plotting with Pandas and NumPy

In previous chapters, we learned how to create plots directly using Matplotlib’s pyplot and Object-Oriented interfaces.
In this chapter, we’ll explore how Matplotlib integrates seamlessly with Pandas and NumPy — the two fundamental Python libraries for data manipulation and numerical computation.

Combining these libraries allows you to visualize structured data directly, making analysis faster and more efficient.

✅ Why Integrate Pandas and NumPy with Matplotlib?

  • Direct plotting: Pandas DataFrames and Series have built-in .plot() methods that use Matplotlib under the hood.
  • Cleaner code: No need to manually unpack arrays or write repetitive plotting commands.
  • Efficient workflows: Easily move from data preparation to visualization in just a few lines of code.

✅ 1. Using Matplotlib with NumPy

NumPy provides powerful array operations, and Matplotlib works perfectly with NumPy arrays.


import numpy as np
import matplotlib.pyplot as plt

# Create data using NumPy
x = np.linspace(0, 10, 100)
y = np.sin(x)
z = np.cos(x)

# Plot with Matplotlib
plt.plot(x, y, label="Sine", color="blue")
plt.plot(x, z, label="Cosine", color="orange")
plt.title("Sine and Cosine Functions using NumPy")
plt.xlabel("X Axis")
plt.ylabel("Y Axis")
plt.legend()
plt.grid(True)
plt.show()

Here, NumPy generates a sequence of evenly spaced numbers with linspace(), and the functions sin() and cos() compute the values efficiently.

✅ 2. Creating Subplots for Multiple NumPy Arrays

When dealing with several mathematical relationships, subplots make comparison easier.


fig, axs = plt.subplots(2, 1, figsize=(7, 6))

x = np.linspace(0, 2 * np.pi, 100)
axs[0].plot(x, np.sin(x), color='red', label='Sine')
axs[0].legend()
axs[0].set_title("Sine Function")

axs[1].plot(x, np.cos(x), color='green', label='Cosine')
axs[1].legend()
axs[1].set_title("Cosine Function")

plt.tight_layout()
plt.show()

Each subplot gets its own axis, allowing independent customization and labeling.

✅ 3. Introduction to Pandas Plotting

Pandas simplifies plotting even further. Its .plot() method automatically uses Matplotlib to visualize Series and DataFrames.


import pandas as pd
import numpy as np

# Create a DataFrame
data = {
    'Month': ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'],
    'Sales': [200, 250, 300, 280, 320, 400],
    'Profit': [50, 70, 90, 80, 100, 130]
}
df = pd.DataFrame(data)

# Simple line plot
df.plot(x='Month', y='Sales', kind='line', marker='o', color='teal', title='Monthly Sales Trend')
plt.show()

Pandas automatically handles the figure creation, axis labeling, and legends.

✅ 4. Multiple Columns in a Single Plot

You can visualize multiple columns together for comparison.


df.plot(x='Month', y=['Sales', 'Profit'], kind='line', marker='o')
plt.title("Sales vs Profit")
plt.ylabel("Amount")
plt.grid(True)
plt.show()

This helps in identifying correlations or differences between related variables.

✅ 5. Common Plot Types with Pandas

Pandas supports multiple plot types directly:

  • Line plot: df.plot(kind='line')
  • Bar chart: df.plot(kind='bar')
  • Histogram: df.plot(kind='hist')
  • Pie chart: df.plot(kind='pie')
  • Box plot: df.plot(kind='box')
  • Scatter plot: df.plot(kind='scatter', x='Sales', y='Profit')

Example:


df.plot(kind='bar', x='Month', y='Sales', color='orange', legend=False)
plt.title("Monthly Sales Bar Chart")
plt.ylabel("Sales")
plt.show()

✅ 6. Plotting Data from NumPy Arrays in Pandas

You can easily integrate NumPy computations into Pandas plots.


x = np.linspace(0, 2 * np.pi, 100)
df = pd.DataFrame({
    'x': x,
    'sin': np.sin(x),
    'cos': np.cos(x)
})

df.plot(x='x', y=['sin', 'cos'], kind='line', title='Trigonometric Functions')
plt.show()

This combination of NumPy and Pandas gives you powerful flexibility — numeric efficiency plus visual clarity.

✅ 7. Using DataFrame Styles and Aesthetics

Pandas plots can be styled just like standard Matplotlib plots.


plt.style.use('seaborn-v0_8-darkgrid')

df.plot(x='Month', y=['Sales', 'Profit'], kind='bar', figsize=(8,5))
plt.title("Sales and Profit Comparison", fontsize=14, color='darkred')
plt.xlabel("Month")
plt.ylabel("Amount")
plt.legend(title="Metrics")
plt.tight_layout()
plt.show()

✅ 8. Customizing Axes and Legends

You can modify Pandas plots using Matplotlib’s plt functions or ax object.


ax = df.plot(x='Month', y=['Sales', 'Profit'], kind='bar', color=['#007bff', '#28a745'])
ax.set_title("Monthly Sales and Profit Overview", fontsize=14)
ax.set_ylabel("Value")
ax.legend(["Sales", "Profit"])
plt.show()

✅ 9. Combining Matplotlib and Pandas for Advanced Visualization

You can use both together for highly customized charts.


fig, ax = plt.subplots(figsize=(8,5))
df.plot(x='Month', y='Sales', kind='bar', ax=ax, color='skyblue')
ax.plot(df['Month'], df['Profit'], color='darkred', marker='o', linewidth=2)
ax.set_title("Sales and Profit (Combined View)")
ax.set_ylabel("Amount")
plt.show()

Here, we plotted a bar chart (Sales) and overlaid a line chart (Profit) — combining Matplotlib’s power with Pandas simplicity.

✅ 10. Best Practices

  • Use Pandas plots for quick analysis, and Matplotlib for customization.
  • Always label your axes, titles, and legends clearly.
  • Use consistent color schemes across plots.
  • When handling large datasets, use NumPy for performance efficiency.
  • Combine multiple visualizations to extract deeper insights.

✅ Summary

In this chapter, we explored how to use Matplotlib effectively with Pandas and NumPy.
You learned how to:

  • Create line, bar, and scatter plots directly from DataFrames.
  • Integrate numerical computations from NumPy into visualizations.
  • Customize and combine plots for advanced analysis.

This integration forms the backbone of modern data analysis workflows — fast, clean, and powerful.

In the next chapter, we’ll move to Saving and Exporting Plots — learning how to export your visualizations in high quality for reports, presentations, or publications.

Leave a Reply

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