Reputation: 1272
I am using matplotlib 3.0.3
and want to create an animation of image plots using the FuncAnimation
module. For plotting speed, I update the image data using im.set_data
for imshow
and im.set_array()
for plt.pcolormesh
(set_data
is not available as an attribute). If I update the data partially with NaN values, imshow
displays them as blank pixels, while pcolormesh
shows them as the lowest color from the colormap (blue for viridis
).
Is this intended and if not, why is this the behavior? It seems related to set_array()
, since pcolormesh
normally does plot NaN as blank pixels.
Minimal example:
import numpy as np
import matplotlib.pyplot as plt
fig1 = plt.figure()
im1 = plt.pcolormesh(np.random.rand(10,10))
im1.set_array((np.zeros((10,10)) * np.nan).ravel())
fig2 = plt.figure()
im2 = plt.imshow(np.random.rand(10,10))
im2.set_data(np.zeros((10,10)) * np.nan)
Upvotes: 0
Views: 2165
Reputation: 339052
Matplotlib images do not work well with nan
s. One should instead use masked arrays. Then both cases are the same (except for the need to flatten the pcolormesh array).
import numpy as np
import matplotlib.pyplot as plt
A = np.random.rand(10,10)
B = np.ma.array(A, mask=np.ones((10,10)))
fig1 = plt.figure()
im1 = plt.pcolormesh(A)
im1.set_array(B.ravel())
plt.colorbar()
fig2 = plt.figure()
im2 = plt.imshow(A)
im2.set_array(B)
plt.colorbar()
plt.show()
Upvotes: 0