import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
= np.linspace(-1, 1, 21)
x = np.linspace(-2, 2, 21)
y = np.meshgrid(x, y)
X, Y = np.exp(-(X*X + Y*Y)**.5)
Z
= plt.subplots(1, 3, figsize=(9.0, 4.0), constrained_layout=True)
fig, axs
= axs.flat
ax, ax1, ax2
= ax.imshow(Z, extent=[x.min(), x.max(), y.min(), y.max()],
im ='lower', aspect='equal')
originset(xlabel='X axis', ylabel='Y axis', xticks=x[::4], yticks=y[::4], title='Using imshow');
ax.
# fig.colorbar(im, ax=ax)
# Plot Z using pcolormesh
='auto')
ax1.pcolormesh(x, y, Z, shadingset(aspect='equal', xlabel='X axis', ylabel='Y axis', title='using pcolormesh');
ax1.
# Plot filled contours using contourf
= ax2.contourf(X, Y, Z, levels=20, cmap='viridis')
contour_set
# Add colorbar for reference
=ax2)
fig.colorbar(contour_set, ax
set(#aspect='equal',
ax2.='X axis', ylabel='Y axis', title='using contourf'); xlabel
Matplotlib: tips and tricks
programming
Python
LLM
Here are several things I always forget when using matplotlib
. This post written with help from Gemini and ChatGPT4.0.