2009-12-30 152 views
33

有没有办法在matplotlib中更改坐标轴的颜色(不是嘀嗒声)?我一直在浏览Axes,Axis和Artist的文档,但没有运气; matplotlib画廊也没有提示。 有什么想法?Matplotlib:更改轴的颜色

回答

61

当使用数字,你可以很容易地改变与脊柱颜色:

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

优秀的答案,谢谢!给其他人一个注释:''ax.tick_params(axis ='x',colors ='red',which ='both')'' - which =“both”同时改变主要和次要的刻度颜色。 – kinverarity 2015-03-10 10:53:24

+0

'ax.tick_params(axis ='x',colors ='red')'似乎改变了勾号和标签的颜色... – Jonathan 2016-02-16 17:00:30

+0

是否可以使用'ax.yaxis.label.set_color('灰色')'这样一种方式,只有从'y1'到'y2'的嘀嗒声改变他们的颜色,而其他的仍然没有改变? – FaCoffee 2016-03-31 14:27:20

16

为了记录在案,我这是怎么设法使其工作:

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

+1,比使用全局rc好多了。 – Mark 2010-01-01 19:15:21

+0

感谢您的这一点,希望matplotlib将增加一个更简单的方法来实现这一点。 – jhanifen 2010-10-13 17:07:13

7

您可以通过调整默认的控制设定做。

import matplotlib 
from matplotlib import pyplot as plt 

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

Matplotlib还有一个[上下文管理器](http://matplotlib.org/users/style_sheets.html#temporary-styling),它允许临时更改rc参数http://stackoverflow.com/a/41527038/2166823 – 2017-01-07 22:08:33