亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Seaborn Heatmap:單元格中的文本下劃線

Seaborn Heatmap:單元格中的文本下劃線

忽然笑 2022-06-07 17:52:43
我正在用 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")這是我目前的結果:當然我嘗試過使用 argument annot_kws={"style": "underlined"},但顯然在 Seaborn 中,“style”鍵只支持值“normal”、“italic”或“oblique”。有解決方法嗎?
查看完整描述

1 回答

?
縹緲止盈

TA貢獻2041條經驗 獲得超4個贊

是的,您可以在文本中使用 tex 命令解決您的問題?;舅枷胧悄褂?的annot鍵seaborn.heatmap將字符串數組分配為文本標簽。這些包含您的數據值 + 一些 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))

由此產生的情節基本上是你想要的:

http://img1.sycdn.imooc.com//629f2013000193e305280405.jpg

查看完整回答
反對 回復 2022-06-07
  • 1 回答
  • 0 關注
  • 133 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號