TRex
TRex

Reputation: 495

python adding title to individual axis in Seaborn

Below is the output (subplots) from my codes

enter image description here

What I want to do is -

  1. on the first graph (left most) instead of putting "Category1 y axis / Category2 x axis" at the top I wanted Category1 along the y axis and Category2 along the x-axis at the bottom.

  2. Also can I move xticklabels for all the graphs to the top?

codes

import matplotlib.pyplot as plt
import seaborn as sns

dataf = np.random.randint(1,10,9).reshape(3,3)
dataA = np.random.randint(1,10,9).reshape(3,3)
dataB = np.random.randint(1,10,9).reshape(3,3)

fig, (ax1, ax2, ax3) = plt.subplots(1,3)
ax1.set_title("Category1 y axis / Category2 x axis")
ax2.set_title("Category1 average value")
ax3.set_title("Category2 average value")

sns.heatmap(dataf, ax=ax1,annot=True)
sns.heatmap(dataA, ax=ax2,annot=True)
sns.heatmap(dataB, ax=ax3,annot=True)
plt.show()

Upvotes: 1

Views: 62

Answers (2)

tdy
tdy

Reputation: 41327

To set the x- and y-labels, use set_xlabel() and set_ylabel().

To move the x-ticks, use labeltop=True in tick_params().

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

dataf = np.random.randint(1,10,9).reshape(3,3)
dataA = np.random.randint(1,10,9).reshape(3,3)
dataB = np.random.randint(1,10,9).reshape(3,3)

fig, (ax1, ax2, ax3) = plt.subplots(1,3)

sns.heatmap(dataf, ax=ax1, annot=True)
sns.heatmap(dataA, ax=ax2, annot=True)
sns.heatmap(dataB, ax=ax3, annot=True)

# set leftmost ylabel
ax1.set_ylabel('Category 1')

# set xlabels
ax1.set_xlabel('Category 2')
ax2.set_xlabel('Category 1 average value')
ax3.set_xlabel('Category 2 average value')

# move xticklabels to top
ax1.tick_params(which='major', labelbottom=False, labeltop=True)
ax2.tick_params(which='major', labelbottom=False, labeltop=True)
ax3.tick_params(which='major', labelbottom=False, labeltop=True)

heatmap output

Upvotes: 1

Sandeep Maurya
Sandeep Maurya

Reputation: 58

ax1.set_ylabel('Category1')
ax1.set_xlabel('Category2')

Upvotes: 0

Related Questions