Setup & Basics
| Task | Code | Description |
|---|
| Import Matplotlib | import matplotlib.pyplot as plt | Import pyplot module |
| Inline plotting (Jupyter) | %matplotlib inline | Display plots inline |
| Basic plot | plt.plot([1,2,3],[4,5,6]) | Simple line plot |
| Show plot | plt.show() | Display figure |
| Create figure | plt.figure(figsize=(6,4)) | Create custom figure |
| Clear figure | plt.clf() | Clear figure |
| Save figure | plt.savefig('plot.png') | Save plot as image |
| Close figure | plt.close() | Close figure window |
Line Plot
| Task | Code | Description |
|---|
| Simple line | plt.plot(x, y) | Basic line chart |
| Line color | plt.plot(x, y, color='red') | Change line color |
| Line style | plt.plot(x, y, linestyle='--') | Dashed line |
| Line width | plt.plot(x, y, linewidth=2) | Set line thickness |
| Marker style | plt.plot(x, y, marker='o') | Show data points |
| Marker size | plt.plot(x, y, marker='s', markersize=8) | Change marker size |
| Multiple lines | plt.plot(x, y1); plt.plot(x, y2) | Multiple plots |
| Label line | plt.plot(x, y, label='Sales') | Add label |
| Show legend | plt.legend() | Display legend box |
Labels & Titles
| Task | Code | Description |
|---|
| X-axis label | plt.xlabel('X Axis') | Add X label |
| Y-axis label | plt.ylabel('Y Axis') | Add Y label |
| Title | plt.title('My Graph') | Add graph title |
| Add grid | plt.grid(True) | Show grid lines |
| Set axis limits | plt.xlim(0,10) / plt.ylim(0,100) | Define value range |
| Add text | plt.text(2, 50, 'Peak') | Place text inside plot |
| Annotate | plt.annotate('Point', xy=(2,3), xytext=(3,5), arrowprops=dict(arrowstyle='->')) | Add arrow annotation |
Bar Chart
| Task | Code | Description |
|---|
| Vertical bars | plt.bar(x, y) | Basic bar chart |
| Horizontal bars | plt.barh(x, y) | Horizontal bars |
| Bar color | plt.bar(x, y, color='green') | Change bar color |
| Bar width | plt.bar(x, y, width=0.4) | Adjust bar width |
| Add labels | plt.xlabel('Items'); plt.ylabel('Quantity') | Axis labels |
| Add values | plt.text(i, v, str(v)) | Add numbers on bars |
| Multiple bars | plt.bar(x-0.2, y1, width=0.4); plt.bar(x+0.2, y2, width=0.4) | Grouped bars |
Pie Chart
| Task | Code | Description |
|---|
| Basic pie | plt.pie(values) | Simple pie chart |
| Add labels | plt.pie(values, labels=labels) | Label each slice |
| Show percentages | plt.pie(values, labels=labels, autopct='%1.1f%%') | Display percentage |
| Explode slice | plt.pie(values, explode=[0,0.1,0,0]) | Offset one slice |
| Start angle | plt.pie(values, startangle=90) | Rotate start point |
| Equal aspect | plt.axis('equal') | Make pie circular |
Scatter Plot
| Task | Code | Description |
|---|
| Basic scatter | plt.scatter(x, y) | Simple scatter plot |
| Marker size | plt.scatter(x, y, s=50) | Control dot size |
| Marker color | plt.scatter(x, y, c='red') | Set color |
| Vary colors | plt.scatter(x, y, c=values, cmap='viridis') | Gradient colors |
| Transparency | plt.scatter(x, y, alpha=0.5) | Semi-transparent dots |
| Add color bar | plt.colorbar() | Add color scale |
Histogram
| Task | Code | Description |
|---|
| Basic histogram | plt.hist(data) | Plot frequency distribution |
| Bins | plt.hist(data, bins=10) | Number of bins |
| Color | plt.hist(data, color='orange') | Set color |
| Edge color | plt.hist(data, edgecolor='black') | Add edges |
| Transparency | plt.hist(data, alpha=0.7) | Adjust opacity |
| Orientation | plt.hist(data, orientation='horizontal') | Horizontal bars |
Subplots
| Task | Code | Description |
|---|
| Create subplot | plt.subplot(2,2,1) | 2x2 grid, first plot |
| Adjust layout | plt.tight_layout() | Prevent overlap |
| Figure + axes | fig, ax = plt.subplots() | Create figure/axes object |
| Multiple axes | fig, (ax1, ax2) = plt.subplots(1,2) | Side-by-side plots |
| Plot on axis | ax.plot(x, y) | Plot using object-oriented style |
| Set title | ax.set_title('Line Chart') | Add title to subplot |
Colors, Styles, & Themes
| Task | Code | Description |
|---|
| Change style | plt.style.use('ggplot') | Apply theme |
| Available styles | plt.style.available | Show all styles |
| Set color | color='r' or '#FF5733' | RGB or hex |
| Line style | linestyle='--' or ':' | Custom dash |
| Marker type | marker='o' or 's' | Shape of points |
| Alpha transparency | alpha=0.6 | Opacity level |
Axes Customization
| Task | Code | Description |
|---|
| Set ticks | plt.xticks([1,2,3]) | Custom x ticks |
| Tick labels | plt.xticks([1,2], ['A','B']) | Rename ticks |
| Rotate ticks | plt.xticks(rotation=45) | Rotate labels |
| Add legend | plt.legend(loc='upper left') | Legend position |
| Change background | ax.set_facecolor('#EFEFEF') | Background color |
| Add border | plt.box(True) | Toggle border box |
3D Plot (Optional)
| Task | Code | Description |
|---|
| Import 3D Axes | from mpl_toolkits.mplot3d import Axes3D | 3D setup |
| Create figure | fig = plt.figure() | New figure |
| Add 3D plot | ax = fig.add_subplot(111, projection='3d') | Add 3D subplot |
| 3D scatter | ax.scatter(x, y, z) | 3D scatter plot |
| 3D line | ax.plot3D(x, y, z) | 3D line plot |
| 3D surface | ax.plot_surface(X, Y, Z, cmap='viridis') | Surface plot |
Save & Export
| Task | Code | Description |
|---|
| Save PNG | plt.savefig('chart.png') | Save as image |
| Save high quality | plt.savefig('chart.png', dpi=300) | High resolution |
| Save PDF | plt.savefig('plot.pdf') | Export as PDF |
| Transparent background | plt.savefig('chart.png', transparent=True) | Transparent plot |