11.3 Matplotlib & Seaborn: Data Visualization
Introduction to Matplotlib and Seaborn, the most popular libraries for creating plots and charts.
Data visualization is crucial for understanding trends, patterns, and relationships in data. The Matplotlib and Seaborn libraries are the most popular choices in Python for creating plots and charts.
Matplotlib:
It is the fundamental library for creating static, animated, and interactive visualizations in Python. It provides a wide range of plot types (line, bar, pie, histograms, etc.) and offers absolute, detailed control over every element of the plot (titles, labels, colors, line styles, etc.). It is the foundation for many other visualization libraries.
Seaborn:
It is a data visualization library based on Matplotlib. It provides a higher-level interface for creating attractive and informative statistical graphics. Seaborn simplifies the creation of complex plots like heatmaps, violin plots, and pair plots, and comes with more attractive default color palettes.
Tip!
The following code requires the packages to be installed ('pip install matplotlib seaborn numpy pandas') and will not run completely in the live editor.
# import matplotlib.pyplot as plt
# import seaborn as sns
# import numpy as np
# import pandas as pd
# # Example 1: Simple line plot with Matplotlib
# x = np.array([1, 2, 3, 4, 5])
# y = np.array([2, 4, 1, 5, 7])
# plt.plot(x, y)
# plt.xlabel("X-axis")
# plt.ylabel("Y-axis")
# plt.title("Simple Line Plot")
# plt.show() # Displays the plot (in a local environment)
# print("Matplotlib: Creating a line plot.")
# # Example 2: Scatter plot with Seaborn
# np.random.seed(0)
# data_scatter = pd.DataFrame({
# 'Size': np.random.rand(50) * 100,
# 'Price': np.random.rand(50) * 500 + 100
# })
# sns.scatterplot(x='Size', y='Price', data=data_scatter)
# plt.title("Scatter Plot: Size vs. Price")
# plt.show()
print("The Matplotlib/Seaborn code is commented out. To run it, install the libraries locally ('pip install matplotlib seaborn numpy pandas').")
print("These libraries are used for creating plots and visualizing data.")
Explore More with AI
Use AI to generate new examples, delve deeper into theory, or get your questions answered.