I have the following dataframe:
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
data = {'Item': [19, 92],
'16': [None, 1],
'56': [1, None],
'70': [1, None]}
df = pd.DataFrame(data).set_index('Item')
that I use to get a heatmap plot:
fig, ax = plt.subplots(figsize=(20,10))
sns.heatmap(df, cmap="Reds", annot=True, cbar=False, linewidth=1, ax=ax)
plt.show()
This works, but few days ago I have updated my environment and now I get the following message:
FutureWarning: Format strings passed to MaskedConstant are ignored, but in future may error or produce different behavior
annotation = ("{:" + self.fmt + "}").format(val)
I tried to explicitly pass the format parameter fmt='.2g' and I even tried to define a mask
mask = df.isnull()
to get rid of the None values, thus by using the line:
sns.heatmap(df, cmap="Reds", mask=mask, annot=True, fmt='.2g', cbar=False, linewidth=1, ax=ax)
but the FutureWarning message is not being removed.
Finally I understood how to address this FutureWarning message, preventing any issues with the MaskedConstant class and its formatting behavior. The trick is to use a separate set of labels for the annotation parameter, in the following way: just create the labels by mapping the dataframe
labels = df.map(lambda v: v if v else '')
and then pass these labels into the annot= parameter:
sns.heatmap(df, cmap="Reds", annot=labels, cbar=False, linewidth=1)
Hope this can be usefull to someone else.