Matplotlib's Pyplot module provides a MATLAB-like interface for creating various types of plots and visualizations in Python. It is part of the Matplotlib library and is typically imported as plt
. Here's an overview of how to use Pyplot to create different types of plots and customize them.
First, you need to import the pyplot
module from the matplotlib
library:
import matplotlib.pyplot as plt
A line plot is one of the simplest types of plots. It shows data as a series of points connected by straight lines.
# Data x = [1, 2, 3, 4, 5] y = [2, 3, 5, 7, 11] # Create a line plot plt.plot(x, y) plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.title('Line Plot') plt.show()
A scatter plot shows individual data points and is useful for visualizing relationships between variables.
# Data x = [1, 2, 3, 4, 5] y = [2, 3, 5, 7, 11] # Create a scatter plot plt.scatter(x, y) plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.title('Scatter Plot') plt.show()
A bar plot represents data with rectangular bars.
# Data categories = ['A', 'B', 'C', 'D'] values = [15, 30, 45, 10] # Create a bar plot plt.bar(categories, values) plt.xlabel('Categories') plt.ylabel('Values') plt.title('Bar Plot') plt.show()
A histogram shows the distribution of a dataset.
import numpy as np # Data data = np.random.randn(1000) # Create a histogram plt.hist(data, bins=30) plt.xlabel('Value') plt.ylabel('Frequency') plt.title('Histogram') plt.show()
You can customize plots with titles, labels, legends, grid lines, and more.
# Data x = [1, 2, 3, 4, 5] y = [2, 3, 5, 7, 11] # Create a plot with labels and title plt.plot(x, y) plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.title('Line Plot with Labels') plt.show()
# Data x = [1, 2, 3, 4, 5] y1 = [1, 4, 9, 16, 25] y2 = [2, 3, 5, 7, 11] # Create a plot with multiple lines and legend plt.plot(x, y1, label='Squared', marker='o') plt.plot(x, y2, label='Prime', marker='s') plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.title('Line Plot with Legend') plt.legend() plt.show()
# Data x = [1, 2, 3, 4, 5] y = [2, 3, 5, 7, 11] # Create a plot with grid lines plt.plot(x, y) plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.title('Line Plot with Grid') plt.grid(True) plt.show()
You can create multiple plots in a single figure using subplots.
# Data x = [1, 2, 3, 4, 5] y1 = [1, 4, 9, 16, 25] y2 = [2, 3, 5, 7, 11] # Create subplots fig, axs = plt.subplots(2) # First subplot axs[0].plot(x, y1) axs[0].set_title('Squared Values') # Second subplot axs[1].plot(x, y2) axs[1].set_title('Prime Numbers') # Show plot plt.tight_layout() plt.show()
Here's a comprehensive example that combines different types of plots and customizations:
# Data x = [1, 2, 3, 4, 5] y1 = [1, 4, 9, 16, 25] y2 = [2, 3, 5, 7, 11] # Create subplots fig, axs = plt.subplots(2) # First subplot axs[0].plot(x, y1) axs[0].set_title('Squared Values') # Second subplot axs[1].plot(x, y2) axs[1].set_title('Prime Numbers') # Show plot plt.tight_layout() plt.show()
Here's a comprehensive example that combines different types of plots and customizations:
import matplotlib.pyplot as plt import numpy as np # Data x = np.linspace(0, 10, 100) y1 = np.sin(x) y2 = np.cos(x) # Create subplots fig, axs = plt.subplots(2, 2, figsize=(10, 10)) # First subplot: Line plot axs[0, 0].plot(x, y1, 'r-', label='sin(x)') axs[0, 0].set_title('Line Plot') axs[0, 0].legend() # Second subplot: Scatter plot axs[0, 1].scatter(x, y2, color='b', label='cos(x)') axs[0, 1].set_title('Scatter Plot') axs[0, 1].legend() # Third subplot: Histogram data = np.random.randn(1000) axs[1, 0].hist(data, bins=30, color='g') axs[1, 0].set_title('Histogram') # Fourth subplot: Bar plot categories = ['A', 'B', 'C', 'D'] values = [15, 30, 45, 10] axs[1, 1].bar(categories, values, color='y') axs[1, 1].set_title('Bar Plot') # Adjust layout plt.tight_layout() plt.show()
You can save the plot to a file using savefig
.
# Create a plot plt.plot(x, y) plt.title('Saved Plot') # Save the plot to a file plt.savefig('plot.png') # Display the plot plt.show()
You can specify the figure size when creating a plot.
# Create a plot with custom figure size plt.figure(figsize=(8, 6)) plt.plot(x, y) plt.title('Custom Figure Size') plt.show()