在Seaborn中增加刻度标签字体大小

40

我的seaborn图表存在一个严重问题,由于某些原因,沿轴的数字以非常小的字体打印,导致它们无法阅读。我尝试使用

with plt.rc_context(dict(sns.axes_style("whitegrid"),
                     **sns.plotting_context(font_scale=5))):
    b = sns.violinplot(y="Draughts", data=dr)

对于无法帮助到的情况,这只会使轴上的文本更大,而不是沿轴的数字。查看图片

3个回答

60

这里的回答可以让在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


1
我认为这不正确:font_scale 改变了所有字体,据我所知,包括标题大小。有没有一种方法仅更改刻度标签? - Mayou36
2
sns.set(); fig, ax = plt.subplots(); sns.violinplot(data = df, ax = ax); ax.yaxis.set_tick_params(labelsize = 30); plt.show() - p-robot
这种方法会改变所有的字体。 - undefined

32

在接受的答案的基础上进行扩展,如果你只想调整刻度标签的字体大小而不需要以同样的比例缩放其他标签,可以尝试以下方法:

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()

图表链接


2
如果你的图是一个FacetGrid,你可以直接使用g.set_yticklabels(size = 15) - Levi Baguley
是的,谢谢你的回答!有很多错误的回答建议缩放所有字体。这正是我在寻找的! - Mayou36

11
  • 本回答将讨论如何独立设置x或y刻度标签大小。
  • sns.set(font_scale=2)来自p-robot将设置所有图形字体
  • Kabir Ahuja的答案之所以有效,是因为y标签位置被用作文本。
    • 如果有y标签文本,则该解决方案将无效。
  • 有多种方法可以获取刻度和标签:
    • plt.xticks()
    • ax.get_xticklabels()
    • ax.get_xticks()
  • python 3.10matplotlib 3.5.2seaborn 0.12.0中进行了测试

给定以下图表

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()

设置y轴无效

enter image description here

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

print(ax.get_xticks())
print(ax.get_yticks())
print('\n')
print(ax.get_xticklabels())
print(ax.get_yticklabels())
print('\n')
print(plt.xticks())
print(plt.yticks())
print('\n')

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

print(ylabels)

print(yticks)

# there is no text label
print(ylabels[0].get_text())

# there are text labels on the x-axis
print(xlabels)

# the answer from Kabir Ahuja works because of this
print(ax.get_yticks())

# set the x-axis ticklabel size
ax.set_xticklabels(xlabels, size=5)

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

plt.show()

print 输出

[0 1 2 3]
[-10.   0.  10.  20.  30.  40.  50.  60.  70.]

[Text(0, 0, 'Thur'), Text(1, 0, 'Fri'), Text(2, 0, 'Sat'), Text(3, 0, 'Sun')]
[Text(0, 0, ''), Text(0, 0, ''), Text(0, 0, ''), Text(0, 0, ''), Text(0, 0, ''), Text(0, 0, ''), Text(0, 0, ''), Text(0, 0, ''), Text(0, 0, '')]

(array([0, 1, 2, 3]), [Text(0, 0, 'Thur'), Text(1, 0, 'Fri'), Text(2, 0, 'Sat'), Text(3, 0, 'Sun')])
(array([-10.,   0.,  10.,  20.,  30.,  40.,  50.,  60.,  70.]), [Text(0, 0, ''), Text(0, 0, ''), Text(0, 0, ''), Text(0, 0, ''), Text(0, 0, ''), Text(0, 0, ''), Text(0, 0, ''), Text(0, 0, ''), Text(0, 0, '')])

[Text(0, 0, ''), Text(0, 0, ''), Text(0, 0, ''), Text(0, 0, ''), Text(0, 0, ''), Text(0, 0, ''), Text(0, 0, ''), Text(0, 0, ''), Text(0, 0, '')]
[-10.   0.  10.  20.  30.  40.  50.  60.  70.]
← empty string from ylabels[0].get_text()
[Text(0, 0, 'Thur'), Text(1, 0, 'Fri'), Text(2, 0, 'Sat'), Text(3, 0, 'Sun')]
[-10.   0.  10.  20.  30.  40.  50.  60.  70.]

绘图

  • 没有y轴刻度标签,因为
    • y_text = [y.get_text() for y in ylabels] = ['', '', '', '', '', '', '', '', '']

enter image description here

设置y轴刻度标签大小

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

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

设置xticklabel的大小

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

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

这很有效

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

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

# set the x-labels with
ax.set_xticklabels(xlabels, size=5)

# prevents: UserWarning: FixedFormatter should only be used together with FixedLocator
ax.set_yticks(ax.get_yticks()[1:])
# set the y-labels with
_ = ax.set_yticklabels(ax.get_yticks(), size=5)

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接