![]()
Matplotlib is one of the most popular libraries in Python for creating static, animated, and interactive visualizations. It provides a wide range of plotting options, making it a powerful tool for visualizing data in different formats. In this guide, we will introduce the basics of data visualization using Matplotlib and cover how to create various types of plots.
1. Installing Matplotlib
To use Matplotlib, you need to install it first. You can install it using pip:
pip install matplotlib
Once installed, you can import Matplotlib as follows:
pythonCopyEditimport matplotlib.pyplot as plt
By convention, the pyplot module from Matplotlib is imported as plt.
2. Basic Plotting with Matplotlib
The core function for creating plots in Matplotlib is plot(), which can be used to generate basic line plots.
2.1. Simple Line Plot
Here’s how you can create a basic line plot.
import matplotlib.pyplot as plt
# Data for plotting
x = [1, 2, 3, 4, 5]
y = [10, 20, 25, 30, 35]
# Creating a simple line plot
plt.plot(x, y)
# Adding title and labels
plt.title('Simple Line Plot')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
# Display the plot
plt.show()
Output: A simple line plot will be displayed showing the relationship between x and y.
2.2. Customizing Line Plot
You can customize the appearance of the plot by adding markers, changing colors, line styles, and more.
plt.plot(x, y, color='red', marker='o', linestyle='--', linewidth=2)
plt.title('Customized Line Plot')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.show()
3. Plotting Multiple Lines
You can plot multiple lines on the same plot by calling plot() multiple times.
y2 = [5, 15, 20, 25, 30]
# Plot multiple lines
plt.plot(x, y, label='Line 1', color='blue', marker='o')
plt.plot(x, y2, label='Line 2', color='green', marker='x')
# Adding labels and title
plt.title('Multiple Lines Plot')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
# Displaying legend
plt.legend()
# Show the plot
plt.show()
4. Other Types of Plots
4.1. Scatter Plot
A scatter plot is used to show the relationship between two variables, typically to observe any correlation.
# Scatter plot
plt.scatter(x, y, color='purple', marker='^')
plt.title('Scatter Plot')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.show()
4.2. Bar Plot
A bar plot is useful for comparing quantities corresponding to different categories. The bars can be vertical or horizontal.
# Bar plot
categories = ['A', 'B', 'C', 'D', 'E']
values = [5, 10, 15, 20, 25]
plt.bar(categories, values, color='orange')
plt.title('Bar Plot')
plt.xlabel('Categories')
plt.ylabel('Values')
plt.show()
For horizontal bars:
plt.barh(categories, values, color='skyblue')
plt.title('Horizontal Bar Plot')
plt.xlabel('Values')
plt.ylabel('Categories')
plt.show()
4.3. Histogram
A histogram shows the distribution of a dataset by dividing it into bins.
import numpy as np
# Create some data
data = np.random.randn(1000)
# Create a histogram
plt.hist(data, bins=30, color='green', edgecolor='black')
plt.title('Histogram')
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.show()
4.4. Pie Chart
A pie chart is used to represent proportions of categories in a dataset.
# Pie chart
labels = ['Category A', 'Category B', 'Category C']
sizes = [40, 30, 30]
plt.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90, colors=['lightblue', 'lightgreen', 'lightcoral'])
plt.title('Pie Chart')
plt.show()
5. Customizing Plots
Matplotlib allows extensive customization of plots. You can modify titles, labels, axes limits, gridlines, and more.
5.1. Adding Titles and Labels
You can add titles and labels to your plots for better readability.
plt.plot(x, y)
plt.title('Plot with Title')
plt.xlabel('X-axis Label')
plt.ylabel('Y-axis Label')
plt.show()
5.2. Changing Axes Limits
You can set the limits for both the x-axis and y-axis using xlim() and ylim().
plt.plot(x, y)
plt.xlim(0, 6)
plt.ylim(0, 40)
plt.show()
5.3. Adding Gridlines
You can add gridlines to the plot to improve readability.
plt.plot(x, y)
plt.grid(True) # Display gridlines
plt.show()
6. Subplots
Sometimes, you may want to display multiple plots in a single figure. You can use the subplot() method to create multiple subplots in the same figure.
# Create a 2x2 grid of subplots
plt.subplot(2, 2, 1) # 2 rows, 2 columns, 1st subplot
plt.plot(x, y)
plt.title('Plot 1')
plt.subplot(2, 2, 2) # 2nd subplot
plt.bar(categories, values)
plt.title('Plot 2')
plt.subplot(2, 2, 3) # 3rd subplot
plt.scatter(x, y, color='red')
plt.title('Plot 3')
plt.subplot(2, 2, 4) # 4th subplot
plt.pie(sizes, labels=labels, autopct='%1.1f%%')
plt.title('Plot 4')
# Display all subplots
plt.tight_layout() # Adjust the layout for better spacing
plt.show()
7. Saving Plots
Once you’ve created your plot, you can save it to a file using the savefig() function.
# Save plot as a PNG file
plt.plot(x, y)
plt.title('Saved Plot')
plt.savefig('plot.png')
# Save plot as a PDF file
plt.savefig('plot.pdf')
