简体   繁体   English

精细控制 Seaborn 图中的字体大小

[英]Fine control over the font size in Seaborn plots

I'm currently trying to use Seaborn to create plots for my academic papers.我目前正在尝试使用 Seaborn 为我的学术论文创建图表。 The plots look great and easy to generate, but one problem that I'm having some trouble with is having the fine control on the font size in the plots.这些图看起来很棒且易于生成,但我遇到的一个问题是对图中字体大小的精细控制。

My font size in my paper is 9pt and I would like to make sure the font size in my plots are either 9pt or 10pt.我论文中的字体大小是 9pt,我想确保绘图中的字体大小是 9pt 或 10pt。 But in seaborn, the font size is mainly controlled through font scale sns.set_context("paper", font_scale=0.9) .但在seaborn中,字体大小主要是通过font scale来控制的sns.set_context("paper", font_scale=0.9) So it's hard for me to find the right font size except through trial and error.所以我很难找到合适的字体大小,除非通过反复试验。 Is there a more efficient way to do this?有没有更有效的方法来做到这一点?

I also want to make sure the font size is consistent between different seaborn plots.我还想确保字体大小在不同的 seaborn 图之间保持一致。 But not all my seaborn plots have the same dimension, so it seems like using the same font_scale on all the plots does not necessarily create the same font size across these different plots?但并非我所有的 seaborn 图都具有相同的尺寸,所以似乎在所有图上使用相同的 font_scale不一定在这些不同的图上创建相同的字体大小?

I've attached my code below.我在下面附上了我的代码。 I appreciate any comments on how to format the plot for a two column academic paper.对于如何为两栏学术论文格式化 plot 的任何评论,我表示感谢。 My goal is to be able to control the size of the figure without distorting the font size or the plot. I use Latex to write my paper.我的目标是能够控制图形的大小而不扭曲字体大小或 plot。我使用 Latex 来写我的论文。

# Seaborn setting                                                                                                                                              
sns.set(style='whitegrid', rc={"grid.linewidth": 0.1})
sns.set_context("paper", font_scale=0.9)                                                  
plt.figure(figsize=(3.1, 3)) # Two column paper. Each column is about 3.15 inch wide.                                                                                                                                                                                                                                 
color = sns.color_palette("Set2", 6)

# Create a box plot for my data                                                      
splot = sns.boxplot(data=df, palette=color, whis=np.inf,                              
        width=0.5, linewidth = 0.7)

# Labels and clean up on the plot                                                                                                                                                                                                                                                                                              
splot.set_ylabel('Normalized WS')                                                     
plt.xticks(rotation=90)                                                               
plt.tight_layout()                                                                    
splot.yaxis.grid(True, clip_on=False)                                                 
sns.despine(left=True, bottom=True)                                                   
plt.savefig('test.pdf', bbox_inches='tight')                                          

You are right.你说的对。 This is a badly documented issue.这是一个记录不充分的问题。 But you can change the font size parameter (by opposition to font scale) directly after building the plot.但是您可以在构建绘图后直接更改字体大小参数(通过与字体比例相反)。 Check the following example:检查以下示例:

import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset("tips")

b = sns.boxplot(x=tips["total_bill"])
b.axes.set_title("Title",fontsize=50)
b.set_xlabel("X Label",fontsize=30)
b.set_ylabel("Y Label",fontsize=20)
b.tick_params(labelsize=5)
plt.show()

, which results in this: ,结果如下:

不同标签的不同字体大小

To make it consistent in between plots I think you just need to make sure the DPI is the same.为了使其在绘图之间保持一致,我认为您只需要确保 DPI 相同。 By the way it' also a possibility to customize a bit therc dictionaries since "font.size" parameter exists but I'm not too sure how to do that.顺便说一句,由于存在“font.size”参数,因此也可以自定义一点rc字典,但我不太确定如何做到这一点。

NOTE: And also I don't really understand why they changed the name of the font size variables for axis labels and ticks.注意:而且我也不明白为什么他们更改了轴标签和刻度的字体大小变量的名称。 Seems a bit un-intuitive.似乎有点不直观。

It is all but satisfying, isn't it?这几乎是令人满意的,不是吗? The easiest way I have found to specify when setting the context, eg:我发现在设置上下文时指定的最简单方法,例如:

sns.set_context("paper", rc={"font.size":8,"axes.titlesize":8,"axes.labelsize":5})   

This should take care of 90% of standard plotting usage.这应该处理 90% 的标准绘图使用。 If you want ticklabels smaller than axes labels, set the 'axes.labelsize' to the smaller (ticklabel) value and specify axis labels (or other custom elements) manually, eg:如果您希望刻度标签小于轴标签,请将“axes.labelsize”设置为较小的(刻度标签)值并手动指定轴标签(或其他自定义元素),例如:

axs.set_ylabel('mylabel',size=6)

you could define it as a function and load it in your scripts so you don't have to remember your standard numbers, or call it every time.您可以将其定义为一个函数并将其加载到您的脚本中,这样您就不必记住您的标准数字,或者每次都调用它。

def set_pubfig:
    sns.set_context("paper", rc={"font.size":8,"axes.titlesize":8,"axes.labelsize":5})   

Of course you can use configuration files , but I guess the whole idea is to have a simple, straightforward method, which is why the above works well.当然你可以使用配置文件,但我想整个想法是有一个简单、直接的方法,这就是上面的效果很好的原因。

Note: If you specify these numbers, specifying font_scale in sns.set_context is ignored for all specified font elements, even if you set it.注意:如果您指定这些数字,则在sns.set_context中指定font_scale将忽略所有指定的字体元素,即使您设置了它。

I've just spent way too long trying to find out the actual values of the "paper" sns context.我花了太长时间试图找出“纸质”sns 上下文的实际值。 I could only find it for "talk" and honestly I am raging!我只能找到它来“聊天”,老实说,我很生气! Just going to use刚要用

sns.set_context("paper", rc{"font.size":8,"axes.titlesize":8,"axes.labelsize":5})  

even though these might be the values anyway!!!!即使这些可能是价值观!!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM