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
Increase tick label font size in seaborn
提问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.
回答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()
回答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()
回答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()
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] = ['', '', '', '', '', '', '', '', '']
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)