Matplotlib supports a variety of plot types, each suited for different kinds of data visualization. Here are some common plot types along with brief explanations and examples:
1. Line Plot
Used to display data points connected by lines, ideal for showing trends over time.
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.plot(x, y)
plt.title("Line Plot")
plt.xlabel("X values")
plt.ylabel("Sine of X")
plt.show()
2. Bar Plot
Useful for comparing quantities corresponding to different groups.
categories = ['A', 'B', 'C', 'D']
values = [3, 7, 5, 2]
plt.bar(categories, values)
plt.title("Bar Plot")
plt.xlabel("Categories")
plt.ylabel("Values")
plt.show()
3. Histogram
Displays the distribution of a dataset by grouping data into bins.
data = np.random.randn(1000) # Generate random data
plt.hist(data, bins=30, alpha=0.7)
plt.title("Histogram")
plt.xlabel("Value")
plt.ylabel("Frequency")
plt.show()
4. Scatter Plot
Shows the relationship between two numerical variables, useful for identifying correlations.
x = np.random.rand(50)
y = np.random.rand(50)
plt.scatter(x, y)
plt.title("Scatter Plot")
plt.xlabel("X values")
plt.ylabel("Y values")
plt.show()
5. Pie Chart
Displays proportions of a whole, useful for showing percentage breakdowns.
sizes = [15, 30, 45, 10]
labels = ['A', 'B', 'C', 'D']
plt.pie(sizes, labels=labels, autopct='%1.1f%%')
plt.title("Pie Chart")
plt.show()
6. Box Plot
Visualizes the distribution of data based on a five-number summary: minimum, first quartile, median, third quartile, and maximum.
data = [np.random.normal(0, std, 100) for std in range(1, 4)]
plt.boxplot(data, vert=True, patch_artist=True)
plt.title("Box Plot")
plt.xlabel("Dataset")
plt.ylabel("Value")
plt.show()
Further Learning
To explore more about these plot types and their customization options, consider checking out the Matplotlib documentation or relevant labs on LabEx. Experimenting with different datasets and visual styles can also enhance your understanding of data visualization.
If you have any specific plot types you want to learn more about or need help with, feel free to ask!
