import matplotlib.pyplot as plt | Import pyplot |
import numpy as np | Import numpy (often used) |
%matplotlib inline | Jupyter inline display |
plt.style.use("seaborn") | Use style preset |
plt.rcParams["figure.figsize"] = [10, 6] | Set default figure size |
plt.plot(x, y) | Basic line plot |
plt.plot(x, y, "r--") | Red dashed line |
plt.plot(x, y, "bo-") | Blue circles with line |
plt.plot(x, y, linewidth=2) | Set line width |
plt.plot(x, y, label="data1") | Add label for legend |
plt.plot(x, y1, x, y2) | Multiple lines |
plt.show() | Display plot |
plt.savefig("plot.png") | Save as PNG |
plt.savefig("plot.pdf", dpi=300) | Save high-res PDF |
plt.savefig("plot.svg", format="svg") | Save as SVG |
plt.close() | Close figure |
plt.scatter(x, y) | Scatter plot |
plt.scatter(x, y, c=colors, s=sizes) | With colors and sizes |
plt.bar(x, height) | Vertical bar chart |
plt.barh(y, width) | Horizontal bar chart |
plt.bar(x, h1, label="A"); plt.bar(x, h2, bottom=h1, label="B") | Stacked bar |
plt.hist(data, bins=30) | Histogram |
plt.hist(data, bins=30, density=True) | Normalized histogram |
plt.hist2d(x, y, bins=30) | 2D histogram |
plt.boxplot(data) | Box plot |
plt.violinplot(data) | Violin plot |
plt.pie(sizes, labels=labels) | Pie chart |
plt.pie(sizes, explode=[0,0.1,0,0]) | Exploded pie |
plt.fill_between(x, y1, y2) | Fill between curves |
plt.stackplot(x, y1, y2, y3) | Stacked area |
plt.contour(X, Y, Z) | Contour lines |
plt.contourf(X, Y, Z) | Filled contours |
plt.imshow(data, cmap="hot") | Heatmap/image |
plt.pcolormesh(X, Y, Z) | Pseudocolor plot |
plt.colorbar() | Add colorbar |
plt.xlabel("X axis") | X axis label |
plt.ylabel("Y axis") | Y axis label |
plt.title("Plot Title") | Plot title |
plt.suptitle("Super Title") | Figure super title |
plt.legend() | Show legend |
plt.legend(loc="upper right") | Legend position |
plt.xlim(0, 10) | Set x limits |
plt.ylim(-1, 1) | Set y limits |
plt.axis("equal") | Equal aspect ratio |
plt.axis("off") | Hide axes |
plt.grid(True) | Show grid |
plt.grid(alpha=0.5, linestyle="--") | Styled grid |
plt.xticks([0,1,2], ["a","b","c"]) | Custom x ticks |
plt.yscale("log") | Logarithmic scale |
color="red" or color="#FF0000" | Set color |
cmap="viridis" | Colormap |
linestyle="-" or "--" or ":" or "-." | Line styles |
marker="o" or "s" or "^" or "x" | Marker styles |
alpha=0.5 | Transparency |
plt.text(x, y, "text") | Add text |
plt.annotate("note", xy=(x,y), xytext=(x2,y2), arrowprops=dict(arrowstyle="->")) | Annotate with arrow |
plt.axhline(y=0, color="k") | Horizontal line |
plt.axvline(x=0, color="k") | Vertical line |
plt.axhspan(ymin, ymax, alpha=0.3) | Horizontal span |
fig, ax = plt.subplots() | Single subplot |
fig, axes = plt.subplots(2, 3) | 2x3 grid of subplots |
fig, axes = plt.subplots(2, 2, figsize=(10, 8)) | With figure size |
axes[0, 1].plot(x, y) | Plot on specific subplot |
plt.subplot(2, 2, 1) | Select subplot (1-indexed) |
plt.tight_layout() | Auto-adjust spacing |
plt.subplots_adjust(hspace=0.5, wspace=0.3) | Manual spacing |
fig, axes = plt.subplots(2, 2, sharex=True) | Share x axis |
ax2 = ax1.twinx() | Secondary y axis |
fig = plt.figure(figsize=(10, 6)) | Create figure |
ax = fig.add_subplot(111) | Add subplot |
ax.plot(x, y) | Plot on axes |
ax.set_xlabel("X") | Set x label |
ax.set_title("Title") | Set title |
ax.legend() | Show legend |
from mpl_toolkits.mplot3d import Axes3D | Import 3D toolkit |
ax = fig.add_subplot(111, projection="3d") | Create 3D axes |
ax.plot3D(x, y, z) | 3D line plot |
ax.scatter3D(x, y, z) | 3D scatter |
ax.plot_surface(X, Y, Z, cmap="viridis") | 3D surface |
ax.plot_wireframe(X, Y, Z) | 3D wireframe |
from matplotlib.animation import FuncAnimation | Import animation |
ani = FuncAnimation(fig, update, frames=100) | Create animation |
ani.save("anim.gif", writer="pillow") | Save as GIF |
ani.save("anim.mp4", writer="ffmpeg") | Save as MP4 |