0

I have the following code to create a heatmap with seaborn! unfourtunately my y-ticklabels are long names, and are cut off from the figure as shown in the attached image!

seasons = ['yearly','djf','mam','jja','son']
for season_index,season in enumerate(['yearly','djf','mam','jja','son']):
    im = sns.heatmap(bias[:,season_index,:],linewidth = 0.5,cmap='coolwarm', annot=True,annot_kws={'fontsize':7, 'fontweight':'bold'})
    im.set_xticklabels(['1','2','3','4','5','6','7','8','9'])
    im.set_yticklabels(model_list)
    plt.savefig(f"figures/bias/yearly_bias_{seasons[season_index]}.png")
    plt.close()

enter image description here

I tried changing the figure size with matplotlib, but everytime i did that, the tick labels would rotate 90° and overlap, and in my code it didn't work to reset it with the rotation keyword in the set_yticklabels function.

i also tried this but that had the same result in turning the labels.

Thank you!

  • Have you tried putting [`plt.tight_layout()`](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.tight_layout.html) before `plt.savefig()`? [Here](https://matplotlib.org/stable/gallery/images_contours_and_fields/image_annotated_heatmap.html#using-the-helper-function-code-style) could be more on your issue. – Nikita Shabankin Nov 02 '22 at 17:22

1 Answers1

1
plt.tight_layout()

this worked, thank you ver ymuch to Nikita Shabankin!