Python seaborn heatmap, how to plot y axis labels.
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

I need to add y axis labels to the a heat-map, in the code below the Y axis should contain the person names associated with the development language.

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
test_data = {
'Java':[38,  5, 30, 4],
'COBOL':[29, 10, 9, 34],
'C':[18, 23, 26, 12],
'C#':[23, 26, 27,12],
'.NET':[28, 15, 32, 14],
'Assembler':[18, 23, 23, 23],
'Python':[ 8,  8, 32, 8]
}

df = pd.DataFrame(test_data)
# This feeble attempt does not work
plt.yticks(np.arange(4),['Tom', 'Dick', 'Sue','Ben']) 
print(df)
plt.figure(figsize=(10,5))
sns.heatmap(df,cmap="YlGnBu",annot=True)
plt.show()
awarded to TheOsch
Tags
python3

Crowdsource coding tasks.

2 Solutions


Hello, Please try this.

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
test_data = {
'Java':[38, 5, 30, 4],
'COBOL':[29, 10, 9, 34],
'C':[18, 23, 26, 12],
'C#':[23, 26, 27,12],
'.NET':[28, 15, 32, 14],
'Assembler':[18, 23, 23, 23],
'Python':[ 8, 8, 32, 8]
}

df = pd.DataFrame(test_data)
print(df)
sns.heatmap(df,cmap="YlGnBu",annot=True, yticklabels = (['Tom', 'Dick', 'Sue','Ben']))
plt.show()

Thanks, this works perfect,
broadreach 5 months ago
Winning solution

Here's the correct function call order (key: yticks should go after figure, and heatmap, i.e. when the plot already exists, not before it).

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np

test_data = {
    'Java':[38,  5, 30, 4],
    'COBOL':[29, 10, 9, 34],
    'C':[18, 23, 26, 12],
    'C#':[23, 26, 27,12],
    '.NET':[28, 15, 32, 14],
    'Assembler':[18, 23, 23, 23],
    'Python':[ 8,  8, 32, 8]
}

df = pd.DataFrame(test_data)
print(df)
plt.figure(figsize=(10,5))
sns.heatmap(df,cmap="YlGnBu",annot=True)
plt.yticks(np.arange(4),['Tom', 'Dick', 'Sue','Ben']) 
plt.show()
View Timeline