Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inconsistent shape between the condition and the input while using seaborn

I am trying to plot a heatmap with seaborn. Here is the list that I am trying to plot:

b = [5, 4, 4, 4, 13, 4, 4, 1, 9, 4, 3, 9, 1, 4, 4, 1, 7, 1, 5, 3, 7, 1, 9, 4, 3, 9, 5, 4, 2, 1, 4, 1, 9, 4, 3, 9, 4, 8, 1, 7, 1, 9, 4, 8, 1, 7, 1, 4, 8, 1, 7, 1, 4, 1, 7, 1, 4, 10, 4, 3, 4, 7, 1, 8, 5, 10, 8, 9, 4, 1, 3, 9, 4, 1, 9, 4, 3, 7, 7, 1, 1, 3, 4, 9, 5, 5, 4, 1, 1, 9, 4, 9, 4, 7, 1, 9, 4, 10, 9, 4, 4, 4, 8, 10, 3, 9, 5, 4, 4, 1, 3, 9, 4, 10, 5, 4, 1, 1, 8, 1, 7, 5, 1, 8, 8, 5, 3, 1, 8, 8, 8, 1, 3, 4, 2, 1, 2, 9, 4, 10, 1, 5, 3, 9, 5, 4, 4, 4, 1, 1, 7, 1, 8, 2, 1, 8, 5, 9, 5, 10, 9, 5, 4, 1, 10, 7, 1, 8, 5, 2, 1, 3, 4, 7, 1, 2, 1, 7, 1, 4, 4, 8, 5, 3, 7, 1, 2, 1, 10, 9, 4, 1, 2, 1, 3, 9, 4, 10, 9, 1, 9, 5, 4, 3, 9, 4, 1, 8, 5, 9, 4, 1, 1, 3, 9, 4, 9, 5, 4, 1, 1, 9, 4, 3, 4, 10, 1, 9, 4, 3, 4, 10, 7, 1, 7, 1, 9, 4, 3, 4, 4, 1, 1, 9, 5, 4, 3, 5, 4, 1, 8, 5, 7, 1, 3, 9, 4, 10, 9, 4, 9, 1, 8, 5, 3, 9, 4, 1, 3, 9, 5, 3, 9, 4, 1, 3, 4, 4, 4, 8, 8, 3, 9, 5, 4, 3, 5, 4, 10, 4, 7, 1, 5, 7, 1, 1, 3, 9, 4, 10, 4, 4, 1, 9, 4, 1, 1, 5, 4, 4, 3, 5, 4, 3, 5, 4, 3, 4, 4, 1, 1, 2, 1, 4, 3, 5, 4, 3, 4, 4, 4, 4, 4, 1, 1, 8, 8, 9, 5, 8, 4, 7, 1, 2, 4, 3, 9, 4, 10, 1, 1, 3, 4, 7, 1, 4, 1, 1, 8, 5, 3, 9, 5, 4, 10, 8, 3, 4, 4, 1, 1, 3, 9, 4, 2, 5, 5, 4, 4, 1, 1, 2, 1, 7, 3, 4, 9, 1, 4, 10, 9, 4, 9, 6, 4, 11, 5, 4, 10, 4, 4, 1, 9, 5, 4, 3, 9, 4, 3, 9, 5, 12, 4, 4, 4, 1, 1, 3, 9, 5, 4, 1, 3, 5, 4, 4, 4, 10, 1, 4, 4, 10, 4, 1, 5, 3, 5, 4, 4, 7, 1, 8, 4, 1, 2, 1, 9, 4, 3, 7, 1, 9, 5, 4, 4, 10, 9, 5, 4, 4, 3, 5, 10, 5, 4, 4, 1, 9, 4, 7, 1, 5, 3, 1, 4, 3, 4, 5, 3, 1, 5, 4, 5, 3, 4, 10, 8, 5, 3, 9, 4, 3, 4, 3, 7, 9, 1, 9, 4, 4, 3, 9, 4, 4, 4, 8, 9, 4, 3, 9, 5, 4, 4, 2, 5, 4, 1, 8, 3, 9, 4, 4, 10, 7, 1, 1, 9, 4, 3, 4, 9, 4, 1, 2, 1, 10, 1, 9, 4, 2, 1, 4, 1, 8, 5, 4, 3, 9, 4, 1, 9, 4, 3, 9, 3, 9, 4, 1, 4, 4, 1, 7, 7, 1, 2, 1, 3, 4, 2, 1, 4, 10, 1, 7, 1, 3, 7, 1, 11, 1, 3, 9, 4, 1, 9, 4, 7, 1, 1, 4, 2, 1, 9, 4, 3, 4, 1, 8, 1, 9, 4, 3, 4, 1, 8, 4, 1, 7, 7, 1, 7, 1, 4, 3, 9, 5, 4, 7, 1, 8, 9, 5, 4, 7, 1, 3, 9, 4, 3, 4, 7, 1, 1]

This is the code that I am trying to use to plot:

import numpy as np
import seaborn as sns
from matplotlib.colors import ListedColormap
data = np.asarray(b)
sns.heatmap( data,cmap=ListedColormap(['green', 'yellow', 'red']))

After running the above code, this is the error I am getting:

IndexError: Inconsistent shape between the condition and the input (got (633, 1) and (633,))

I did check some other answers but none of them answered my concerns.

I am not quite sure as to where the problem lies. Here is the result that I get when run data.shape:

(633,)

Any help will be appreciated. Thanks!

like image 327
Shawn Avatar asked Sep 11 '19 12:09

Shawn


2 Answers

import numpy as np
import seaborn as sns
from matplotlib.colors import ListedColormap
import matplotlib.pyplot as plt
data = np.asarray(b).reshape(633,1)
sns.heatmap(data,cmap=ListedColormap(['green', 'yellow', 'red']))
plt.show()

heatmap requires 2D dataset https://seaborn.pydata.org/generated/seaborn.heatmap.html

like image 62
Riddam Jain Avatar answered Oct 13 '22 10:10

Riddam Jain


From the docs:

data : rectangular dataset 2D dataset that can be coerced into an ndarray. If a Pandas DataFrame is provided, the index/column information will be used to label the columns and rows.

You have to transform your data into a 2D dataset.

One way to do so is:

sns.heatmap(data[:, np.newaxis], cmap=ListedColormap(['green', 'yellow', 'red']))
like image 34
iDrwish Avatar answered Oct 13 '22 10:10

iDrwish