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

Time:02-06

I wanted to plot a function on a black colored background. I already managed to modify the labels etc. but I don't know the command to change the color of the title solely.

Example

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')

I need this for a GUI I created. I don't want to change the rcParams of the matplotlib module. I want to enter a command similar to above.

Example

CodePudding user response:

You can use the set_color method:

ax1.title.set_color('blue')

CodePudding user response:

Is the following what you want?

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')

enter image description here

  •  Tags:  
  • Related