How to increase the cell size for annotation in Seaborn heatmap

亡梦爱人 提交于 2019-12-13 02:18:59

问题


I'd like to plot 66x66 confusion matrix with min=0 and max=15075 (shared here). Since I'd like to annotate the cells, I am using annot=True. But I can't seem to fit the values in the cell even after trying approaches suggested in other stack overflow posts like this, this and this. This is my current code:

from sklearn.metrics import confusion_matrix
conf_mat = confusion_matrix(y_test, y_pred) # please load the data from the pastebin [link above][1]

# get the tick label font size
fontsize_pt = 10 # plt.rcParams['ytick.labelsize'] does not return a numeric value like suggested [here][2], so I set it to '10'
dpi = 72.27

# comput the matrix height in points and inches
matrix_height_pt = fontsize_pt * conf_mat.shape[0]
matrix_height_in = matrix_height_pt / dpi

# compute the required figure height 
top_margin = 0.04
bottom_margin = 0.04
figure_height = matrix_height_in / (1 - top_margin - bottom_margin)

# build the figure instance with the desired height
fig, ax = plt.subplots(figsize=(20, figure_height),
                       gridspec_kw=dict(top=(1-top_margin), bottom=bottom_margin))

# here, I turned on annot and changed the color map scheme
ax = sns.heatmap(conf_mat, ax=ax, annot=True, cmap="cubehelix")

When I run the above code, it returns a figure that looks like this

I would like the annotated figures to fit in each cell of the heatmap, and if possible, the color range to be a bit more reasonable (instead of all being black as it is right now).

Any suggestion for improvement would be greatly appreciated. Thank you.

来源:https://stackoverflow.com/questions/51144934/how-to-increase-the-cell-size-for-annotation-in-seaborn-heatmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!