How to increase the cell size for annotation in Se

2019-08-09 11:10发布

问题:

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.