fig, ax = plt.subplots() # Single subplot
fig, axes = plt.subplots(2, 2) # 2x2 grid of subplots
• Plot on a specific subplot (Axes object).
axes[0, 0].plot(x, np.sin(x))
• Set the title for a specific subplot.
axes[0, 0].set_title('Subplot 1')• Set labels for a specific subplot.
axes[0, 0].set_xlabel('X-axis')
axes[0, 0].set_ylabel('Y-axis')• Add a legend to a specific subplot.
axes[0, 0].legend(['Sine'])
• Add a main title for the entire figure.
fig.suptitle('Main Figure Title')• Automatically adjust subplot parameters for a tight layout.
plt.tight_layout()
• Share x or y axes between subplots.
fig, axes = plt.subplots(2, 1, sharex=True)
• Get the current Axes instance.
ax = plt.gca()
• Create a second y-axis that shares the x-axis.
ax2 = ax.twinx()
VI. Specialized Plots
• Create a contour plot.
X, Y = np.meshgrid(x, x)
Z = np.sin(X) * np.cos(Y)
plt.contour(X, Y, Z, levels=10)
• Create a filled contour plot.
plt.contourf(X, Y, Z)
• Create a stream plot for vector fields.
U, V = np.cos(X), np.sin(Y)
plt.streamplot(X, Y, U, V)
• Create a 3D surface plot.
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z)
#Python #Matplotlib #DataVisualization #DataScience #Plotting
━━━━━━━━━━━━━━━
By: @DataScienceM ✨