0

I'm new to Python and fairly new to seaborn.

I have a pandas dataframe named df which looks like:

TIMESTAMP ACT_TIME_AERATEUR_1_F1 ACT_TIME_AERATEUR_1_F2 ACT_TIME_AERATEUR_1_F3 ACT_TIME_AERATEUR_1_F4 ACT_TIME_AERATEUR_1_F5 ACT_TIME_AERATEUR_1_F6 
2015-08-01 23:00:00 80 0 0 0 10 0
2015-08-01 23:20:00 60 0 20 0 10 10
2015-08-01 23:40:00 80 10 0 0 10 10
2015-08-01 00:00:00 60 10 20 40 10 10


df.info()

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 38840 entries, 0 to 38839
Data columns (total 7 columns):
TIMESTAMP                 38840 non-null datetime64[ns]
ACT_TIME_AERATEUR_1_F1    38696 non-null float64
ACT_TIME_AERATEUR_1_F3    38697 non-null float64
ACT_TIME_AERATEUR_1_F5    38695 non-null float64
ACT_TIME_AERATEUR_1_F6    38695 non-null float64
ACT_TIME_AERATEUR_1_F7    38693 non-null float64
ACT_TIME_AERATEUR_1_F8    38696 non-null float64
dtypes: datetime64[ns](1), float64(6)
memory usage: 2.1 MB

I try to do a heatmap using this code :

data = sns.load_dataset("df")

# Draw a heatmap with the numeric values in each cell
sns.heatmap(data, annot=True, fmt="d", linewidths=.5)

But it does not work Can you help me pelase to find the error?

Thanks

Edit First , I load dataframe from csv file :

df1 = pd.read_csv('C:/Users/Demonstrator/Downloads/Listeequipement.csv',delimiter=';', parse_dates=[0], infer_datetime_format = True)

Then, I select only rows which date '2015-08-01 23:10:00' and '2015-08-02 00:00:00'

    import seaborn as sns
    df1['TIMESTAMP']= pd.to_datetime(df1_no_missing['TIMESTAMP'], '%d-%m-%y %H:%M:%S')
    df1['date'] = df_no_missing['TIMESTAMP'].dt.date
    df1['time'] = df_no_missing['TIMESTAMP'].dt.time
    date_debut = pd.to_datetime('2015-08-01 23:10:00')
    date_fin = pd.to_datetime('2015-08-02 00:00:00')
    df1 = df1[(df1['TIMESTAMP'] >= date_debut) & (df1['TIMESTAMP'] < date_fin)]

Then, construct the heatmap :
sns.heatmap(df1.iloc[:,2:],annot=True, fmt="d", linewidths=.5)

I get this error :

TypeError                                 Traceback (most recent call last)
<ipython-input-363-a054889ebec3> in <module>()
      7 df1 = df1[(df1['TIMESTAMP'] >= date_debut) & (df1['TIMESTAMP'] < date_fin)]
      8 
----> 9 sns.heatmap(df1.iloc[:,2:],annot=True, fmt="d", linewidths=.5)

C:\Users\Demonstrator\Anaconda3\lib\site-packages\seaborn\matrix.py in

heatmap(data, vmin, vmax, cmap, center, robust, annot, fmt, annot_kws, linewidths, linecolor, cbar, cbar_kws, cbar_ax, square, ax, xticklabels, yticklabels, mask, **kwargs) 483 plotter = _HeatMapper(data, vmin, vmax, cmap, center, robust, annot, fmt, 484 annot_kws, cbar, cbar_kws, xticklabels, --> 485 yticklabels, mask) 486 487 # Add the pcolormesh kwargs here

C:\Users\Demonstrator\Anaconda3\lib\site-packages\seaborn\matrix.py in

init(self, data, vmin, vmax, cmap, center, robust, annot, fmt, annot_kws, cbar, cbar_kws, xticklabels, yticklabels, mask) 165 # Determine good default values for the colormapping 166 self._determine_cmap_params(plot_data, vmin, vmax, --> 167 cmap, center, robust) 168 169 # Sort out the annotations

C:\Users\Demonstrator\Anaconda3\lib\site-packages\seaborn\matrix.py in

_determine_cmap_params(self, plot_data, vmin, vmax, cmap, center, robust) 202 cmap, center, robust): 203 """Use some heuristics to set good defaults for colorbar and range.""" --> 204 calc_data = plot_data.data[~np.isnan(plot_data.data)] 205 if vmin is None: 206 vmin = np.percentile(calc_data, 2) if robust else calc_data.min()

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''

Bio
  • 1,503
  • 3
  • 12
  • 15

2 Answers2

0

Remove the timestamp variables(i.e. first two columns) before passing it to sns.heatmap, no need for the load dataset as well, just use:

sns.heatmap(df.iloc[:,2:],annot=True, fmt="d", linewidths=.5)

EDIT

Ok here is your dataframe, just changed the column names in the interest of time

df
Out[9]: 
           v1        v2  v3  v4  v5  v6  v7  v8
0  2015-08-01  23:00:00  80   0   0   0  10   0
1  2015-08-01  23:20:00  60   0  20   0  10  10
2  2015-08-01  23:40:00  80  10   0   0  10  10
3  2015-08-01  00:00:00  60  10  20  40  10  10

Now seaborn cannot recognize timestamp variables for the heatmap right, so we will remove the first two columns and pass the dataframe to seaborn

import seaborn as sns
sns.heatmap(df.iloc[:,2:],annot=True, fmt="d", linewidths=.5)

So we get the result as

Output from seaborn

If you don't get the result by using this, please edit your question to include rest of your code. This is not the problem then.

Gaurav Dhama
  • 1,346
  • 8
  • 19
  • I have a problem with this line data = sns.load_dataset("df") . Ive got a strange error : HTTPError Traceback (most recent call last) in () 11 df1_no_missing = df1_no_missing[(df1_no_missing['TIMESTAMP'] >= date_debut) & (df_no_missing['TIMESTAMP'] < date_fin)] 12 df1_no_missing ---> 13 data = sns.load_dataset("df1_no_missing") 14 #sns.heatmap(data.iloc[:,2:],annot=True, fmt="d", linewidths=.5) 15 – Bio Aug 05 '16 at 13:54
  • You dont need that line bro, just write a single line which i have mentioned in my answer. `load_dataset` looks for online csv files on https://github.com/mwaskom/seaborn-data. Look for a detailed explnation on this thread :http://stackoverflow.com/questions/30336324/seaborn-load-dataset – Gaurav Dhama Aug 05 '16 at 14:00
  • Ok so you mean that I replace data by df , here : sns.heatmap(data.iloc[:,2:],annot=True, fmt="d", linewidths=.5) ? – Bio Aug 05 '16 at 14:06
  • 1
    Yes Sir, that's how to do it!! – Gaurav Dhama Aug 05 '16 at 14:19
  • I edit my first post, to explain my error. Thank you :) – Bio Aug 05 '16 at 17:03
  • Please understand that seaborn can only take numeric variables not datetime values. Take a print of your dataset, take a look at what columns are numeric and then construct the heatmap, it will work fine – Gaurav Dhama Aug 05 '16 at 17:14
  • Yes I know, but normally when I use : sns.heatmap(df1.iloc[:,2:],annot=True, fmt="d", linewidths=.5), it must dont care about the two first column not? – Bio Aug 05 '16 at 17:17
  • But you are creating new columns too like date and time after importing the file – Gaurav Dhama Aug 05 '16 at 20:12
0

Since you don't allocate the timestamp as an index. The row names are the index. Do this :

df1.set_index("TIMESTAMP", inplace=1)

Another fix of this problem is :

ax = sns.heatmap(df1.iloc[:, 1:6:], annot=True, linewidths=.5)
ax.set_yticklabels([i.strftime("%Y-%m-%d %H:%M:%S") for i in df1.TIMESTAMP], rotation=0)
vegetarianCoder
  • 2,762
  • 2
  • 16
  • 27