Python draws colorful inverted contour maps

import numpy as np
import matplotlib.pyplot as plt

origin = 'lower'

delta = 0.025

x = y = np.arange(-3.0, 3.01, delta)
X, Y = np.meshgrid(x, y)
Z1 = np.exp(-X**2 - Y**2)
Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)
Z = (Z1 - Z2) * 2

nr, nc = Z.shape

# put NaNs in one corner:
Z[-nr // 6:, -nc // 6:] = np.nan
# contourf will convert these to masked


Z = np.ma.array(Z)
# mask another corner:
Z[:nr // 6, :nc // 6] = np.ma.masked

# mask a circle in the middle:
interior = np.sqrt(X**2 + Y**2) < 0.5
Z[interior] = np.ma.masked
fig1, ax2 = plt.subplots(layout='constrained')
CS = ax2.contourf(X, Y, Z, 10, cmap=plt.cm.bone, origin=origin)

# Note that in the following, we explicitly pass in a subset of the contour
# levels used for the filled contours.  Alternatively, we could pass in
# additional levels to provide extra resolution, or leave out the *levels*
# keyword argument to use all of the original levels.

CS2 = ax2.contour(CS, levels=CS.levels[::2], colors='r', origin=origin)

ax2.set_title('Nonsense (3 masked regions)')
ax2.set_xlabel('word length anomaly')
ax2.set_ylabel('sentence length anomaly')

# Make a colorbar for the ContourSet returned by the contourf call.
cbar = fig1.colorbar(CS)
cbar.ax.set_ylabel('verbosity coefficient')
# Add the contour line levels to the colorbar
cbar.add_lines(CS2)
plt.show()

insert image description here

CS = ax2.contourf(X, Y, Z, 10, cmap=plt.cm.Accent, origin=origin)

insert image description here

CS = ax2.contourf(X, Y, Z, 10, cmap=plt.cm.Accent_r, origin=origin)

insert image description here

CS = ax2.contourf(X, Y, Z, 10, cmap=plt.cm.Blues, origin=origin)

insert image description here

CS = ax2.contourf(X, Y, Z, 10, cmap=plt.cm.Blues_r, origin=origin)

insert image description here

CS = ax2.contourf(X, Y, Z, 10, cmap=plt.cm.BrBG, origin=origin)

insert image description here

CS = ax2.contourf(X, Y, Z, 10, cmap=plt.cm.BrBG_r, origin=origin)

insert image description here

CS = ax2.contourf(X, Y, Z, 10, cmap=plt.cm.BuGn, origin=origin)

insert image description here

CS = ax2.contourf(X, Y, Z, 10, cmap=plt.cm.BuGn_r, origin=origin)

insert image description here

CS = ax2.contourf(X, Y, Z, 10, cmap=plt.cm.BuPu, origin=origin)

insert image description here

CS = ax2.contourf(X, Y, Z, 10, cmap=plt.cm.BuPu_r, origin=origin)

insert image description here

CS = ax2.contourf(X, Y, Z, 10, cmap=plt.cm.CMRmap, origin=origin)

insert image description here
References: https://matplotlib.org/stable/gallery/images_contours_and_fields/contourf_demo.html#sphx-glr-gallery-images-contours-and-fields-contourf-demo-pyDevelopment
tools: Visual Studio 2022 and WeChat Alt+ A screenshot tool

Guess you like

Origin blog.csdn.net/m0_38127487/article/details/132204578