Matplotlib:更改轴的颜色

时间:2009-12-30 22:04:00

标签: matplotlib colors

有没有办法在matplotlib中更改轴的颜色(而不是刻度线)?我一直在浏览Axes,Axis和Artist的文档,但没有运气; matplotlib画廊也没有提示。 有什么想法吗?

3 个答案:

答案 0 :(得分:93)

使用数字时,您可以使用以下方法轻松更改脊椎颜色:

ax.spines['bottom'].set_color('#dddddd')
ax.spines['top'].set_color('#dddddd') 
ax.spines['right'].set_color('red')
ax.spines['left'].set_color('red')

使用以下内容仅更改刻度:

ax.tick_params(axis='x', colors='red')
ax.tick_params(axis='y', colors='red')

以下仅更改标签:

ax.yaxis.label.set_color('red')
ax.xaxis.label.set_color('red')

最后标题:

ax.title.set_color('red')

答案 1 :(得分:19)

为了记录,这就是我设法让它发挥作用的方式:

fig = pylab.figure()
ax  = fig.add_subplot(1, 1, 1)
for child in ax.get_children():
    if isinstance(child, matplotlib.spines.Spine):
        child.set_color('#dddddd')

答案 2 :(得分:12)

您可以通过调整默认的rc设置来完成。

import matplotlib
from matplotlib import pyplot as plt

matplotlib.rc('axes',edgecolor='r')
plt.plot([0, 1], [0, 1])
plt.savefig('test.png')
相关问题