14

I have a seaborn heatmap but i need to remove the axis tick marks that show as dashes. I want the tick labels but just need to remove the dash (-) at each tick on both axes. My current code is:

sns.heatmap(df, annot=True, fmt='.2f', center=0)

enter image description here

I tried despine and that didnt work.

deep_butter
  • 185
  • 1
  • 1
  • 7
  • 6
    `ax.tick_params(left=False, bottom=False)`? – ImportanceOfBeingErnest Apr 04 '19 at 02:52
  • that did the trick. I actually tried that before asking but I had entered the line before the sns.heatmap() line. Is there a reason why it doesn't work if plt.tick_params() is placed before sns.heatmap()?? – deep_butter Apr 04 '19 at 03:05
  • 2
    The problem is that before you call `sns.heatmap` there is no axes around for those parameters to apply to. Hence I would suggest to use `ax = sns.heatmap(..); ax.tick_param(..)` from which it is clear that you need an `ax` for it to work. – ImportanceOfBeingErnest Apr 04 '19 at 03:08

2 Answers2

10

@ImportanceOfBeingEarnest had a nice answer in the comments that I wanted to add as an answer (in case the comment gets deleted).

For a heatmap:

ax = sns.heatmap(df, annot=True, fmt='.2f', center=0)
ax.tick_params(left=False, bottom=False) ## other options are right and top

If this were instead a clustermap (like here How to remove x and y axis labels in a clustermap?), you'd have an extra call:

g = sns.clustermap(...)
g.ax_heatmap.tick_params(left=False, bottom=False)

And for anyone who wanders in here looking for the related task of removing tick labels, see this answer (https://stackoverflow.com/a/26428792/3407933).

spacetyper
  • 1,471
  • 18
  • 27
  • 1
    The answer above on ```clustermap``` did not work for me. Instead I used: ```g.ax_heatmap.tick_params(tick2On=False, labelsize=False)```. This did remove the ticks and their labels. – pr94 Aug 11 '20 at 10:01
2
ax = sns.heatmap(df, annot=True, fmt='.2f', center=0)
ax.tick_params(axis='both', which='both', length=0)

ax is a matplotlib.axes object. All axes parameters can be changed in this object, and here's an example from the matplotlib tutorial on how to change tick parameters. both selects both x and y axis, and then their length is changed to 0, so that the ticks are not visible anymore.

molexi
  • 530
  • 5
  • 13