Unveiling Data Insights With Matplotlib
Unveiling Data Insights With Matplotlib
1
# Using a predefined color map
plt.imshow(data, cmap='viridis')
plt.style.use('ggplot')
1.3.2 Annotations
Annotations provide additional information on your plots, such as arrows, text boxes, and high-
lights.
import matplotlib.pyplot as plt
2
# Creating a 2x3 grid of subplots
grid = GridSpec(2, 3)
subplots_adjust(wspace=0.4, hspace=0.4)
plt.align_labels()
3
1.6 Nested Subplots and Shared Axes
1.6.1 Nested Subplots
You can create nested subplots to showcase different levels of detail within your data.
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
plt.subplot(inner_grid_left[0])
plt.subplot(inner_grid_left[1])
plt.subplot(inner_grid_right[0])
plt.subplot(inner_grid_right[1])
plt.subplot(inner_grid_right[2])
ax1 = plt.subplot(grid[0])
ax2 = plt.subplot(grid[1], sharex=ax1)
ax2 = ax1.twinx()
ax2.plot(x, y2, 'b-')
ax2.set_ylabel('Secondary Y-axis', color='b')
4
1.8 Hexbin Plots: Visualizing Density
Hexbin plots are excellent for visualizing the density of points in a scatterplot. Instead of displaying
individual data points, hexbins divide the plot area into hexagonal bins and represent the data
density within each bin with color or shading.
import matplotlib.pyplot as plt
# Generating treemap
sizes = [30, 20, 25, 15, 10]
labels = ['A', 'B', 'C', 'D', 'E']
colors = plt.cm.Spectral
squarify.plot(sizes=sizes, label=labels, color=colors, alpha=0.7)
plt.axis('off')
plt.title('Treemap: Hierarchical Data Representation')
plt.show()
5
For sunburst charts, you might want to explore libraries like Plotly, which offer interactive sunburst
visualizations.
x = [1, 2, 3, 4, 5]
y = [2, 3, 4, 5, 6]
z = [3, 4, 5, 6, 7]
ax.set_xlabel('X Label')
6
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.show()
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
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))
ax.plot_surface(X, Y, Z, cmap='viridis')
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.show()
X, Y = np.meshgrid(x, y)
Z = np.sin(np.sqrt(X**2 + Y**2))
ax.plot_wireframe(X, Y, Z, cmap='plasma')
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.show()
7
2.5 Combining 2D and 3D Elements
Matplotlib allows you to combine 2D and 3D elements within the same plot, providing enhanced
context to your visualizations.
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x_2d = [1, 2, 3, 4, 5]
y_2d = [2, 4, 6, 8, 10]
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.legend()
plt.show()
ax.view_init(elev=elev, azim=azim)
ax.plot_surface(X, Y, Z, cmap='viridis')
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.show()
8
3 Conclusion
In the world of data visualization, Matplotlib stands as a versatile and powerful library, offering a
wide array of techniques to elevate your plots from basic to breathtaking. Through this journey,
we’ve explored a range of advanced visualization concepts that can bring your data to life in
captivating ways.
As you embark on your data visualization journey, remember that each technique is a tool in
your toolkit, to be chosen based on the data’s nature and the story you aim to tell. Matplotlib’s
versatility empowers you to choose the right tool for the job, whether it’s enhancing aesthetics,
managing subplots, embracing advanced plot types, or venturing into 3D visualization.
The culmination of these techniques allows you to create visualizations that not only present data
but also communicate insights effectively. A well-crafted visualization has the power to enlighten,
inform, and inspire, making it an essential skill for data scientists, analysts, and researchers across
disciplines. By mastering these advanced visualization concepts, you’ve equipped yourself to tell
richer, more impactful stories through your data. Happy plotting!