A far easier way to do this is to send an empty array []
to set_array()
.
I don't really know why, but I've seen it done in this answer, and it works.
The only thing I know is that before you set the array, you get None
from get_array()
, which is probably why you get this error.
You can do :
plt.clf()
fig, ax = plt.subplots(1,1)
# Set color mappable
range_min = df.col1.min()
range_max = df.col1.max()
cmap = matplotlib.cm.ScalarMappable(
norm = mcolors.Normalize(range_min, range_max),
cmap = plt.get_cmap('binary'))
for i in polygonDict.keys():
ax.add_patch(ds.PolygonPatch(polygonDict[i], fc = cmap.to_rgba(df.col1.loc[i])))
cmap.set_array([]) # or alternatively cmap._A = []
fig.colorbar(cmap, ax = ax)
After a few tests, it seems that you can send any array in the world ([df.col1], [0,1], ['hello']) to set_array()
, and your colorbar will be the one you want. It just can't be None
.
I've noticed that if you set the array to an number array, and then call autoscale()
, the min and max values of the colorbar will be the min and max of that array.
I hope this helps.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…