Use pcolor
or pcolormesh
. pcolormesh
速度要快得多,但仅限于直线网格,而 pcolor 可以处理任意形状的单元格。 specgram
uses pcolormesh
,如果我没记错的话。(它使用imshow
.)
举个简单的例子:
import numpy as np
import matplotlib.pyplot as plt
z = np.random.random((11,11))
x, y = np.mgrid[:11, :11]
fig, ax = plt.subplots()
ax.set_yscale('symlog')
ax.pcolormesh(x, y, z)
plt.show()
您看到的差异是由于绘制了“原始”值而导致的specgram
返回。什么specgram
实际上绘图是一个缩放版本。
import matplotlib.pyplot as plt
import numpy as np
x = np.cumsum(np.random.random(1000) - 0.5)
fig, (ax1, ax2) = plt.subplots(nrows=2)
data, freqs, bins, im = ax1.specgram(x)
ax1.axis('tight')
# "specgram" actually plots 10 * log10(data)...
ax2.pcolormesh(bins, freqs, 10 * np.log10(data))
ax2.axis('tight')
plt.show()
请注意,当我们使用以下方式绘制事物时pcolormesh
,没有插值。 (这是要点的一部分pcolormesh
--它只是矢量矩形而不是图像。)
如果你想要对数刻度的东西,你可以使用pcolormesh
用它:
import matplotlib.pyplot as plt
import numpy as np
x = np.cumsum(np.random.random(1000) - 0.5)
fig, (ax1, ax2) = plt.subplots(nrows=2)
data, freqs, bins, im = ax1.specgram(x)
ax1.axis('tight')
# We need to explictly set the linear threshold in this case...
# Ideally you should calculate this from your bin size...
ax2.set_yscale('symlog', linthreshy=0.01)
ax2.pcolormesh(bins, freqs, 10 * np.log10(data))
ax2.axis('tight')
plt.show()