🎨 Matplotlib Plotting Tutorial¶
Matplotlib is a versatile plotting library for Python. Understanding which type of plot or graph to use for various data scenarios can help you effectively communicate information.
To get started, let's import matplotlib and numpy.
import matplotlib.pyplot as plt
import numpy as np
1. Line Plot¶
- Description: Displays information as a series of data points connected by straight line segments.
- Useful For: Time series data, showing trends over intervals.
x = np.linspace(0, 10, 100)
ax = plt.subplot()
ax.plot(x, np.sin(x))
ax.set_title("Line Plot")
plt.show()
2. Scatter Plot¶
- Description: Uses dots to represent values for two numerical variables.
- Useful For: Observing relationships between variables.
x = np.random.rand(50)
y = np.random.rand(50)
ax = plt.subplot()
ax.scatter(x, y)
ax.set_title("Scatter Plot")
plt.show()
3. Bar Chart¶
- Description: Represents categorical data with rectangular bars.
- Useful For: Comparing quantities of different categories.
labels = ["A", "B", "C"]
values = [10, 40, 25]
ax = plt.subplot()
ax.bar(labels, values)
ax.set_title("Bar Chart")
plt.show()
4. Histogram¶
- Description: Illustrates the distribution of data by forming bins along the range of the data and then drawing bars to show the number of observations that fall in each bin.
- Useful For: Understanding the underlying frequency distribution of a set of continuous data.
data = np.random.randn(1000)
ax = plt.subplot()
ax.hist(data, bins=30)
ax.set_title("Histogram")
plt.show()
5. Pie Chart¶
- Description: Circular statistical graphic divided into slices to illustrate numerical proportion.
- Useful For: Displaying relative percentages or proportions of multiple categories.
labels = ["A", "B", "C", "D"]
sizes = [15, 30, 45, 10]
ax = plt.subplot()
ax.pie(sizes, labels=labels, autopct='%1.1f%%')
ax.set_title("Pie Chart")
plt.show()
6. Box Plot¶
- Description: Displays a summary of a set of data values, including the minimum, first quartile, median, third quartile, and maximum.
- Useful For: Understanding the spread and skew of your data.
data = [np.random.randn(1000), 5 * np.random.randn(1000) + 50]
ax = plt.subplot()
ax.boxplot(data)
ax.set_title("Box Plot")
plt.show()
7. Contour Plot¶
- Description: Represents three-dimensional data in two dimensions using contours or color-coded regions.
- Useful For: Visualizing data that has three continuous variables.
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))
plt.contourf(X, Y, Z, 20, cmap='RdGy')
plt.title("Contour Plot")
plt.colorbar()
plt.show()
Final Thoughts
When choosing a plot type, always consider the nature of your data and the story you want to tell. Different visual representations can provide unique insights into data, but the key is matching the right visualization to your data's characteristics and the questions you're trying to answer.
This tutorial provides an introduction to some fundamental plot types in Matplotlib. Still, the library offers much more, from 3D plots to more complex visualizations. As you grow in your data visualization journey, exploring the Matplotlib documentation and experimenting with its various functions will be invaluable.