Python - Matplotlib Pyplot Part 5: Advanced Plots (Pie Charts, Subplots, and 3D Plots)
Advanced plotting capabilities include creating pie charts, subplots, and 3D visualizations.
Examples and Explanation
Pie Chart
labels = ['A', 'B', 'C']
sizes = [50, 30, 20]
plt.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90)
plt.title("Pie Chart")
plt.show()
Explanation: The pie() function creates a circular chart representing proportions.
Subplots
plt.subplot(1, 2, 1)
plt.plot([1, 2, 3], [4, 5, 6])
plt.title("Line Plot")
plt.subplot(1, 2, 2)
plt.bar(['A', 'B', 'C'], [3, 7, 5])
plt.title("Bar Chart")
plt.show()
Explanation: Use subplot() to display multiple plots in one figure.
3D Plot
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = [1, 2, 3]
y = [4, 5, 6]
z = [7, 8, 9]
ax.plot(x, y, z)
plt.title("3D Plot")
plt.show()
Explanation: 3D plots add depth to data visualization, useful in scientific research.
Conclusion
Matplotlib Pyplot provides a versatile and comprehensive set of tools for creating visualizations. By mastering these five parts, you can create compelling visual representations of your data, catering to a variety of needs, from exploratory data analysis to presentation-ready plots.