2011-06-30 17 views
167

'daki birçok alt nokta ile alt nokta boyutunu/aralığını geliştirin this question'a çok benzer, ancak bu rakamın olması gerektiği kadar büyük olabilir.Matplotlib

Matplotlib'de bir grup dikey olarak yığılmış çizim oluşturmam gerekiyor. Sonuç figsave kullanılarak kaydedilecek ve bir web sayfasında görüntülenecektir, bu yüzden son görüntülerin alt pikseller aralıklı olduğu sürece ne kadar uzun süre kaldığı umurumda değil.

Figürün ne kadar büyük olmasına izin veriyorum, alt alanlar her zaman çakışıyor gibi görünüyor.

Kodum şu anda görünüyor

gibi
import matplotlib.pyplot as plt 
import my_other_module 

titles, x_lists, y_lists = my_other_module.get_data() 

fig = plt.figure(figsize=(10,60)) 
for i, y_list in enumerate(y_lists): 
    plt.subplot(len(titles), 1, i) 
    plt.xlabel("Some X label") 
    plt.ylabel("Some Y label") 
    plt.title(titles[i]) 
    plt.plot(x_lists[i],y_list) 
fig.savefig('out.png', dpi=100) 

cevap

157
Sen (source)

çağrı imza subplots arasındaki boşluğu değiştirmek için plt.subplots_adjust kullanabilirsiniz

: (ve varsayılan önerilen)

subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=None, hspace=None) 

parametre anlamları

left = 0.125 # the left side of the subplots of the figure 
right = 0.9 # the right side of the subplots of the figure 
bottom = 0.1 # the bottom of the subplots of the figure 
top = 0.9  # the top of the subplots of the figure 
wspace = 0.2 # the amount of width reserved for blank space between subplots 
hspace = 0.2 # the amount of height reserved for white space between subplots 

fiili varsayılan rc dosyası

+0

Hspace ile uğraşmayı denedim, ancak yalnızca tüm çizgilerin üst üste binme sorununu çözmeden daha küçük hale getirdiğini düşünüyorum.Ayrıca diğer parametrelerle oynamaya çalıştım ama bilmiyorum ne sol, sağ, alt ve üst aslında orada belirterek. – mcstrother

+25

@mcstrother Eğer bir arsa gösterdikten sonra 'ayar' düğmesine tıklarsanız, bu parametrelerin 6'sını interaktif olarak değiştirebilir, daha sonra ne çalıştığını bulduktan sonra bunları kodun içine kopyalayabilirsiniz. –

+0

Bir ayar düğmesi göremiyorum. Her ne kadar ben bir Jupyter defterindeyim. % Matplotlib inline ve% matplotlib defterini denedim. –

36

tarafından kontrol edilir I (hspace = 0.001) o subplots_adjust benim için çalışmayı sona erdi neler olduğunu gördük. Boşluk kullandığımda = Yok, her arsa arasında hala beyaz boşluk var. Onu sıfıra çok yakın bir şeye ayarlamak, onları hizalamaya zorlar gibi görünüyor. Burada yüklediğim en zarif kod değil, hspace'ın nasıl çalıştığını görebilirsiniz.

import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.ticker as tic 

fig = plt.figure() 

x = np.arange(100) 
y = 3.*np.sin(x*2.*np.pi/100.) 

for i in range(5): 
    temp = 510 + i 
    ax = plt.subplot(temp) 
    plt.plot(x,y) 
    plt.subplots_adjust(hspace = .001) 
    temp = tic.MaxNLocator(3) 
    ax.yaxis.set_major_locator(temp) 
    ax.set_xticklabels(()) 
    ax.title.set_visible(False) 

plt.show() 

enter image description here

227

hızlı bir örnek olarak plt.tight_layout

kullanmayı deneyin:

import matplotlib.pyplot as plt 

fig, axes = plt.subplots(nrows=4, ncols=4) 
fig.tight_layout() # Or equivalently, "plt.tight_layout()" 

plt.show() 

Sıkı Düzen olmadan

Sıkı Düzeni ile


enter image description here

+1

Bu gerçekten harika. – Lokesh

+5

Eğer arsa kodunuzdan sonra bunu çalıştırmanız gerektiğini göstermek için biraz daha açık olurdu, ancak şovdan hemen önce() – MtRoad

+0

Bu benim için çalıştı! Teşekkürler! –

25
import matplotlib.pyplot as plt 

fig = plt.figure(figsize=(10,60)) 
plt.subplots_adjust(...) 

plt.subplots_adjust yöntemi:

def subplots_adjust(*args, **kwargs): 
    """ 
    call signature:: 

     subplots_adjust(left=None, bottom=None, right=None, top=None, 
         wspace=None, hspace=None) 

    Tune the subplot layout via the 
    :class:`matplotlib.figure.SubplotParams` mechanism. The parameter 
    meanings (and suggested defaults) are:: 

     left = 0.125 # the left side of the subplots of the figure 
     right = 0.9 # the right side of the subplots of the figure 
     bottom = 0.1 # the bottom of the subplots of the figure 
     top = 0.9  # the top of the subplots of the figure 
     wspace = 0.2 # the amount of width reserved for blank space between subplots 
     hspace = 0.2 # the amount of height reserved for white space between subplots 

    The actual defaults are controlled by the rc file 
    """ 
    fig = gcf() 
    fig.subplots_adjust(*args, **kwargs) 
    draw_if_interactive() 

veya

fig = plt.figure(figsize=(10,60)) 
fig.subplots_adjust(...) 

resim konularında boyutu.

"Hspace ile uğraşmayı denedim, ancak yalnızca artan grafikler, örtüşme sorununu çözmeden tüm grafikleri küçültüyor gibi görünüyor."

Böylece daha beyaz yer açmak ve toplam resim daha büyük olması gerekir alt arsa boyutunu tutmak için.

+0

Resmin boyutu önemli, daha büyük resim boyutu bu sorunu çözebilir! 'plt.figure set (figsize = (10, 7)) ', resmin boyutu olurdu' 2000 x 1400' pix – Belter

7

Sen subplot_tool deneyebilirsiniz()

plt.subplot_tool() 
+0

bu beni kurtardı bir sürü ince ayar, kaydedildi teşekkürler! –

+0

https://matplotlib.org/api/_as_gen/matplotlib.pyplot.subplot_tool.html sayfasına bakın. –

-1

Dont buraya mühendisi gerekir Sadece plt.tight_layout() kullanın