4

Hi everyone I have the following dataframe and I want to create heatmap from it with the following code.

My DataFrame

plt.figure(figsize=(10,10))
g = sns.heatmap(
    top_5_stations_hourly_total_traffic_by_time, 
    square=True,
    cbar_kws={'fraction' : 0.01},
    cmap='OrRd',
    linewidth=1
)

g.set_xticklabels(top_5_stations_hourly_total_traffic_by_time.STATION, rotation=45, horizontalalignment='right')
g.set_yticklabels(top_5_stations_hourly_total_traffic_by_time.TIME, rotation=45, horizontalalignment='right')

But it gives the following error :

ValueError: could not convert string to float: '125 ST'

Ugur Selim Ozen
  • 159
  • 3
  • 10
  • I converted station and time columns to categorical types but it gives same error. I want to plot for every station's , every unique time's hourly total traffic value. – Ugur Selim Ozen Nov 09 '21 at 15:59
  • You should have a look at this post : https://stackoverflow.com/questions/37790429/seaborn-heatmap-using-pandas-dataframe – Beinje Nov 09 '21 at 16:01

1 Answers1

3

pivot your data before calling heatmap:

df_heatmap = df.pivot("STATION", "TIME", "HOURLY_TOTAL_TRAFFIC")

>>> sns.heatmap(df_heatmap)

enter image description here

not_speshal
  • 22,093
  • 2
  • 15
  • 30