数组的形状存储在私有属性_meshWidth
和中_meshHeight
。但是,由于这些属性不是公共API的一部分,因此,如果可能的话,保存原始数据的形状要好于依赖这些属性。
import matplotlib.pyplot as plt
import numpy as np
D = np.random.uniform(0, 100, size=(5, 5))
fig, ax = plt.subplots()
h, w = D.shape
img = ax.pcolormesh( np.arange(h+1), np.arange(w+1), D)
D2 = img.get_array().reshape(img._meshWidth, img._meshHeight)
assert np.array_equal(D, D2)
还需要注意的是,如果你想恢复原来的数组D
,然后将坐标数组,np.arange(h+1)
,np.arange(w+1)
必须有一个长度比的形状更大D
。否则,当具有shape时,img.get_array()
返回一个shape数组。(499, 499)``D``(500, 500)