2011-01-21 1725 views

回答

73

作为一个简单的例子(使用比潜在的重复问题稍微干净法):

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() 

alt text

+0

谢谢你到目前为止。 ax.tick_params(axis ='x',colors ='red') 产生一个AxesSubplot没有属性'tick_params'错误。你知道为什么吗? – 2011-01-21 17:51:24

11

如果您有要修改几个数字或次要情节,它可以帮助使用matplotlib context manager更改颜色,而不是单独更改每个颜色。上下文管理器允许您临时更改rc参数,仅用于紧跟在后面的缩进代码,但不会影响全局rc参数。

这段代码产生两个数字,第一个数字是轴的修改颜色,ticks和ticklabels,第二个数字是默认的rc参数。

import matplotlib.pyplot as plt 
with plt.rc_context({'axes.edgecolor':'orange', 'xtick.color':'red', 'ytick.color':'green', 'figure.facecolor':'white'}): 
    # Temporary rc parameters in effect 
    fig, (ax1, ax2) = plt.subplots(1,2) 
    ax1.plot(range(10)) 
    ax2.plot(range(10)) 
# Back to default rc parameters 
fig, ax = plt.subplots() 
ax.plot(range(10)) 

enter image description here

enter image description here

您可以键入plt.rcParams查看所有可用率控制参数,并使用列表理解搜索关键字:

# Search for all parameters containing the word 'color' 
[(param, value) for param, value in plt.rcParams.items() if 'color' in param] 
相关问题