Python ModulesMatplotlib TutorialIntegration with NumPy, Pandas, and Seaborn

Real-World Applications and Compatibility in Matplotlib

Matplotlib’s compatibility with popular Python libraries like NumPy, Pandas, and Seaborn makes it a versatile tool for data visualization. In this section, we’ll explore how these integrations enhance data analysis workflows.


Combining Matplotlib with NumPy

NumPy provides efficient numerical operations and data generation, which complement Matplotlib’s plotting capabilities.

Example: Plotting Numerical Data

import matplotlib.pyplot as plt
import numpy as np
 
# Generate data
x = np.linspace(0, 10, 100)
y = np.sin(x)
 
# Plot data
plt.plot(x, y, label="Sine Wave")
 
# Add labels and title
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.title("Sine Function")
plt.legend()
 
# Display the plot
plt.show()

Combining Matplotlib with Pandas

Pandas integrates seamlessly with Matplotlib, enabling direct plotting from DataFrames and Series.

Example: Visualizing DataFrames

import matplotlib.pyplot as plt
import pandas as pd
 
# Create a sample DataFrame
data = {
    "Month": ["Jan", "Feb", "Mar", "Apr"],
    "Sales": [200, 250, 300, 350]
}
df = pd.DataFrame(data)
 
# Plot the data
plt.plot(df["Month"], df["Sales"], marker='o', label="Monthly Sales")
 
# Add labels and title
plt.xlabel("Month")
plt.ylabel("Sales")
plt.title("Monthly Sales Trend")
plt.legend()
 
# Display the plot
plt.show()

Combining Matplotlib with Seaborn

Seaborn builds on Matplotlib to enhance the aesthetics and simplify complex visualizations.

Example: Enhancing Aesthetics

import matplotlib.pyplot as plt
import seaborn as sns
 
# Generate sample data
sns.set_theme(style="whitegrid")
data = sns.load_dataset("tips")
 
# Create a boxplot
sns.boxplot(x="day", y="total_bill", data=data)
 
# Add title
plt.title("Total Bill Distribution by Day")
 
# Display the plot
plt.show()

Real-World Case Studies in Data Visualization

Case Study 1: Stock Price Analysis

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
 
# Simulated stock prices
dates = pd.date_range("2023-01-01", periods=100)
prices = np.cumsum(np.random.normal(loc=0, scale=1, size=100)) + 100
 
# Create a DataFrame
stock_data = pd.DataFrame({"Date": dates, "Price": prices})
 
# Plot the stock prices
plt.plot(stock_data["Date"], stock_data["Price"], label="Stock Price")
 
# Add labels and title
plt.xlabel("Date")
plt.ylabel("Price")
plt.title("Simulated Stock Price Over Time")
plt.legend()
 
# Display the plot
plt.show()

Case Study 2: Correlation Heatmap for Sales Data

import seaborn as sns
import matplotlib.pyplot as plt
 
# Sample sales data
data = {
    "Product A": [50, 60, 70, 80],
    "Product B": [30, 40, 50, 60],
    "Product C": [20, 30, 40, 50]
}
df = pd.DataFrame(data)
 
# Compute correlation matrix
corr = df.corr()
 
# Plot heatmap
sns.heatmap(corr, annot=True, cmap="coolwarm")
 
# Add title
plt.title("Correlation Heatmap")
 
# Display the plot
plt.show()

Combining Matplotlib with NumPy, Pandas, and Seaborn enhances data visualization and analysis, making it an indispensable tool for real-world applications.