2011-01-21 934 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] 
相關問題