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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:19:03  来源:igfitidea点击:

Python: How to increase/reduce the fontsize of x and y tick labels?

pythontextmatplotlibaxis-labelskwargs

提问by FaCoffee

I seem to have a problem in figuring out how to increase or decrease the fontsizeof 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 fontsizein 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 rotationand fontsizecan easily control what I was after.

争论rotationfontsize可以很容易地控制我所追求的。

Reference: http://matplotlib.org/api/axes_api.html

参考:http: //matplotlib.org/api/axes_api.html

回答by toti08

Use the keyword sizeinstead of fontsize.

使用关键字size而不是fontsize

回答by tmdavison

You can set the fontsize directly in the call to set_xticklabelsand set_yticklabels(as noted in previous answers). This will only affect one Axesat 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 ticklabelfont 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 rcParamsin your matplotlibrcfile:

最后,如果这是您希望为所有 matplotlib 图设置的设置,您还可以rcParamsmatplotlibrc文件中设置这两个:

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_yticklabelsto 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也是可能的。