How to plot Heat map in python?

datavisualization
ipython
python

#1

Hi,

I am new to python. Recently I have started working on a project where I need to show the relation of target variable with weekday and hour. Now to show this relationship, I want to go with Heat Map. I have grouped the dataframe using groupby command at hour and weekday level. Can you suggest me the way to plot heatmap in python?

Should I go with matplotlib or seaborn?

Thanks,
Mukesh


#2

Hi Mukesh,

Recently I have worked in Kaggle Taxi challenge and have similar variable like you have (Hour and Weekday as cday). I have also used heat map to visualize it.

First, I read the dataset

df_train=pd.read_csv("/media/sunil/DATAPART/Rough/18-Jun-15/Taxi/train/train.csv", converters={'POLYLINE': lambda x: json.loads(x)[-1:]})

After Generating datetime variable, used groupby statement to group count of Trips at Hour and weekday level

temp1 = df_train.groupby(['cday','hour']).TAXI_ID.count()

Finallly, plot heatmap using matplotlib

import matplotlib.pyplot as plt
import numpy as np

# Generate Data
data=temp1.values
test=data.reshape(7,24)
#data = np.random.rand(7,24)
rows = df_train['hour'].unique()
columns = df_train['cday'].unique()

plt.pcolor(test,cmap=plt.cm.Reds)

plt.show()
plt.close()

Regards,
Imran