You can get a list of tick labels using ax.get_xticklabels()
. This is actually a list of text objects. As a result, you can use set_color()
on an element of that list to change the color:
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(5,4))
ax.plot([1,2,3])
ax.get_xticklabels()[3].set_color("red")
plt.show()
Alternatively, you can get the current axes using plt.gca()
. The below code will give the same result
import matplotlib.pyplot as plt
plt.figure(figsize=(5,4))
plt.plot([1, 2, 3])
plt.gca().get_xticklabels()[3].set_color("red")
plt.show()
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…