Search code examples
pythonmatplotlibpycharmheatmapcolormap

plt.cm.RdBu ( Cannot find reference in cm.py )


The following code snippet is supposed to show a heatmap of a dataset, like the first image:

colormap = plt.cm.RdBu
plt.figure(figsize=(18, 15))
plt.title('Pearson Correlation of Features', y=1.05, size=50)
sns.heatmap(df.corr(), linewidths=0.1, vmax=1.0, square=True, cmap=colormap, linecolor='white', annot=True)
plt.show()

enter image description here

However, my RdBu is highlighted in PyCharm and the warning message says:

Cannot find reference 'RdBu' in 'cm.py'

There are a few posts that basically show the syntax is correct and I am not missing any packages. What am I doing wrong here that I get this empty map instead?

enter image description here


Solution

  • I was missing the following code before the graph code:

    for c in df.columns:
        if df[c].dtype == 'object':
            lbl = LabelEncoder()
            lbl.fit(list(df[c].values))
            df[c] = lbl.transform(list(df[c].values))