2011-10-15 50 views
20

這是對this後的一種後續問題,其中討論了軸,蜱和標籤的着色。我希望爲此打開一個新的擴展問題是可以的。在matplotlib中優雅地改變情節框架的顏色

用軸[ax1,ax2]圍繞雙圖(通過add_subplot)更改完整框架(滴答和軸)的顏色會產生大量代碼。這段代碼改變上面的曲線的框架的顏色:(!)

ax1.spines['bottom'].set_color('green') 
ax1.spines['top'].set_color('green') 
ax1.spines['left'].set_color('green') 
ax1.spines['right'].set_color('green') 
for t in ax1.xaxis.get_ticklines(): t.set_color('green') 
for t in ax1.yaxis.get_ticklines(): t.set_color('green') 
for t in ax2.xaxis.get_ticklines(): t.set_color('green') 
for t in ax2.yaxis.get_ticklines(): t.set_color('green') 

因此改變兩幅地塊的邊框顏色有兩個Y軸各,我需要16行代碼..這是它的樣子:

enter image description here

其他方法我挖出至今:

  • matplotlib.rc:討論here;全球變化,而非本地變化。我想要用不同顏色的其他情節。請,不愁太多的顏色在情節的討論... :-)

    matplotlib.rc('axes',edgecolor='green') 
    
  • 挖掘出軸的刺,然後改變它:還討論here;我覺得不是很優雅。

    for child in ax.get_children(): 
        if isinstance(child, matplotlib.spines.Spine): 
         child.set_color('#dddddd') 
    

有冷凝以上塊的優雅方式,一些 更 「Python化」?

我在ubuntu下使用python 2.6.5和matplotlib 0.99.1.1。

回答

12

重構上面的代碼:

import matplotlib.pyplot as plt 

for ax, color in zip([ax1, ax2, ax3, ax4], ['green', 'green', 'blue', 'blue']): 
    plt.setp(ax.spines.values(), color=color) 
    plt.setp([ax.get_xticklines(), ax.get_yticklines()], color=color) 
+0

不錯的一個。請包括雙色,我會'接受'你的答案。 (如:斧頭,拉鍊顏色([ax1,ax2,ax3,ax4],['green','green','blue','blue']): plt.setp(ax.spines.values (),color = color); plt.setp([ax.get_xticklines(),ax.get_yticklines()],color = color)') – Boffin

+0

@Boffin done :) – ianalis

+0

在你的最後一行我認爲你的意思是'ax1 '只是'ax' – Joel

9

假設你正在使用一個合理了最新版本matplotlib的(> = 1.0),也許嘗試這樣的事情:

import matplotlib.pyplot as plt 

# Make the plot... 
fig, axes = plt.subplots(nrows=2) 
axes[0].plot(range(10), 'r-') 
axes[1].plot(range(10), 'bo-') 

# Set the borders to a given color... 
for ax in axes: 
    ax.tick_params(color='green', labelcolor='green') 
    for spine in ax.spines.values(): 
     spine.set_edgecolor('green') 

plt.show() 

enter image description here

+0

我跑matplotlib 0.99.1.1(D'哦!)。我也沒有很好地解釋雙重情節。我更新了最初的問題並添加了一個圖形。你展示了另一種着色的可能性,我可以添加到我的列表中(如果運行最新版本的m.p.l.)。如果您在一個情節中使用兩個y軸對框架進行着色,是否還會更短? – Boffin

3

也許這是一個有點粗到回答我自己的問題,但我想分享我迄今可以找到的內容。該版本可以使用兩種不同顏色的軸[ax1, ax2][ax3, ax4]對兩個子圖進行着色。這是很多短於我在上面的問題中陳述的16條線。它的靈感來自於Joe Kington的回答,並在twinx kills tick label color

import matplotlib.pyplot as plt 
import numpy as np 

# Generate some data 
num = 200 
x = np.linspace(501, 1200, num) 
yellow_data, green_data , blue_data= np.random.random((3,num)) 
green_data += np.linspace(0, 3, yellow_data.size)/2 
blue_data += np.linspace(0, 3, yellow_data.size)/2 

fig = plt.figure() 
plt.subplot(211) # Upper Plot 
ax1 = fig.add_subplot(211) 
ax1.fill_between(x, 0, yellow_data, color='yellow') 
ax2 = ax1.twinx() 
ax2.plot(x, green_data, 'green') 
plt.setp(plt.gca(), xticklabels=[]) 
plt.subplot(212) # Lower Plot 
ax3 = fig.add_subplot(212) 
ax3.fill_between(x, 0, yellow_data, color='yellow') 
ax4 = ax3.twinx() 
ax4.plot(x, blue_data, 'blue') 

# Start coloring 
for ax, color in zip([ax1, ax2, ax3, ax4], ['green', 'green', 'blue', 'blue']): 
    for ticks in ax.xaxis.get_ticklines() + ax.yaxis.get_ticklines(): 
     ticks.set_color(color) 
    for pos in ['top', 'bottom', 'right', 'left']: 
     ax.spines[pos].set_edgecolor(color) 
# End coloring 

plt.show() 

color_plots

標誌着我這是公認的,因爲它是我能找到的迄今爲止最緊湊的解決方案。儘管如此,我仍然樂於接受其他更好的方法來解決這個問題。