B Yellow
B Yellow

Reputation: 129

Seaborn heatmap clipping y-axis tick labels

I'm attempting to create a heatmap with a DataFrame with rows like:

у     7.14e-02  4.29e-01  5.00e-01
ф     0.00e+00  1.00e+00  0.00e+00
х     0.00e+00  1.00e+00  0.00e+00
ц     0.00e+00  1.00e+00  0.00e+00
ч     0.00e+00  9.75e-01  2.50e-02
ш     0.00e+00  1.00e+00  0.00e+00
щ     0.00e+00  1.00e+00  0.00e+00

However, when I try to create a heatmap from this data, I end up with a y-axis that looks like this (note letters like у, ф, and ш with their bottom/right clipped off).

y-axis

Here's the code I'm using to initialize it:

fig, ax = plt.subplots(figsize=(15, 15)
plot = sns.heatmap(df_result, annot=True, fmt=".2f")

EDIT: I have tested replacing the Cyrillic character 'у' with the Latin character 'y' and it is no longer clipped. Any reason this issue would be specific to Cyrillic characters?

Upvotes: 2

Views: 1390

Answers (2)

nav610
nav610

Reputation: 791

The issue may be in the encoding when reading in the csv.

Here is the csv:

name,value1,value2,value3
у, 7.14e-02, 4.29e-01, 5.00e-01
ф, 0.00e+00, 1.00e+00, 0.00e+00
х, 0.00e+00, 1.00e+00, 0.00e+00
ц, 0.00e+00, 1.00e+00, 0.00e+00
ч, 0.00e+00, 9.75e-01, 2.50e-02
ш, 0.00e+00, 1.00e+00, 0.00e+00
щ, 0.00e+00, 1.00e+00, 0.00e+00

Read in the csv as:

df = pd.read_csv("data.csv",encoding='utf-8')

Save names list as y_labels and graph:

y_labels = df['name'].tolist()

fig, ax = plt.subplots(figsize = (15, 15))
ax  = sns.heatmap(df[['value1','value2','value3']],
yticklabels =  y_labels)

y-axis labels are not cut off. enter image description here

Upvotes: 4

Parth Shah
Parth Shah

Reputation: 1475

Rotating the yticks would help with the clipping.

degrees = 90
plt.yticks(rotation=degrees)

Or you could add some padding for the yaxis.

n = 15
ax.tick_params(axis='y', which='major', pad=n)

Documentation for padding with matplotlib can be found here.

Upvotes: 0

Related Questions