本文介绍了如何在海底热图标签中使用科学计数法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用seaborn在python中获取热图.不幸的是,即使数量很大,它也没有使用科学计数法.我想知道是否有任何简单的方法可以转换为科学计数法或任何其他合理的格式.这是一段显示问题的代码:

I'm trying to get a heatmap using seaborn in python. Unfortunately it is not using scientific notation even though the numbers are very large. I was wondering if there's any simple way to convert to scientific notation or any other reasonable format. Here's a piece of code that shows the problem:

import seaborn as sns
import numpy as np
C_vals = np.logspace(3, 10, 8)
g_vals = np.logspace(-6, 2, 9)
score = np.random.rand(len(g_vals), len(C_vals))
sns.heatmap(score, xticklabels=C_vals, yticklabels=g_vals)

结果图如下

推荐答案

热图允许创建从输入到 xticklabels / yticklabels 命令的标签.然后将它们沿轴放置,因此没有数字格式可以更改其外观.

The heatmap allows to create its labels from the input to the xticklabels/yticklabels command. Those are then put along the axes, so there is no numeric format to change their appearance.

一个选项是在将标签提供给热图之前对其进行格式化.为此,可以(错误地)使用matplotlib ScalarFormatter ,它可以根据浮点数自动生成MathText字符串.以下是一个示例:

An option is to format the labels prior to supplying them to the heatmap. To this end a matplotlib ScalarFormatter can be (mis)used, which allows to automatically generate a MathText string from a float number. The following would be an example:

import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import seaborn as sns
import numpy as np

C_vals = np.logspace(3, 10, 8)
g_vals = np.logspace(-6, 2, 9)
score = np.random.rand(len(g_vals),len(C_vals))

tick = ticker.ScalarFormatter(useOffset=False, useMathText=True)
tick.set_powerlimits((0,0))

tc = [u"${}$".format(tick.format_data(x)) for x in C_vals]
tg = [u"${}$".format(tick.format_data(x)) for x in g_vals]

sns.heatmap(score, xticklabels=tc, yticklabels=tg)

plt.show()

这篇关于如何在海底热图标签中使用科学计数法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 23:37