Skip to content Skip to sidebar Skip to footer

Increase Tick Label Font Size In Seaborn

I have a huge problem with my seaborn plots. For some reason, the numbers along the axis are printed with a really small font, which makes them unreadable. I've tried to scale them

Solution 1:

The answer from here makes fonts larger in seaborn ...

import pandas as pd, numpy as np, seaborn as sns
from matplotlib import pyplot as plt

# Generate data
df = pd.DataFrame({"Draughts": np.random.randn(100)})

# Plot using seaborn
sns.set(font_scale = 2)
b = sns.violinplot(y = "Draughts", data = df)
plt.show()

enter image description here

Solution 2:

Expanding on the accepted answer, if you want to just rescale the font size of the tick labels without scaling other labels by the same amount, you can try this:

import pandas as pd, numpy as np, seaborn as sns
from matplotlib import pyplot as plt

# Generate data
df = pd.DataFrame({"Draughts": np.random.randn(100)})

# Plot using seaborn
b = sns.violinplot(y = "Draughts", data = df)
b.set_yticklabels(b.get_yticks(), size = 15)

plt.show()

Plot link

Solution 3:

  • This answer will address setting x or y ticklabel size independently.
  • sns.set(font_scale=2) from p-robot will set all the figure fonts.
  • The answer from Kabir Ahuja works because y-labels position is being used as the text.
    • If there are y-labels text, that solution will not work.

Given the following plot

import matplotlib.pyplot as plt
import seaborn as sns

# data
tips = sns.load_dataset("tips")

# plot figure
plt.figure(figsize=(8, 6))
p = sns.violinplot(x="day", y="total_bill", data=tips)

# get label text
_, ylabels = plt.yticks()
_, xlabels = plt.xticks()
plt.show()

place graph here

yl = list(ylabels)
print(yl)
>>>[Text(0, -10.0, ''),
Text(0, 0.0, ''),
Text(0, 10.0, ''),
Text(0, 20.0, ''),
Text(0, 30.0, ''),
Text(0, 40.0, ''),
Text(0, 50.0, ''),
Text(0, 60.0, ''),
Text(0, 70.0, '')]

# see that there are no text labelsprint(yl[0].get_text())
>>> ''# see that there are text labels on the x-axisprint(list(xlabels))
>>> [Text(0, 0, 'Thur'), Text(1, 0, 'Fri'), Text(2, 0, 'Sat'), Text(3, 0, 'Sun')]

# the answer from Kabir Ahuja works because of thisprint(p.get_yticks())
>>> array([-10.,   0.,  10.,  20.,  30.,  40.,  50.,  60.,  70.])

# in this case, the following won't work because the text is ''# this is what to do if the there are text labels
p.set_yticklabels(ylabels, size=15)

# set the x-axis ticklabel size
p.set_xticklabels(xlabels, size=5)
  • There are no ytick labels because
    • y_text = [x.get_text() for x in ylabels] = ['', '', '', '', '', '', '', '', '']

enter image description here

To set yticklabel size

# use
p.set_yticklabels(p.get_yticks(), size=15)

# or
_, ylabels = plt.yticks()
p.set_yticklabels(ylabels, size=15)

To set xticklable size

# use
p.set_xticklabels(p.get_xticks(), size=15)

# or
_, xlabels = plt.xticks()
p.set_xticklabels(xlabels, size=15)

With the given plot

# set the y-labels with
p.set_yticklabels(p.get_yticks(), size=5)

# set the x-labels with
_, xlabels = plt.xticks()
p.set_xticklabels(xlabels, size=5)

enter image description here

Post a Comment for "Increase Tick Label Font Size In Seaborn"