Python:如何增加/减少x和y刻度标签的字体大小? [英] Python: How to increase/reduce the fontsize of x and y tick labels?
问题描述
在使用matplotlib
时如何弄清楚x和y刻度标签的fontsize
似乎有问题.
I seem to have a problem in figuring out how to increase or decrease the fontsize
of both the x and y tick labels while using matplotlib
.
我知道有set_xticklabels(labels, fontdict=None, minor=False, **kwargs)
函数,但是我不明白如何控制其中的fontsize
.
I am aware that there is the set_xticklabels(labels, fontdict=None, minor=False, **kwargs)
function, but I failed to understand how to control the fontsize
in it.
我希望某些明确的东西,例如
I expected something somehow explicit, like
title_string=('My Title')
plt.suptitle(title_string, y=1.0, fontsize=17)
但是到目前为止,我还没有找到类似的东西.我想念什么?
but I haven't found anything like that so far. What am I missing?
推荐答案
您可以直接在对set_xticklabels
和set_yticklabels
的调用中设置字体大小(如先前的回答所述).一次只会影响一个Axes
.
You can set the fontsize directly in the call to set_xticklabels
and set_yticklabels
(as noted in previous answers). This will only affect one Axes
at a time.
ax.set_xticklabels(x_ticks, rotation=0, fontsize=8)
ax.set_yticklabels(y_ticks, rotation=0, fontsize=8)
您还可以使用ticklabel字体大小(即,针对脚本中的所有图形/子图). > rcParams
:
You can also set the ticklabel
font size globally (i.e. for all figures/subplots in a script) using rcParams
:
import matplotlib.pyplot as plt
plt.rc('xtick',labelsize=8)
plt.rc('ytick',labelsize=8)
或者,等效地:
plt.rcParams['xtick.labelsize']=8
plt.rcParams['ytick.labelsize']=8
最后,如果要为所有matplotlib图设置此设置,则还可以在matplotlibrc
文件中设置这两个rcParams
:
Finally, if this is a setting that you would like to be set for all your matplotlib plots, you could also set these two rcParams
in your matplotlibrc
file:
xtick.labelsize : 8 # fontsize of the x tick labels
ytick.labelsize : 8 # fontsize of the y tick labels
这篇关于Python:如何增加/减少x和y刻度标签的字体大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!