question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Heatmaps can have too long numeric labels

See original GitHub issue

Often times, I have heatmaps like this:

screen shot 2016-09-05 at 9 07 19 pm

I’d like for the numeric labels to be shorter. I eventually found a solution through this SO answer after looking at the source for keywords to google. This solution is nowhere as clean as I’d like.

d = [{'p1': p1, 'p2': p2, 'error': np.random.rand()}
     for p1 in np.logspace(0, 1, num=5)
     for p2 in np.logspace(0, 3, num=5)]

df = pd.DataFrame(d)
df = df.pivot(values='error', index='p1', columns='p2')

ax = sns.heatmap(df)

# format text labels
fmt = '{:0.2f}'
xticklabels = []
for item in ax.get_xticklabels():
    item.set_text(fmt.format(float(item.get_text())))
    xticklabels += [item]
yticklabels = []
for item in ax.get_yticklabels():
    item.set_text(fmt.format(float(item.get_text())))
    yticklabels += [item]

ax.set_xticklabels(xticklabels)
ax.set_yticklabels(yticklabels)

plt.show()

…which produces this plot:

screen shot 2016-09-05 at 9 50 01 pm

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
FlorianWilhelmcommented, Sep 17, 2018

@stsievert to really make this work with seaborn I had to overwrite the current FixedFormatter like

from matplotlib.ticker import FixedFormatter

ax = sns.heatmap(heat_data)
x_format = ax.xaxis.get_major_formatter()
x_format.seq = ["{:0.0f}".format(float(s)) for s in x_format.seq]
y_format = ax.yaxis.get_major_formatter()
y_format.seq = ["{:0.0f}".format(float(s)) for s in y_format.seq]
ax.xaxis.set_major_formatter(x_format)
ax.yaxis.set_major_formatter(y_format)

and then it worked.

4reactions
mwaskomcommented, Sep 6, 2016

There’s a number of ways to control this sort of thing. The matplotlib solution would be to use a tick formatter. Or you could do


ax = sns.heatmap(df,
                 xticklabels=df.columns.values.round(2),
                 yticklabels=df.index.values.round(2))
Read more comments on GitHub >

github_iconTop Results From Across the Web

Round decimal places seaborn heatmap labels - Stack Overflow
I have a heat map that gets its labels ...
Read more >
A Complete Guide to Heatmaps | Tutorial by Chartio
Heatmaps take the form of a grid of colored squares, where colors correspond with cell value. This article will show you how to...
Read more >
How to include labels in sns heatmap
I got your problem like this way: You want to show labels on the x and y-axis on the seaborn heatmap. So for...
Read more >
How can I modify the X and Y axis tick labels for a Heatmap ...
Direct link to this answer​​ I understand that you are trying to change the displayed tick labels on the X and Y axis...
Read more >
Using heat maps - Amazon QuickSight - AWS Documentation
Work with the heat map visual type in Amazon QuickSight. ... Heat maps can also be used to show the count of values...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found