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

后端 未结 2 599
故里飘歌
故里飘歌 2021-01-30 07:17

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 hav

相关标签:
2条回答
  • 2021-01-30 07:42

    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.:

    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.

    0 讨论(0)
  • 2021-01-30 07:49

    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:

    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.

    0 讨论(0)
提交回复
热议问题