第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

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

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

忽然笑 2022-06-07 17:52:43
我正在用 Python 進行一些數(shù)據(jù)分析,并且正在使用 Seaborn 進行可視化。Seaborn 非常適合創(chuàng)建熱圖。我試圖強調(diào)熱圖中每一列的最大值。通過將它們設(shè)置為斜體和粗體,我能夠正確突出顯示最大單元格中的文本。盡管如此,我還是沒有辦法強調(diào)它。這是我的代碼示例: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")這是我目前的結(jié)果:當(dāng)然我嘗試過使用 argument annot_kws={"style": "underlined"},但顯然在 Seaborn 中,“style”鍵只支持值“normal”、“italic”或“oblique”。有解決方法嗎?
查看完整描述

1 回答

?
縹緲止盈

TA貢獻2041條經(jīng)驗 獲得超4個贊

是的,您可以在文本中使用 tex 命令解決您的問題?;舅枷胧悄褂?的annot鍵seaborn.heatmap將字符串?dāng)?shù)組分配為文本標(biāo)簽。這些包含您的數(shù)據(jù)值 + 一些 tex 前綴/后綴,以允許 tex 使它們變?yōu)榇煮w/強調(diào)(斜體)/下劃線或其他任何內(nèi)容。


一個例子(帶有隨機數(shù)):


# 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()

進一步解釋注解數(shù)組:


# 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))

由此產(chǎn)生的情節(jié)基本上是你想要的:

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

查看完整回答
反對 回復(fù) 2022-06-07
  • 1 回答
  • 0 關(guān)注
  • 138 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號