【问题标题】:Seaborn Heatmap: underline text in a cellSeaborn Heatmap:单元格中的文本下划线
【发布时间】:2019-09-26 09:39:39
【问题描述】:

我正在使用 Python 进行一些数据分析,并且正在使用 Seaborn 进行可视化。 Seaborn 非常适合创建热图。

我试图在我的热图中强调每一列的最大值。

通过将它们设为斜体粗体,我能够正确突出显示最大单元格中的文本。尽管如此,我还是找不到下划线的方法。

这是我的代码示例:


data_matrix = < extract my data and put them into a matrix >
max_in_each_column = np.max(data_matrix, axis=0)

sns.heatmap(data_matrix,
            mask=data_matrix == max_in_each_column,
            linewidth=0.5,
            annot=True,
            xticklabels=my_x_tick_labels,
            yticklabels=my_y_tick_labels,
            cmap="coolwarm_r")

sns.heatmap(data_matrix,
            mask=data_matrix != max_in_each_column,
            annot_kws={"style": "italic", "weight": "bold"},
            linewidth=0.5,
            annot=True,
            xticklabels=my_x_tick_labels,
            yticklabels=my_y_tick_labels,
            cbar=False,
            cmap="coolwarm_r")

这是我目前的结果:

当然,我尝试过使用 argumentannot_kws={"style": "underlined"},但显然在 Seaborn 中,“style”键只支持值“normal”、“italic”或“oblique”。

有解决办法吗?

【问题讨论】:

  • 您可以使用 matplotlib 中的 text 选项来放置注释并在 seaborn 绘图期间关闭注释。您应该提供一个工作示例以获得更多帮助

标签: python matplotlib data-visualization seaborn underline


【解决方案1】:

是的,您可以在文本中使用 tex 命令来解决您的问题。基本思想是使用seaborn.heatmapannot 键将字符串数组分配为文本标签。这些包含您的数据值 + 一些 tex 前缀/后缀,以允许 tex 将它们设为粗体/强调(斜体)/下划线或其他任何内容。

一个例子(带有随机数):

# random data
data_matrix = np.round(np.random.rand(10, 10), decimals=2)
max_in_each_column = np.max(data_matrix, axis=0)

# Activating tex in all labels globally
plt.rc('text', usetex=True)
# Adjust font specs as desired (here: closest similarity to seaborn standard)
plt.rc('font', **{'size': 14.0})
plt.rc('text.latex', preamble=r'\usepackage{lmodern}')

# remains unchanged
sns.heatmap(data_matrix,
            mask=data_matrix == max_in_each_column,
            linewidth=0.5,
            annot=True,
            cmap="coolwarm_r")

# changes here
sns.heatmap(data_matrix,
            mask=data_matrix != max_in_each_column,
            linewidth=0.5,
            # Use annot key with np.array as value containing strings of data + latex 
            # prefixes/suffices making the bold/italic/underline formatting
            annot=np.array([r'\textbf{\emph{\underline{' + str(data) + '}}}'
                            for data in data_matrix.ravel()]).reshape(
                np.shape(data_matrix)),
            # fmt key must be empty, formatting error otherwise
            fmt='',
            cbar=False,
            cmap="coolwarm_r")

plt.show()

注解数组进一步解释:

# For all matrix_elements in your 2D data array (2D requires the .ravel() and .reshape() 
# stuff at the end) construct in sum a 2D data array consisting of strings 
# \textbf{\emph{\underline{<matrix_element>}}}. Each string will be represented by tex as 
# a bold, italic and underlined representation of the matrix_element
np.array([r'\textbf{\emph{\underline{' + str(data) + '}}}'
                        for data in data_matrix.ravel()]).reshape(np.shape(data_matrix))

得到的情节基本上是你想要的:

【讨论】:

    猜你喜欢
    • 2021-01-22
    • 1970-01-01
    • 2019-08-02
    • 2012-12-06
    • 1970-01-01
    • 2011-02-11
    • 1970-01-01
    • 2013-08-22
    相关资源
    最近更新 更多