How to change font size in scanpy

4.1k views Asked by At

I am generating dotplots using scanpy and unable to change the font size. Is there a way to do so?

For example, how would I edit this line of code?

sc.pl.dotplot(df, ["gene"], 'CellType', dendrogram=True, save = name)

2

There are 2 answers

6
Paul Brodersen On BEST ANSWER

IIRC, scanpy just uses matplotlib under the hood, so there are several options:

You can set the fontsize globally:

import matplotlib.pyplot as plt
plt.rcParams.update({'font.size': 'large'})

You can update specifically only the fontsize of (all) axis labels:

plt.rcParams.update({'axes.labelsize' : 'large'}) 
plt.rcParams.update({'axes.xtick.labelsize' : 'large'})   
plt.rcParams.update({'axes.ytick.labelsize' : 'large'})    

Finally, if you have a handle of the axis, you can change the fontsize of labels by traversing the axis attributes:

dp_object = sc.pl.dotplot(df,  ["gene"], 'CellType', dendrogram=True,  save = name)
axes_dict = dp_object.get_axes()
# figure out which axis you want by printing the axes_dict
# print(axes_dict)
ax = axes_dict[...]
ax.xaxis.label.set_fontsize(22)
for label in ax.get_xticklabels():
    label.set_fontsize('large')
0
Workhorse On

Turns out you can do this directly using scanpy with the following:

sc.set_figure_params(scanpy=True, fontsize=14)

However, I am still unsure how to change the y axis specifically...