0

This is first time trying this. I actually have a dict of lists I am generating in a program, but since this is my first time ever trying this, I am using a dummy dict just for testing.

I am following this: python Making heatmap from DataFrame

but I am failing with the following:

Traceback (most recent call last):
  File "C:/Users/Mark/PycharmProjects/main/main.py", line 20, in <module>
    sns.heatmap(df, cmap='RdYlGn_r', linewidths=0.5, annot=True)
  File "C:\Users\Mark\AppData\Roaming\Python\Python36\site-packages\seaborn\matrix.py", line 517, in heatmap
    yticklabels, mask)
  File "C:\Users\Mark\AppData\Roaming\Python\Python36\site-packages\seaborn\matrix.py", line 168, in __init__
    cmap, center, robust)
  File "C:\Users\Mark\AppData\Roaming\Python\Python36\site-packages\seaborn\matrix.py", line 205, in _determine_cmap_params
    calc_data = plot_data.data[~np.isnan(plot_data.data)]
TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

My code:

import pandas as pd
import seaborn as sns


Index = ['key1', 'key2', 'key3', 'key4', 'key5']
Cols = ['A', 'B', 'C', 'D']

testdict = {
    "key1": [1, 2, 3, 4],
    "key2": [5, 6, 7, 8],
    "key3": [9, 10, 11, 12],
    "key4": [13, 14, 15, 16],
    "key5": [17, 18, 19, 20]
}

df = pd.DataFrame(testdict, index=Index, columns=Cols)
df = df.transpose()

sns.heatmap(df, cmap='RdYlGn_r', linewidths=0.5, annot=True)
AChampion
  • 29,683
  • 4
  • 59
  • 75
MarkS
  • 1,455
  • 2
  • 21
  • 36

1 Answers1

0

You need to switch your column and index labels

Cols = ['key1', 'key2', 'key3', 'key4', 'key5']
Index = ['A', 'B', 'C', 'D']
Jan K
  • 4,040
  • 1
  • 15
  • 16
  • Pardon the silly question: Your answer made the error go away, but nothing gets displayed. The program simply finished with error code 0. How do I actually display the plot? And also, is there a way to save the plot to a file? .png or something else? Thanks. – MarkS Apr 24 '18 at 18:30
  • write plt.show() if running a script or if in jupyter notebook start the cell with "%matplotlib inline". Regarding saving plt.savefig('name.png') will do the job. If still lost, google matplotlib – Jan K Apr 24 '18 at 18:31
  • Much appreciated. – MarkS Apr 24 '18 at 18:38