Matplotlib Complete Cheat Sheet
1. Basic Plot Setup
Function/Command Description Example
import matplotlib.pyplot as plt Standard import statement. import matplotlib.pyplot as plt
plt.figure() Create new figure. plt.figure(figsize=(10,6))
plt.show() Display the plot. plt.show()
plt.subplots() Create figure & axes for subplots. fig, axes = plt.subplots(2,2)
plt.savefig() Save plot as image file. plt.savefig('my_plot.png', dpi=300)
plt.close() Close current figure. plt.close()
2. Basic Plot Types
Function Description & Use Case Example
plt.plot() Line plot. Track training loss. plt.plot(x,y)
plt.scatter() Scatter plot. Visualize clusters. plt.scatter(X[:,0],X[:,1],c=y)
plt.bar() Bar plot. Feature importance. plt.bar(categories,values)
plt.hist() Histogram. Check distribution. plt.hist(data,bins=30)
plt.boxplot() Box plot. Compare features/outliers. plt.boxplot([data1,data2])
plt.imshow() Display 2D array/image. plt.imshow(image_array)
plt.pcolormesh() 2D colored grid. Decision boundaries. plt.pcolormesh(X,Y,Z)
3. Plot Customization & Styling
Function/Parameter Description Example
plt.title() Set plot title. plt.title('Model Accuracy')
plt.xlabel() Set X-axis label. plt.xlabel('Epochs')
plt.ylabel() Set Y-axis label. plt.ylabel('Accuracy')
plt.xlim(), plt.ylim() Set axis limits. plt.xlim(0,100)
plt.legend() Show legend. plt.legend(['Train','Validation'])
plt.grid() Add grid lines. plt.grid(True,alpha=0.3)
color Set line/point color. plt.plot(x,y,color='red')
linestyle Set line style. plt.plot(x,y,linestyle='--')
marker Set data markers. plt.plot(x,y,marker='o')
alpha Set transparency. plt.scatter(x,y,alpha=0.5)
4. Specialized Plots for AIML
Technique Description & Use Case Example
Subplots Multiple plots together for before-after comparison. fig, axes = plt.subplots()
Confusion Matrix Analyze classifier performance. plt.imshow() + plt.text()
ROC Curve Binary classifier performance. plt.plot(FPR, TPR)
Learning Curves Train/validation loss over time to check overfitting. Multiple plt.plot() calls
Decision Boundary Visualize class separation. plt.contourf() / plt.pcolormesh()