100

有没有办法改变 matplotlib 中轴(不是刻度)的颜色?我一直在查看 Axes、Axis 和 Artist 的文档,但没有运气;matplotlib 库也没有任何提示。任何的想法?

4

4 回答 4

188

使用图形时,您可以通过以下方式轻松更改书脊颜色:

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

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

  • which="both"更改主要和次要刻度颜色
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')
于 2012-08-21T16:42:42.313 回答
23

为了记录,这就是我设法使它工作的方式:

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')
于 2010-01-01T19:04:10.103 回答
22

您可以通过调整默认的 rc 设置来做到这一点。

import matplotlib
from matplotlib import pyplot as plt

matplotlib.rc('axes',edgecolor='r')
plt.plot([0, 1], [0, 1])
plt.savefig('test.png')
于 2010-01-01T19:14:36.900 回答
1

全局设置所有轴的边缘颜色:

matplotlib.rcParams['axes.edgecolor'] = '#ff0000'
于 2021-12-27T05:27:29.663 回答