0

Im doing a heatmap from a pandas dataframe with matplotlib. Everything looks good, only I have too much padding on the top of the heatmap and I don't know how to change it: heatmap with too much padding on the top

The code I am currently using:

plt.figure(num=1,figsize=(10,10))
plt.pcolor(table, cmap='Reds', vmin=0, vmax=5, edgecolors="black")
plt.tight_layout()
plt.yticks(np.arange(0.5, len(table.index), 1), table.index)
plt.xticks(np.arange(0.5, len(table.columns), 1), table.columns, rotation=45)
plt.savefig('test.png', bbox_inches='tight')
plt.show()

Is that because of yticks are strings and matplotlib is trying to make space in case I want to rotate them?

Thanks! Regars!

1

1 Answer 1

1

Matplotlib tries to find a nice round number for the axis limits (e.g. 45 instead of 41) to make the plot look nicer with automated ticks. Redefining the ticks doesn't change the limits. You can manually force them to match your data exactly:

plt.gca().set_ylim((0, len(table.index)))
plt.gca().set_xlim((0, len(table.columns)))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.