Skip to content

Python matplotlib Module: Detailed Overview and Examples

matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python. It is highly customizable and can be used to generate plots, histograms, power spectra, bar charts, error charts, scatter plots, and more.

Importing matplotlib

To use matplotlib, you typically import the pyplot submodule:

import matplotlib.pyplot as plt

Basic Plotting

Line Plot

Example

import matplotlib.pyplot as plt

# Data
x = [1, 2, 3, 4]
y = [10, 20, 25, 30]

# Plot
plt.plot(x, y)
plt.xlabel('x-axis')
plt.ylabel('y-axis')
plt.title('Line Plot')
plt.show()

Scatter Plot

Example

import matplotlib.pyplot as plt

# Data
x = [5, 7, 8, 7, 2, 17, 2, 9, 4, 11]
y = [99, 86, 87, 88, 100, 86, 103, 87, 94, 78]

# Plot
plt.scatter(x, y)
plt.xlabel('x-axis')
plt.ylabel('y-axis')
plt.title('Scatter Plot')
plt.show()

Bar Chart

Example

import matplotlib.pyplot as plt

# Data
x = ['A', 'B', 'C', 'D']
y = [3, 12, 5, 18]

# Plot
plt.bar(x, y)
plt.xlabel('Categories')
plt.ylabel('Values')
plt.title('Bar Chart')
plt.show()

Histogram

Example

import matplotlib.pyplot as plt

# Data
data = [1.5, 2.5, 2.1, 3.5, 3.7, 2.8, 3.2, 4.1, 3.9, 3.7]

# Plot
plt.hist(data, bins=5)
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.title('Histogram')
plt.show()

Pie Chart

Example

import matplotlib.pyplot as plt

# Data
labels = 'A', 'B', 'C', 'D'
sizes = [15, 30, 45, 10]

# Plot
plt.pie(sizes, labels=labels, autopct='%1.1f%%')
plt.title('Pie Chart')
plt.show()

Customizing Plots

Adding Legends

Example

import matplotlib.pyplot as plt

# Data
x = [1, 2, 3, 4]
y1 = [10, 20, 25, 30]
y2 = [15, 25, 20, 35]

# Plot
plt.plot(x, y1, label='Series 1')
plt.plot(x, y2, label='Series 2')
plt.xlabel('x-axis')
plt.ylabel('y-axis')
plt.title('Line Plot with Legend')
plt.legend()
plt.show()

Changing Line Styles and Colors

Example

import matplotlib.pyplot as plt

# Data
x = [1, 2, 3, 4]
y = [10, 20, 25, 30]

# Plot
plt.plot(x, y, linestyle='--', color='r', marker='o')
plt.xlabel('x-axis')
plt.ylabel('y-axis')
plt.title('Custom Line Plot')
plt.show()

Adding Grid

Example

import matplotlib.pyplot as plt

# Data
x = [1, 2, 3, 4]
y = [10, 20, 25, 30]

# Plot
plt.plot(x, y)
plt.xlabel('x-axis')
plt.ylabel('y-axis')
plt.title('Plot with Grid')
plt.grid(True)
plt.show()

Subplots

Example

import matplotlib.pyplot as plt

# Data
x = [1, 2, 3, 4]
y1 = [10, 20, 25, 30]
y2 = [15, 25, 20, 35]

# Plot
fig, (ax1, ax2) = plt.subplots(1, 2)

ax1.plot(x, y1)
ax1.set_title('Subplot 1')

ax2.plot(x, y2)
ax2.set_title('Subplot 2')

plt.show()

Advanced Plotting

3D Plotting

Example

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# Data
x = [1, 2, 3, 4]
y = [10, 20, 25, 30]
z = [5, 15, 20, 10]

# Plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot(x, y, z)
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.set_zlabel('Z-axis')
ax.set_title('3D Line Plot')
plt.show()

Contour Plots

Example

import matplotlib.pyplot as plt
import numpy as np

# Data
x = np.linspace(-5, 5, 50)
y = np.linspace(-5, 5, 50)
X, Y = np.meshgrid(x, y)
Z = np.sin(np.sqrt(X**2 + Y**2))

# Plot
plt.contour(X, Y, Z)
plt.title('Contour Plot')
plt.show()

Saving Plots

Save Plot to File

Example

import matplotlib.pyplot as plt

# Data
x = [1, 2, 3, 4]
y = [10, 20, 25, 30]

# Plot
plt.plot(x, y)
plt.xlabel('x-axis')
plt.ylabel('y-axis')
plt.title('Plot to Save')

# Save
plt.savefig('plot.png')
plt.show()

Practical Examples

Example 1: Visualizing a Sine Wave

import matplotlib.pyplot as plt
import numpy as np

# Data
x = np.linspace(0, 2 * np.pi, 100)
y = np.sin(x)

# Plot
plt.plot(x, y)
plt.xlabel('x-axis')
plt.ylabel('y-axis')
plt.title('Sine Wave')
plt.show()

Example 2: Comparing Multiple Series

import matplotlib.pyplot as plt

# Data
x = [1, 2, 3, 4]
y1 = [10, 20, 25, 30]
y2 = [15, 25, 20, 35]

# Plot
plt.plot(x, y1, label='Series 1')
plt.plot(x, y2, label='Series 2')
plt.xlabel('x-axis')
plt.ylabel('y-axis')
plt.title('Comparison of Two Series')
plt.legend()
plt.show()

Example 3: Creating a Heatmap

import matplotlib.pyplot as plt
import numpy as np

# Data
data = np.random.rand(10, 10)

# Plot
plt.imshow(data, cmap='hot', interpolation='nearest')
plt.title('Heatmap')
plt.colorbar()
plt.show()

Conclusion

The matplotlib module is a versatile and powerful library for creating a wide range of visualizations in Python. From simple line plots to complex 3D plots, matplotlib provides the tools needed to create informative and visually appealing graphics. By leveraging the customization options and advanced plotting capabilities, you can tailor your visualizations to effectively communicate your data and insights.