Home > Enterprise >  Python heatmap Xaxis shifted down after arranging Yaxis
Python heatmap Xaxis shifted down after arranging Yaxis

Time:03-16

I created a heatmap and I arranged the y axis so each hour will be in the edge of a square. It shifted the entire xaxis down, how can I upper it so it the ticks and labels of xaxis will be of the heatmap like the yaxis. This is the code:

The making of the heatmap "frequencies of the days and hours- 2 hours resolution"

day_short_names = ['Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat', 'Sun']  #X axis 
hoursformat=['24:00','22:00','20:00','18:00','16:00','14:00','12:00','10:00','8:00','6:00','4:00','2:00','00:00'] #Y axis
pypl.figure(figsize = (20,15))
sb.heatmap(data = final, annot = True, cmap = 'mako', fmt = '000', xticklabels=day_short_names,yticklabels=hoursformat) #Formation of heatmap
pypl.yticks(nmp.arange(13),labels=hoursformat, va='center')
pypl.title('Listening Distribution to Spotify by Weekdays and Hours', fontsize = 25) #Title of heatmap
pypl.xlabel('Days of Week', fontsize = 15) #X axis title
pypl.ylabel('Hours (resolution of 2 hours)',fontsize = 15) #Y axis title
pypl.show() #showing the histogram graph

And this is the heatmap:

enter image description here

CodePudding user response:

I am not expert enough to explain what happened, but I suspect it was because the y-axis ticks were set in the heatmap and then the y-axis ticks were set again. So I think we can remove the tick settings in the heatmap and add a new y-axis and x-axis.

sns.heatmap(data=final, annot=True, cmap='mako', fmt='000',cbar_kws={'shrink':0.95})
plt.yticks(np.arange(13), labels=hoursformat, va='center')
plt.xticks(np.arange(7) 0.5, labels=day_short_names, va='center')

enter image description here

  • Related