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))
由此產生的情節基本上是你想要的:
添加回答
舉報