Python 精细控制学术论文 Seaborn 图中的字体大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36220829/
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 17:35:03  来源:igfitidea点击:

Fine control over the font size in Seaborn plots for academic papers

pythonmatplotlibplotseaborn

提问by kc2uno

I'm currently trying to use Seaborn to create plots for my academic papers. 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.

我目前正在尝试使用 Seaborn 为我的学术论文创建图表。绘图看起来很棒且易于生成,但我遇到的一个问题是对绘图中的字体大小进行精细控制。

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. But in seaborn, the font size is mainly controlled through 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?

我论文中的字体大小是 9pt,我想确保我的图中的字体大小是 9pt 或 10pt。但是在seaborn中,字体大小主要是通过 font scale 来控制的sns.set_context("paper", font_scale=0.9)。所以我很难找到合适的字体大小,除非通过反复试验。有没有更有效的方法来做到这一点?

I also want to make sure the font size is consistent between different seaborn plots. But not all my seaborn plots have the same dimension, so it seems like using the same font_scaleon all the plots does not necessarily create the same font size across these different plots?

我还想确保不同 seaborn 图之间的字体大小是一致的。但并非我所有的 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. 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.

我在下面附上了我的代码。我感谢任何关于如何格式化两栏学术论文的情节的评论。我的目标是能够在不扭曲字体大小或绘图的情况下控制图形的大小。我用 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')                                          

回答by armatita

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
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)
sns.plt.show()

, which results in this:

,这导致:

Different font sizes for different labels

不同标签的不同字体大小

To make it consistent in between plots I think you just need to make sure the DPI is the same. By the way it' also a possibility to customize a bit the rc dictionariessince "font.size" parameter exists but I'm not too sure how to do that.

为了使其在绘图之间保持一致,我认为您只需要确保 DPI 相同。顺便说一句,由于存在“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.

注意:而且我也不太明白为什么他们更改了轴标签和刻度的字体大小变量的名称。似乎有点不直观。

回答by TNT

It is all but satisfying, isn't it? The easiest way I have found to specify when setting the context, e.g.:

这一切都令人满意,不是吗?我发现在设置上下文时指定的最简单方法,例如:

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

This should take care of 90% of standard plotting usage. 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, e.g.:

这应该处理 90% 的标准绘图使用。如果您希望刻度标签小于轴标签,请将 'axes.labelsize' 设置为较小的 (ticklabel) 值并手动指定轴标签(或其他自定义元素),例如:

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_scalein sns.set_contextis ignored for all specified font elements, even if you set it.

注意:如果您指定这些数字,则对于所有指定的字体元素指定font_scalein 将sns.set_context被忽略,即使您设置了它。