增加seaborn中的刻度标签字体大小

时间:2017-02-22 23:18:56

标签: python seaborn

我的seaborn情节有一个很大的问题。由于某种原因,沿轴的数字打印的字体非常小,这使得它们不可读。我试图用

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

没有帮助,这只会使轴文本变大,而不是沿轴的数字。 See image

2 个答案:

答案 0 :(得分:9)

扩展已接受的答案,如果您只想重新调整刻度标签的字体大小而又不按相同的比例缩放其他标签,则可以尝试以下操作:

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

答案 1 :(得分:2)

  • 此答案将分别解决设置x或y刻度标签大小的问题。
  • p-robot中的
  • sns.set(font_scale=2)将设置所有图形字体
  • Kabir Ahuja的答案有效,因为y-labels位置被用作文本。
    • 如果有y标签文本,则该解决方案将不起作用。

给出以下情节

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 labels
print(yl[0].get_text())
>>> ''

# see that there are text labels on the x-axis
print(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 this
print(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)
  • 没有ytick标签,因为
    • y_text = [x.get_text() for x in ylabels] = ['', '', '', '', '', '', '', '', '']

enter image description here

设置yticklabel大小

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

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

设置xticklable大小

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

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

使用给定的情节

# 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