Python中的 Matplotlib.pyplot.pcolor()函数
Matplotlib 是用于数据可视化的著名Python包。 Numpy 是 Matplotlib 的数值数学扩展。 Matplotlib 能够生成高质量的图形、图表和图形。 Matplotlib 生成面向对象的 API,用于使用 Tkinter、wxPython 或 Qt 等 GUI 工具包将绘图嵌入到项目中。 John D. Hunter 是 Matplotlib 的最初开发者,它是在 BSD 风格的许可下分发的。
matplotlib.pyplot.pcolor()
Matplotlib 包含多种有助于执行不同任务的函数,其中之一是matplotlib.pyplot.pcolor()函数。 Matplotlib 库的 pyplot 模块中的pcolor()函数有助于创建带有非常规矩形网格的伪彩色图。
Syntax: matplotlib.pyplot.pcolor(*args, alpha=None, norm=None, cmap=None, vmin=None, vmax=None, data=None, **kwargs)
Call Signature: pcolor([X, Y,] C, **kwargs)
Parameters:
C: Denotes a scaler 2-D array
X, Y: array_like, optional, coordinates of quadrilateral corners
cmap: str or Colormap, optional
norm: Normalize, optional
vmin, vmax: scaler, optional
edgecolors: {‘none’, None, ‘face’, color sequence}, optional
alpha: scaler, optional
snap: bool, optional
Other Parameters:
antialiaseds: bool, optional
**kwargs
Returns: The function returns a collection i.e matplotlib.collections.Collection
注意:对于较大的数组, matplotlib.pyplot.pcolor() 的工作速度非常慢。
下面的例子演示了 matplotlib.pyplot.pcolor()函数的工作:
示例 1:使用 pcolor()函数生成图像
借助 pcolor()函数,我们可以生成二维图像样式的图,如下所示
Python3
# Demonstration of matplotlib function
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LogNorm
Z = np.random.rand(4, 12)
fig, (ax0, ax1) = plt.subplots(2, 1)
c = ax0.pcolor(Z)
ax0.set_title('No edge image')
c = ax1.pcolor(Z, edgecolors='k', linewidths=5)
ax1.set_title('Thick edges image')
fig.tight_layout()
plt.show()
Python3
# Demonstration of matplotlib function
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LogNorm
N = 100
X, Y = np.mgrid[-4:4:complex(0, N), -4:4:complex(0, N)]
# Image show that a low hump with a spike coming out.
# We need a z/colour axis on a log scale in order
# to watch both hump and spike.
Z1 = np.exp(-(X)**2 - (Y)**2)
Z2 = np.exp(-(X * 10)**2 - (Y * 10)**2)
Z = Z1 + 50 * Z2
fig, (ax0, ax1) = plt.subplots(2, 1)
c = ax0.pcolor(X, Y, Z,norm=LogNorm(vmin=Z.min(), vmax=Z.max()), cmap=plt.cm.autumn)
fig.colorbar(c, ax=ax0)
c = ax1.pcolor(X, Y, Z, cmap=plt.cm.autumn)
fig.colorbar(c, ax=ax1)
plt.show()
输出:
示例 2:使用对数刻度处理 pcolor()
蟒蛇3
# Demonstration of matplotlib function
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LogNorm
N = 100
X, Y = np.mgrid[-4:4:complex(0, N), -4:4:complex(0, N)]
# Image show that a low hump with a spike coming out.
# We need a z/colour axis on a log scale in order
# to watch both hump and spike.
Z1 = np.exp(-(X)**2 - (Y)**2)
Z2 = np.exp(-(X * 10)**2 - (Y * 10)**2)
Z = Z1 + 50 * Z2
fig, (ax0, ax1) = plt.subplots(2, 1)
c = ax0.pcolor(X, Y, Z,norm=LogNorm(vmin=Z.min(), vmax=Z.max()), cmap=plt.cm.autumn)
fig.colorbar(c, ax=ax0)
c = ax1.pcolor(X, Y, Z, cmap=plt.cm.autumn)
fig.colorbar(c, ax=ax1)
plt.show()
输出: