设置标题后如何更改matplotlib中轴标题的颜色?

How to change the color of an axis title in matplotlib after setting the title?

我想在黑色背景上绘制一个函数。我已经设法修改了标签等,但我不知道单独更改标题颜色的命令。

例子

import numpy as np 
import matplotlib.pyplot as plt


fig1, ax1 = plt.subplots(1,1)

fig1.patch.set_facecolor('k')     
ax1.set_facecolor('k')
ax1.xaxis.label.set_color('w')
ax1.yaxis.label.set_color('w')
ax1.spines['bottom'].set_color('w')
ax1.spines['top'].set_color('w')
ax1.spines['left'].set_color('w')
ax1.spines['right'].set_color('w')
ax1.tick_params(axis='both', colors='w')


x=np.linspace(0,10,1000)
ax1.plot(x, np.exp(x))
ax1.set_title('This title\'s color should be modified afterwards', color='w')

我创建的 GUI 需要这个。我不想更改 matplotlib 模块的 rcParams。我想输入类似上面的命令。

下面是你想要的吗?

import numpy as np 
import matplotlib.pyplot as plt

title = 'This title'
fig1, ax1 = plt.subplots(1,1)

fig1.patch.set_facecolor('k')     
ax1.set_facecolor('k')
ax1.xaxis.label.set_color('w')
ax1.yaxis.label.set_color('w')
ax1.spines['bottom'].set_color('w')
ax1.spines['top'].set_color('w')
ax1.spines['left'].set_color('w')
ax1.spines['right'].set_color('w')
ax1.tick_params(axis='both', colors='w')
x=np.linspace(0,10,1000)
ax1.plot(x, np.exp(x))
ax1.set_title(title+'\'s color should be modified afterwards', color='blue')

您可以使用set_color方法:

ax1.title.set_color('blue')