I am trying to update the color of the faces of a pcolormesh using set_facecolor. The purpose of this operation is to "bleach" (emulate transparency by whitening the facecolor) some regions of the plot -- see this post. Although I am pretty sure I have been getting this to work in the past, for some reason it does not work anymore in a jupyter lab or jupyter notebook environment (not tested in a script yet). Unexpectedly, calling fig.canvas.draw() seems to restore the initial face colors. Here is a minimal example reproducing the error:
from matplotlib import pyplot as plt
import numpy as np
## Generate data and show initial plot
xx = np.linspace(-1, 1, 5)
xx, yy = np.meshgrid(xx, xx)
data = np.exp( -(xx**2+yy**2) )
### Generate plot
fig, ax = plt.subplots(1, 1)
hpc = ax.pcolormesh(data)
plt.colorbar(hpc)
ax.set_aspect(1)
fig.canvas.draw() # this is required to generate the facecolor array
colors = hpc.get_facecolors() # for checking: this contains the RGBA array
newc = np.ones((colors.shape[0], 3))/2. # uniform grey
hpc.set_facecolor(newc) # this should update the facecolor of the plot
newc = hpc.get_facecolors() # indeed, the facecolor seems to have been updated
fig.canvas.draw() # re-generating the plot
### On my machine: the plot is unchanged and the new facecolors are back to the initial values
print(newc[0,:], hpc.get_facecolors()[0,:])
Configuration:
- backend:
%matplotlib inlineor%matplotlib widgetboth produce the error - python 3.9.13 (macOS Big Sur) or python 3.8.11 (Linux on a cluster -- maybe Fedora)
- matplotlib 3.5.1 or 3.5.2 (resp), jupyterlab 3.4.4 or 3.4.0 (resp.)
Does anyone have an idea of what is going on? Am I doing something wrong? Thanks a lot in advance