Language: Python
Data Science
Matplotlib was originally created by John D. Hunter in 2003 to provide a MATLAB-like plotting interface for Python. It has become the foundation for many Python visualization libraries, including Seaborn and Pandas plotting utilities, and remains widely used in data analysis, scientific research, and machine learning.
Matplotlib is a comprehensive 2D plotting library for Python. It allows the creation of high-quality graphs, charts, and figures in various formats, suitable for publication and interactive visualization.
pip install matplotlibconda install matplotlibMatplotlib allows you to create line plots, scatter plots, bar charts, histograms, 3D plots, and more. The library provides both an object-oriented interface and a state-based interface (pyplot) to build plots programmatically or interactively.
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [2, 3, 5])
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Simple Line Plot')
plt.show()Creates a basic line plot with labeled axes and a title.
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [10, 20, 25, 30]
plt.scatter(x, y, color='red', marker='x')
plt.show()Generates a scatter plot with custom color and marker type.
import matplotlib.pyplot as plt
fig, axs = plt.subplots(2, 1)
axs[0].plot([1,2,3],[1,4,9])
axs[1].bar([1,2,3],[5,2,7])
plt.show()Shows multiple plots in a single figure using subplots.
import matplotlib.pyplot as plt
plt.style.use('ggplot')
plt.plot([1,2,3],[1,4,9])
plt.show()Applies a built-in style to enhance visual aesthetics.
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = np.linspace(0,5,100)
y = np.sin(x)
z = np.cos(x)
ax.plot(x,y,z)
plt.show()Demonstrates creating a 3D line plot.
import matplotlib.pyplot as plt
data = [1,1,2,3,3,3,4,4,5]
plt.hist(data, bins=5, color='purple', alpha=0.7)
plt.show()Plots a histogram with specified number of bins and styling.
Use the object-oriented interface for complex plots for more control.
Label axes and add titles to improve readability.
Use grid lines and legends where appropriate.
Combine Matplotlib with NumPy for efficient data plotting.
Save figures using `plt.savefig()` for reproducibility.