Top 10 advantages of the Matplotlib library in Python

Matplotlib is a powerful tool in Python used for creating attractive graphs and charts. It offers a variety of plot types like line, scatter, bar, and 3D plots to suit different needs. What users like most is its flexibility, allowing them to adjust the look of their plots with colors, styles, and labels. With Matplotlib, data visualization becomes accessible and engaging.

In this Answer, we will explore ten reasons why Matplotlib is highly beneficial and widely used in data visualization.

Advantages of Matplotlib

Notable advantages of Matplotlib are illustrated below.

Advantages of Matplotlib
Advantages of Matplotlib

Let us discuss all these individually.

Wide range of plots

We can create a line plot, a scatter plot, a bar plot, a histogram, a 3D plot, and a polar plot through the matplotlib library. In the code below, we use plt.tight_layout() to adjust the layout of subplots to avoid overlapping. These plots help us visualize data in two-dimension and three-dimension as well.

import matplotlib.pyplot as plt
import numpy as np
# Create a 2x3 grid of subplots
fig, axes = plt.subplots(nrows=2, ncols=3, figsize=(15, 10))
# Line plot
x = np.linspace(0, 10, 100)
y = np.sin(x)
axes[0, 0].plot(x, y)
axes[0, 0].set_title('Line Plot')
# Scatter plot
x = np.random.rand(50)
y = np.random.rand(50)
axes[0, 1].scatter(x, y)
axes[0, 1].set_title('Scatter Plot')
# Bar plot
x = ['A', 'B', 'C', 'D']
y = [3, 7, 2, 9]
axes[0, 2].bar(x, y)
axes[0, 2].set_title('Bar Plot')
# Histogram
data = np.random.randn(1000)
axes[1, 0].hist(data, bins=30)
axes[1, 0].set_title('Histogram')
# 3D plot
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))
axes[1, 1] = fig.add_subplot(2, 3, 5, projection='3d') # Use projection='3d'
axes[1, 1].plot_surface(X, Y, Z, cmap='viridis')
axes[1, 1].set_title('3D Plot')
# Polar plot
theta = np.linspace(0, 2 * np.pi, 100)
r = np.sin(3 * theta)
axes[1, 2].plot(theta, r) # No need for projection='polar'
axes[1, 2].set_title('Polar Plot')
# Adjust layout and display the grid of plots
plt.tight_layout()
plt.savefig("./output/plot.png")
plt.show()

Publication-quality plot

Using Matplotlib, we can create a publication-quality plot. The code creates sample data for the x-axis and calculates corresponding y-axis values using the sine function. We can customize a plot with a blue line and solid line style.

import matplotlib.pyplot as plt
import numpy as np
# Sample data
x = np.linspace(0, 10, 100)
y = np.sin(x)
# Customization publication-quality plot
plt.plot(x, y, label='Data', color='b', linestyle='-', linewidth=2)
plt.xlabel('X-axis', fontsize=12)
plt.ylabel('Y-axis', fontsize=12)
plt.title('Publication-Quality Plot', fontsize=14)
plt.legend(fontsize=12)
plt.tick_params(axis='both', labelsize=10)
plt.grid(True, linestyle='--', alpha=0.7)
plt.tight_layout()
plt.savefig('./output/publication_quality_plot.png', dpi=300)
plt.show()

Integration with NumPy and Pandas

We can integrate NumPy and Pandas, allowing users to create plots directly from NumPy arrays or Pandas DataFrames. If users are working with different libraries, they will prefer using Matplotlib for feasibility. In the code below, we create the data through NumPy and introduce Pandas DataFrame. From this, we plotted the graph.

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
# Sample NumPy data
x = np.linspace(0, 10, 100)
y = np.sin(x)
# Plotting directly from NumPy arrays
plt.plot(x, y)
plt.title('Plotting with NumPy Arrays')
plt.show()
# Sample Pandas data
data = pd.DataFrame({'X': x, 'Y': y})
# Plotting directly from Pandas DataFrame
plt.plot(data['X'], data['Y'])
plt.title('Plotting with Pandas DataFrame')
plt.savefig("./output/plot.png")
plt.show()

Support for LaTeX and math text

Matplotlib supports LaTeX and math text, allowing users to display mathematical equations and symbols in various aspects of their plots, such as axis labels, titles, and annotations. This feature is particularly helpful when dealing with scientific or mathematical data visualizations.

import matplotlib.pyplot as plt
# Sample data
x = [0, 1, 2, 3, 4]
y = [0, 1, 4, 9, 16]
# Plotting with LaTeX-style mathematical expressions
plt.plot(x, y)
plt.xlabel(r'$x$', fontsize=14)
plt.ylabel(r'$y = x^2$', fontsize=14)
plt.title('Plot with LaTeX Math Text')
plt.savefig("./output/plot.png")
plt.show()

Customization options

Matplotlib offers extensive customization options, empowering users to tailor their visualizations to specific requirements. In the code below, we are customizing the plot with linestyle= ' - '.

import matplotlib.pyplot as plt
import numpy as np
# Sample data
x = np.linspace(0, 10, 100)
y = np.sin(x)
# Customizing plot appearance
plt.plot(x, y, marker='o', linestyle='-', color='b', linewidth=2, label='Data')
plt.xlabel('X-axis', fontsize=12)
plt.ylabel('Y-axis', fontsize=12)
plt.title('Customization Example', fontsize=14)
plt.legend(fontsize=12)
plt.grid(True, linestyle='--', alpha=0.7)
plt.xlim(0, 10)
plt.ylim(-1.5, 1.5)
plt.xticks(np.arange(0, 11, 2))
plt.yticks(np.arange(-1, 2, 0.5))
plt.savefig("./output/plot.png")
plt.show()

Interactive plots

Matplotlib's animation module allows users to create interactive and dynamic visualizations, perfect for showcasing time-based data or data with changing characteristics. The resulting interactive plot showcases the sine wave shifting horizontally, and the title dynamically changes to indicate the current iteration.

import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
# Sample data
x = np.linspace(0, 10, 100)
y = np.sin(x)
# Initialize the figure and axis
fig, ax = plt.subplots()
# Set the x and y limits of the plot
ax.set_xlim(0, 10)
ax.set_ylim(-1, 1)
# Initialize the line object (empty plot)
line, = ax.plot([], [], lw=2)
# Function to update the plot for each frame of the animation
def update(i):
line.set_data(x, np.sin(x + i / 5))
ax.set_title(f'Interactive Plot: Iteration {i}')
return line,
# Create the animation
ani = animation.FuncAnimation(fig, update, frames=range(1, 11), interval=500)
# Save the animation as a gif file (optional)
ani.save("./output/animation.gif", writer='pillow')
# Show the interactive plot
plt.show()

Rich ecosystem

Matplotlib's plt.bar() function is utilized to create a bar plot, showcasing a salary comparison for each individual in the DataFrame. The plot is customized with a blue color and black edges. Matplotlib integrates with Pandas in this code to create informative and visually appealing visualizations from DataFrame data.

import matplotlib.pyplot as plt
import pandas as pd
# Sample data
data = {
'Name': ['John', 'Alice', 'Bob', 'Emma', 'David'],
'Age': [28, 24, 22, 29, 25],
'Salary': [50000, 42000, 38000, 55000, 47000]
}
# Create a DataFrame from the sample data
df = pd.DataFrame(data)
# Matplotlib bar plot using Pandas DataFrame
plt.figure(figsize=(8, 5))
plt.bar(df['Name'], df['Salary'], color='skyblue', edgecolor='black')
plt.xlabel('Name')
plt.ylabel('Salary')
plt.title('Salary Comparison using Matplotlib and Pandas')
plt.savefig("./output/plot.png")
plt.show()

Annotations and text

With the help of plt.annotate() we can annotate a specific data point on the plot. It enables us to add arrows, text, and various formatting options to the annotation. Run this code and look at how Matplotlib adds annotation by specifying the maximum point in the sinusoidal curve.

import matplotlib.pyplot as plt
import numpy as np
# Sample data
x = np.linspace(0, 10, 100)
y = np.sin(x)
# Plotting the line plot
plt.plot(x, y, label='Data', color='b', linestyle='-', linewidth=2)
# Adding annotations and text
plt.annotate('Maximum', xy=(np.pi/2, 1), xytext=(3, 1.5),
arrowprops=dict(facecolor='black', shrink=0.05), fontsize=10)
plt.text(8, 0, 'Sinusoidal Curve', fontsize=12, bbox=dict(facecolor='white', alpha=0.5))
plt.xlabel('X-axis', fontsize=12)
plt.ylabel('Y-axis', fontsize=12)
plt.title('Line Plot with Annotations and Text', fontsize=14)
plt.legend(fontsize=12)
plt.grid(True, linestyle='--', alpha=0.7)
plt.tight_layout()
plt.savefig("./output/plot.png")
plt.show()

Twin axes

Twin axes use two scales on the y-axis to represent two distinct datasets simultaneously. In the code below, there are two plots, one for the sin(x)sin(x) and other for 2cos(x)2cos(x).

import matplotlib.pyplot as plt
import numpy as np
# Sample data
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = 2 * np.cos(x)
# Plotting two lines with different scales on the y-axis
fig, ax1 = plt.subplots()
ax1.plot(x, y1, 'b')
ax1.set_xlabel('X-axis')
ax1.set_ylabel('Sin(x)', color='b')
ax1.tick_params('y', colors='b')
ax2 = ax1.twinx()
ax2.plot(x, y2, 'r')
ax2.set_ylabel('2 * Cos(x)', color='r')
ax2.tick_params('y', colors='r')
plt.title('Twin Axes - Secondary Y-axis')
plt.grid(True, linestyle='--', alpha=0.7)
plt.savefig("./output/plot.png")
plt.show()

Downsampling

Downsampling refers to reducing the number of data points in a plot to simplify and speed up the rendering of large datasets. Downsampling is useful when dealing with datasets containing many points that could overwhelm the plot and make it visually cluttered. Two plots are shown in the output; one is before downsampling, and the other is after downsampling.

import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
import numpy as np
# Load the built-in "iris" dataset
iris = load_iris()
data = iris.data
target = iris.target
# Calculate summary statistics
summary_stats = {
'mean': data.mean(axis=0),
'median': np.median(data, axis=0),
'std': data.std(axis=0),
}
# Downsampling the data
downsampled_data = data[::10] # Select every 10th data point
# Plotting the original data
plt.figure(figsize=(10, 6))
plt.scatter(data[:, 0], data[:, 1], c=target, cmap='viridis', edgecolors='k')
plt.xlabel('Sepal Length (cm)')
plt.ylabel('Sepal Width (cm)')
plt.title('Original Iris Data')
plt.colorbar(label='Target Class')
plt.savefig("./output/plot.png")
plt.show()
# Plotting the downsampled data
plt.figure(figsize=(10, 6))
plt.scatter(downsampled_data[:, 0], downsampled_data[:, 1], c=target[::10], cmap='viridis', edgecolors='k')
plt.xlabel('Sepal Length (cm)')
plt.ylabel('Sepal Width (cm)')
plt.title('Downsampled Iris Data')
plt.colorbar(label='Target Class')
plt.savefig("./output/plot1.png")
plt.show()
# Displaying the summary statistics
print("Summary Statistics:")
for stat, values in summary_stats.items():
print(f"{stat.capitalize()}:\n{values}\n")

Conclusion

Matplotlib is a data visualization library in Python that offers functions for creating high-quality plots and visualizations. In short, its ability to handle large datasets and features like downsampling makes it an excellent tool for visualizing big data.

Q

Matplotlib supports a wide range of plot types, including:

A)

Scatter plots and line plots

B)

Bar plots and histograms

C)

3D plots and polar plots

D)

All of the above

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved