Home > Net >  When creating a seaborn heatmap 'could not convert string to float' ValueError
When creating a seaborn heatmap 'could not convert string to float' ValueError

Time:11-10

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'

CodePudding user response:

pivot your data before calling heatmap:

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

>>> sns.heatmap(df_heatmap)

enter image description here

  • Related