如何更改matplotlib绘图上所有元素(记号、标签、标题)的字体大小?
我知道如何更改勾号标签大小,这是通过以下方式完成的:
import matplotlib
matplotlib.rc('xtick', labelsize=20)
matplotlib.rc('ytick', labelsize=20)
但一个人如何改变其他人呢?
如何更改matplotlib绘图上所有元素(记号、标签、标题)的字体大小?
我知道如何更改勾号标签大小,这是通过以下方式完成的:
import matplotlib
matplotlib.rc('xtick', labelsize=20)
matplotlib.rc('ytick', labelsize=20)
但一个人如何改变其他人呢?
当前回答
使用plt.tick_params(labelsize=14)
其他回答
我通过@ryggyr编写了一个修改版本的答案,它允许对单个参数进行更多控制,并在多个子地块上工作:
def set_fontsizes(axes,size,title=np.nan,xlabel=np.nan,ylabel=np.nan,xticks=np.nan,yticks=np.nan):
if type(axes) != 'numpy.ndarray':
axes=np.array([axes])
options = [title,xlabel,ylabel,xticks,yticks]
for i in range(len(options)):
if np.isnan(options[i]):
options[i]=size
title,xlabel,ylabel,xticks,yticks=options
for ax in axes.flatten():
ax.title.set_fontsize(title)
ax.xaxis.label.set_size(xlabel)
ax.yaxis.label.set_size(ylabel)
ax.tick_params(axis='x', labelsize=xticks)
ax.tick_params(axis='y', labelsize=yticks)
如果要仅更改已创建的特定绘图的字体大小,请尝试以下操作:
import matplotlib.pyplot as plt
ax = plt.subplot(111, xlabel='x', ylabel='y', title='title')
for item in ([ax.title, ax.xaxis.label, ax.yaxis.label] +
ax.get_xticklabels() + ax.get_yticklabels()):
item.set_fontsize(20)
如果你像我一样是一个控制狂,你可能需要明确设置所有字体大小:
import matplotlib.pyplot as plt
SMALL_SIZE = 8
MEDIUM_SIZE = 10
BIGGER_SIZE = 12
plt.rc('font', size=SMALL_SIZE) # controls default text sizes
plt.rc('axes', titlesize=SMALL_SIZE) # fontsize of the axes title
plt.rc('axes', labelsize=MEDIUM_SIZE) # fontsize of the x and y labels
plt.rc('xtick', labelsize=SMALL_SIZE) # fontsize of the tick labels
plt.rc('ytick', labelsize=SMALL_SIZE) # fontsize of the tick labels
plt.rc('legend', fontsize=SMALL_SIZE) # legend fontsize
plt.rc('figure', titlesize=BIGGER_SIZE) # fontsize of the figure title
注意,您还可以在matplotlib上设置调用rc方法的大小:
import matplotlib
SMALL_SIZE = 8
matplotlib.rc('font', size=SMALL_SIZE)
matplotlib.rc('axes', titlesize=SMALL_SIZE)
# and so on ...
您可以使用plt.rcParams[“font.size”]在matplotlib中设置font_size,也可以使用plt.rcParams[“font-family”]在matplotlib设置font_family。请尝试以下示例:
import matplotlib.pyplot as plt
plt.style.use('seaborn-whitegrid')
label = [1,2,3,4,5,6,7,8]
x = [0.001906,0.000571308,0.0020305,0.0037422,0.0047095,0.000846667,0.000819,0.000907]
y = [0.2943301,0.047778308,0.048003167,0.1770876,0.532489833,0.024611333,0.157498667,0.0272095]
plt.ylabel('eigen centrality')
plt.xlabel('betweenness centrality')
plt.text(0.001906, 0.2943301, '1 ', ha='right', va='center')
plt.text(0.000571308, 0.047778308, '2 ', ha='right', va='center')
plt.text(0.0020305, 0.048003167, '3 ', ha='right', va='center')
plt.text(0.0037422, 0.1770876, '4 ', ha='right', va='center')
plt.text(0.0047095, 0.532489833, '5 ', ha='right', va='center')
plt.text(0.000846667, 0.024611333, '6 ', ha='right', va='center')
plt.text(0.000819, 0.157498667, '7 ', ha='right', va='center')
plt.text(0.000907, 0.0272095, '8 ', ha='right', va='center')
plt.rcParams["font.family"] = "Times New Roman"
plt.rcParams["font.size"] = "50"
plt.plot(x, y, 'o', color='blue')
这是马里乌斯·雷特根回答的延伸。您可以使用所有修改创建一个单独的JSON文件,然后使用rcParams.update加载它。这些更改仅适用于当前脚本。所以
import json
from matplotlib import pyplot as plt, rcParams
s = json.load(open("example_file.json")
rcParams.update(s)
并将此“example_file.json”保存在同一文件夹中。
{
"lines.linewidth": 2.0,
"axes.edgecolor": "#bcbcbc",
"patch.linewidth": 0.5,
"legend.fancybox": true,
"axes.color_cycle": [
"#348ABD",
"#A60628",
"#7A68A6",
"#467821",
"#CF4457",
"#188487",
"#E24A33"
],
"axes.facecolor": "#eeeeee",
"axes.labelsize": "large",
"axes.grid": true,
"patch.edgecolor": "#eeeeee",
"axes.titlesize": "x-large",
"svg.fonttype": "path",
"examples.directory": ""
}