Skip to content

Instantly share code, notes, and snippets.

@mehdirezaie
Last active October 22, 2020 00:11
Show Gist options
  • Save mehdirezaie/b43314cf633c10bfbadfb447faccbcb5 to your computer and use it in GitHub Desktop.
Save mehdirezaie/b43314cf633c10bfbadfb447faccbcb5 to your computer and use it in GitHub Desktop.

Colormaps and Contours

This example illustrates how to visualize two maps with colorbars and contours.

x = np.linspace(-10, 10, num=100)
y = np.linspace(-10, 10, num=100)
X, Y = np.meshgrid(x, y)

Z1 = X*X + Y*Y
Z2 = abs(X) + abs(Y)


fig, ax = plt.subplots(ncols=2, sharey=True, figsize=(8, 5.7))
fig.subplots_adjust(wspace=0.0, hspace=0.0)


kw = dict(cmap=plt.cm.rainbow, origin='lower', extent=(-10, 10, -10, 10))
kw2 = dict(orientation='horizontal', shrink=0.7)

map1 = ax[0].imshow(Z1, **kw)
ax[0].contour(X, Y, Z1, [20, 50, 100], origin='lower')
cbar1 = fig.colorbar(map1, ax=ax[0], **kw2)
cbar1.set_label(r'$X^{2}+Y^{2}$ (L2)')

map2 = ax[1].imshow(Z2, **kw)
ax[1].contour(X, Y, Z2, [2, 5, 10], origin='lower')
cbar2 = fig.colorbar(map2, ax=ax[1], **kw2)
cbar2.set_label(r'$|X|+|Y|$ (L1)')


ax[0].set_ylabel('Y')
ax[0].set_yticks([-10, -5, 0, 5, 10])
for axi in ax:
    axi.set_xlabel('X')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment