How to display ticks in plain number for seaborn heatmap with logarithmic scale?

被刻印的时光 ゝ 提交于 2021-02-11 14:41:20

问题


I am generating a heatmap using seaborn which has a logarithmic scale. How can I change the colorbar labels from scientific notation to plain number.

import math
from matplotlib.colors import LogNorm
vmax=2
vmin=0.5
center = (vmax+vmin)/2
log_norm = LogNorm(vmin=vmin, vmax=vmin)
cbar_ticks = [0.5, 0.66, 1, 1.5, 2]
ax = sns.heatmap(corr, square=True, mask=mask, cmap=cmap_type, linewidths=.5, vmax=vmax, vmin=vmin, norm=log_norm, cbar_kws={"ticks": cbar_ticks}, center=center)

Edit:

The following code fixes the problem with scientific notation:

    import matplotlib.ticker as tkr
    formatter = tkr.ScalarFormatter(useMathText=True)
    formatter.set_scientific(False)
    cbar_kws={"ticks": cbar_ticks, "format": formatter}

but norm=log_norm is adding additional ticks (Please see image below). How do I remove these?

Updated graph


回答1:


seaborn plots are wrappers around matplotlib so I would suggest this

ax.ticklabel_format(style='plain')



回答2:


The following codes helped.

For the first part of the problem adding "format" in cbar_kws parameters helped. (https://stackoverflow.com/a/35419495/8881141):

import matplotlib.ticker as tkr
formatter = tkr.ScalarFormatter(useMathText=True)
formatter.set_scientific(False)

ax = sns.heatmap(corr, square=True, mask=mask, cmap=cmap_type, linewidths=.5, vmax=vmax, vmin=vmin, norm=log_norm, cbar_kws={"ticks": cbar_ticks, "format": formatter}, center=center)

For the second part, which is caused due to a bug in matplotlib, adding the following removes the minor ticks. (https://stackoverflow.com/a/65676007/11897903):

ax.collections[0].colorbar.ax.yaxis.set_ticks([], minor=True)


来源:https://stackoverflow.com/questions/65673391/how-to-display-ticks-in-plain-number-for-seaborn-heatmap-with-logarithmic-scale

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!