精细控制学术论文在Seaborn图中的字体大小 [英] Fine control over the font size in Seaborn plots for academic papers

查看:72
本文介绍了精细控制学术论文在Seaborn图中的字体大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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?

我还想确保不同的seaborn图之间的字体大小是一致的.但并非我所有的 seaborn 图都具有相同的尺寸,所以似乎在所有图上使用 相同的 font_scale 不一定在这些不同的图中创建相同的字体大小?

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_scale on all the plots does not necessarily create the same font size across these different plots?

我在下面附加了我的代码.我感谢任何关于如何格式化两栏学术论文的情节的评论.我的目标是能够控制图形的大小而不会扭曲字体大小或图形.我用 Latex 写论文.

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.

# 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
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()

,结果如下:

为了使其在图之间保持一致,我认为您只需要确保 DPI 相同.顺便说一句,也可以自定义 rc词典因为存在"font.size"参数,但我不太确定该怎么做.

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 dictionaries since "font.size" parameter exists but I'm not too sure how to do that.

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

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.

这篇关于精细控制学术论文在Seaborn图中的字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆