且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何更改 matplotlib 中绘图的轴、刻度和标签的颜色

更新时间:2023-01-03 23:19:57

举一个简单的例子(使用比可能重复的问题更简洁的方法):

As a quick example (using a slightly cleaner method than the potentially duplicate question):

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)

ax.plot(range(10))
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')

ax.spines['bottom'].set_color('red')
ax.spines['top'].set_color('red')
ax.xaxis.label.set_color('red')
ax.tick_params(axis='x', colors='red')

plt.show()

[t.set_color('red') for t in ax.xaxis.get_ticklines()]
[t.set_color('red') for t in ax.xaxis.get_ticklabels()]