Python:如何增加/减少 x 和 y 刻度标签的字体大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34001751/
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
Python: How to increase/reduce the fontsize of x and y tick labels?
提问by FaCoffee
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
.
我似乎在弄清楚如何fontsize
在使用matplotlib
.
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.
我知道有这个set_xticklabels(labels, fontdict=None, minor=False, **kwargs)
功能,但我不明白如何控制fontsize
它。
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?
但到目前为止我还没有找到类似的东西。我错过了什么?
采纳答案by FaCoffee
It is simpler than I thought it would be.
它比我想象的要简单。
To set the font size of the x-axis ticks:
要设置 x 轴刻度的字体大小:
x_ticks=['x tick 1','x tick 2','x tick 3']
ax.set_xticklabels(x_ticks, rotation=0, fontsize=8)
To do it for the y-axis ticks:
要为 y 轴刻度执行此操作:
y_ticks=['y tick 1','y tick 2','y tick 3']
ax.set_yticklabels(y_ticks, rotation=0, fontsize=8)
The arguments rotation
and fontsize
can easily control what I was after.
争论rotation
和fontsize
可以很容易地控制我所追求的。
Reference: http://matplotlib.org/api/axes_api.html
回答by toti08
Use the keyword size
instead of fontsize
.
使用关键字size
而不是fontsize
。
回答by tmdavison
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.
您可以直接在对set_xticklabels
和的调用中设置字体大小set_yticklabels
(如之前的答案中所述)。这一次只会影响一个Axes
。
ax.set_xticklabels(x_ticks, rotation=0, fontsize=8)
ax.set_yticklabels(y_ticks, rotation=0, fontsize=8)
You can also set the ticklabel
font size globally (i.e. for all figures/subplots in a script) using rcParams
:
您还可以使用以下命令ticklabel
全局设置字体大小(即脚本中的所有图形/子图)rcParams
:
import matplotlib.pyplot as plt
plt.rc('xtick',labelsize=8)
plt.rc('ytick',labelsize=8)
Or, equivalently:
或者,等效地:
plt.rcParams['xtick.labelsize']=8
plt.rcParams['ytick.labelsize']=8
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:
最后,如果这是您希望为所有 matplotlib 图设置的设置,您还可以rcParams
在matplotlibrc
文件中设置这两个:
xtick.labelsize : 8 # fontsize of the x tick labels
ytick.labelsize : 8 # fontsize of the y tick labels
回答by ImportanceOfBeingErnest
One shouldn't use set_yticklabels
to change the fontsize, since this will also set the labels (i.e. it will replace any automatic formatter by a FixedFormatter
), which is usually undesired. The easiest is to set the respective tick_params
:
不应使用set_yticklabels
更改字体大小,因为这也会设置标签(即它将用 a 替换任何自动格式化程序FixedFormatter
),这通常是不受欢迎的。最简单的是设置相应的tick_params
:
ax.tick_params(axis="x", labelsize=8)
ax.tick_params(axis="y", labelsize=20)
or
或者
ax.tick_params(labelsize=8)
in case both axes shall have the same size.
如果两个轴的尺寸相同。
Of course using the rcParams as in @tmdavison's answeris possible as well.
当然,在@tmdavison 的回答中使用 rcParams也是可能的。