首页 > 解决方案 > 带有axes.title.set_text的子图中的标题字体

问题描述

我打算制作多个子图来展示我的结果。我使用了 matplotlib 的子图。我有文字大小的问题。正如您在此处的简单代码中看到的那样。在plt.title文档中它说title(label, fontdict=None, loc='center', pad=None, **kwargs)

import random
from matplotlib.pyplot import figure, plot, xlabel, ylabel, legend, close, subplots, title, savefig, get_current_fig_manager, show, pause, clf

x = []
for i in range(10):
    x.append(random.random()*i)

y_1 = []

for i in range(10):
    y_1.append(random.random()*i)
y_2 = []

for i in range(10):
    y_2.append(random.random()*i)


fig, ax = subplots(1, 2, squeeze = False, figsize = (10,10))
ax[0,1].title.set_text('y_1', fontdict = {'font.size':22})
ax[0,1].plot(x,y_1)
ax[0,1].set_xlabel('x')
ax[0,1].set_ylabel('y_1')

ax[0,0].title.set_text('y_2', fontdict = {'font.size':22})
ax[0,0].plot(x,y_2)
ax[0,0].set_xlabel('x')
ax[0,0].set_ylabel('y_2')

但是如果我运行此代码,我会收到错误TypeError: set_text() got an unexpected keyword argument 'fontdict'

我是否使用了错误的命令。

标签: pythonmatplotlib

解决方案


这实际上只是一个小问题:

要设置特定轴的标题,您应该使用轴的set_title方法。Usingplt.title设置当前坐标区实例的标题。

基本上替换你ax[0,0].title.set_textax[0,0].set_title,你很高兴!


推荐阅读