0

Afternoon All,

I'm looking to create a heatmap of one column in my df. So inc_Year_Month on y axis and Count on the x axis

import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap

data = {
'inc_Year_Month':['2020-07','2020-08','2020-09','2020-10','2020-11','2020-12','2021-01','2021-02','2021-03','2021-04'],
'Count':[2,4,6,8,10,12,30,50,60,100],
}
dfTemp = pd.DataFrame(dict(data))
print(dfTemp)

dfTemp2 = np.asarray(dfTemp).reshape(dfTemp.shape[0],1)
df_heatmap = sns.heatmap(dfTemp2, annot=True, fmt="g", cmap='magma')
# df_heatmap = sns.heatmap(dfTemp2,cmap=ListedColormap(['green', 'yellow', 'red']))
df_heatmap_Figure = df_heatmap.get_figure()
# # Set to high resolution and save
df_heatmap_Figure.savefig('Hi Res Seaborn-heatmap2.png', dpi=300) 

I get the error:

ValueError: cannot reshape array of size 20 into shape (10,1)

I thought I could resolve this by forcing the rectangular 2D dataset into an ndarray:

dfTemp2 = np.asarray(dfTemp).reshape(dfTemp.shape[0],1)
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Peter Lucas
  • 1,979
  • 1
  • 16
  • 27
  • I was trying to achieve what was shown here: https://stackoverflow.com/questions/47585775/seaborn-heatmap-with-single-column – Peter Lucas Jul 18 '22 at 05:53
  • Then set the `'inc_Year_Month'` column as the index of the dataframe. `df = df.set_index('inc_Year_Month')`, and then `ax = sns.heatmap(data=df, annot=True, fmt="g", cmap='viridis')`. [code and plot](https://i.stack.imgur.com/MQogV.png) – Trenton McKinney Jul 18 '22 at 06:00
  • 1
    Thanks @Trenton McKinney, working wll now! – Peter Lucas Jul 18 '22 at 07:17

0 Answers0