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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42404154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 21:39:29  来源:igfitidea点击:

Increase tick label font size in seaborn

pythonseaborn

提问by bjornasm

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 with

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

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

To no help, this only makes the axis text larger, but not the number along the axis. See image

无济于事,这只会使轴文本更大,但不会使沿轴的数字变大。 看图

回答by p-robot

The answer from heremakes fonts larger in seaborn...

这里的答案使字体更大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

在此处输入图片说明

回答by Kabir Ahuja

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

绘图链接

回答by Trenton McKinney

  • This answer will address setting x or y ticklabel size independently.
  • sns.set(font_scale=2)from p-robotwill set all the figure fonts.
  • The answer from Kabir Ahujaworks because y-labels position is being used as the text.
    • If there are y-labels text, that solution will not work.
  • 此答案将独立解决设置 x 或 y 刻度标签大小的问题。
  • sns.set(font_scale=2)p-robot将设置所有图形字体
  • Kabir Ahuja的答案有效,因为 y 标签位置被用作文本。
    • 如果有 y 标签文本,该解决方案将不起作用。

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 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)
  • There are no ytick labels because
    • y_text = [x.get_text() for x in ylabels] = ['', '', '', '', '', '', '', '', '']
  • 没有 ytick 标签,因为
    • y_text = [x.get_text() for x in ylabels] = ['', '', '', '', '', '', '', '', '']

enter image description here

在此处输入图片说明

To set yticklabel size

设置 yticklabel 大小

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

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

To set xticklable size

设置 xticklable 大小

# 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

在此处输入图片说明