2012-09-11 9 views
5

के ग्रिड में एक सबप्लॉट को पुनर्स्थापित करना मैं 7 सबप्लॉट्स के साथ साजिश बनाने की कोशिश कर रहा हूं। फिलहाल मैं दो कॉलम, चार भूखंडों के साथ एक और अन्य तीन के साथ, इस तरह की साजिश रचने हूँ अर्थात्: enter image description hereMatplotlib: सबप्लॉट

मैं folowing तरीके से इस साजिश का निर्माण कर रहा हूँ: प्रकाशन के लिए,

#! /usr/bin/env python 
    import numpy as plotting 
    import matplotlib 
    from pylab import * 
    x = np.random.rand(20) 
    y = np.random.rand(20) 
    fig = figure(figsize=(6.5,12)) 
    subplots_adjust(wspace=0.2,hspace=0.2) 
    iplot = 420 
    for i in range(7): 
     iplot += 1 
     ax = fig.add_subplot(iplot) 
     ax.plot(x,y,'ko') 
     ax.set_xlabel("x") 
     ax.set_ylabel("y") 
    savefig("subplots_example.png",bbox_inches='tight') 

हालांकि मैं लगता है कि यह थोड़ा बदसूरत दिखता है - मैं जो करना चाहता हूं वह अंतिम उप-स्थान को दो स्तंभों के बीच केंद्र में ले जाया जाता है। तो, आखिरी सबप्लॉट की स्थिति को समायोजित करने का सबसे अच्छा तरीका क्या है ताकि यह केंद्रित हो? अर्थात। 3X2 ग्रिड में पहले 6 सबप्लॉट्स और दो स्तंभों के बीच केंद्रित अंतिम उप-स्थान प्राप्त करने के लिए। यदि संभव हो तो, मैं इतना है कि मैं बस का उपयोग कर सकते for पाश रखने के लिए सक्षम होने के लिए करना चाहते हैं:

if i == 6: 
     # do something to reposition/centre this plot  

धन्यवाद,

एलेक्स

+0

यह एक 3x2 ग्रिड होना जरूरी है? – Harpal

उत्तर

5

, आप जो की अनुमति देता है subplot2grid के साथ अपने भूखंडों की व्यवस्था कर सकते,: प्रत्येक भूखंड अवधि जैसे 2 कॉलम है एक colspan पैरामीटर के लिए:

#! /usr/bin/env python 
import numpy as plotting 
import matplotlib 
from pylab import * 
x = np.random.rand(20) 
y = np.random.rand(20) 
fig = figure(figsize=(6.5,12)) 
subplots_adjust(wspace=0.2,hspace=0.2) 
iplot = 420 
for i in range(7): 
    iplot += 1 
    if i == 6: 
     ax = subplot2grid((4,8), (i/2, 2), colspan=4) 
    else: 
     # You can be fancy and use subplot2grid for each plot, which dosen't 
     # require keeping the iplot variable: 
     # ax = subplot2grid((4,2), (i/2,i%2)) 

     # Or you can keep using add_subplot, which may be simpler: 
     ax = fig.add_subplot(iplot) 
    ax.plot(x,y,'ko') 
    ax.set_xlabel("x") 
    ax.set_ylabel("y") 
savefig("subplots_example.png",bbox_inches='tight') 

subplots in a grid with colspan

8

उपयोग ग्रिड कल्पना (doc) एक 4x4 ग्रिड के साथ, और आप पाश के लिए रखना चाहते हैं

import matplotlib.gridspec as gridspec 
gs = gridspec.GridSpec(4, 4) 
ax1 = plt.subplot(gs[0, 0:2]) 
ax2 = plt.subplot(gs[0,2:]) 
ax3 = plt.subplot(gs[1,0:2]) 
ax4 = plt.subplot(gs[1,2:]) 
ax5 = plt.subplot(gs[2,0:2]) 
ax6 = plt.subplot(gs[2,2:]) 
ax7 = plt.subplot(gs[3,1:3]) 
fig = gcf() 
gs.tight_layout(fig) 
ax_lst = [ax1,ax2,ax3,ax4,ax5,ax6,ax7] 
+0

यह पूरी तरह से काम करता है। आप मानक matplotlib तकनीकों का उपयोग करके लेबल किए गए अक्ष आदि के लिए रिक्ति को नियंत्रित कर सकते हैं, आईई 'अंजीर = plt.figure (figsize = (10,15))' प्रत्येक सबप्लॉट/ग्रिड 'जीएस को कॉल करने के बाद .tight_layout (अंजीर) ' – AGS

+0

एक बहुत अच्छा जवाब! धन्यवाद! –