2010-12-01 44 views
23

'daki bir altpipi kaydetme, bir matplotlib biçimindeki tek bir altpipini (png'ye) kaydetmek mümkün mü? Benmatplotlib

import pylab as p 
ax1 = subplot(121) 
ax2 = subplot(122) 
ax.plot([1,2,3],[4,5,6]) 
ax.plot([3,4,5],[7,8,9]) 

farklı dosyalara iki subplots her kaydetmek ya da en azından onları kurtarmak için yeni bir şekle ayrı ayrı kopyalamak mümkün mü var Diyelim?

Ben RHEL 5.

Teşekkür üzerinde matplotlib sürümünü 1.0.0 kullanıyorum,

Robert

cevap

40

@Eli genellikle yapmak için bir ihtiyaç yok çok oldukça doğru olsa bu mümkün. savefig, bir şeklin yalnızca bir bölümünü bir görüntüye seçici olarak kaydetmek için kullanılabilen bir bbox_inches argümanını alır. İşte

hızlı bir örnek:

import matplotlib.pyplot as plt 
import matplotlib as mpl 
import numpy as np 

# Make an example plot with two subplots... 
fig = plt.figure() 
ax1 = fig.add_subplot(2,1,1) 
ax1.plot(range(10), 'b-') 

ax2 = fig.add_subplot(2,1,2) 
ax2.plot(range(20), 'r^') 

# Save the full figure... 
fig.savefig('full_figure.png') 

# Save just the portion _inside_ the second axis's boundaries 
extent = ax2.get_window_extent().transformed(fig.dpi_scale_trans.inverted()) 
fig.savefig('ax2_figure.png', bbox_inches=extent) 

# Pad the saved area by 10% in the x-direction and 20% in the y-direction 
fig.savefig('ax2_figure_expanded.png', bbox_inches=extent.expanded(1.1, 1.2)) 

tam rakam: Full Example Figure


Alan ikinci subplot iç: Inside second subplot


y-ekseni doğrultusunda x-yönünde% 10 ve% 20 oranında doldurulur, ikinci alt planda yaklaşık

Konum: here 3 yıl sonra Full second subplot

+4

1 yoksun: Vay! Matplotlib hakkında daha fazla şey öğrenmeye çalışırken bu yöntemlere rastladım! Eğer resmi belgeler ilgilenen okuyucuları Matplotlib'in bu yararlı köşelerine yöneltmişse ve ilgili kavramların sunumu daha yapılandırılmışsa, bu harika olurdu. :) – EOL

+0

Çok teşekkürler, aradığım şey bu! –

+0

Yeni bir şey öğrenmediğiniz bir gün kötü bir gün ... İyi işlenmiş ++ –

13

@Joe tarafından bir cevap olarak full_extent() fonksiyonu uygulayarak, sen OP'nin tam olarak ne aradığını bulabilir. Alternatif olarak, bir pic sonrası ediyorum biraz daha sıkı sınırlayıcı kutu

import matplotlib.pyplot as plt 
import matplotlib as mpl 
import numpy as np 
from matplotlib.transforms import Bbox 

def full_extent(ax, pad=0.0): 
    """Get the full extent of an axes, including axes labels, tick labels, and 
    titles.""" 
    # For text objects, we need to draw the figure first, otherwise the extents 
    # are undefined. 
    ax.figure.canvas.draw() 
    items = ax.get_xticklabels() + ax.get_yticklabels() 
# items += [ax, ax.title, ax.xaxis.label, ax.yaxis.label] 
    items += [ax, ax.title] 
    bbox = Bbox.union([item.get_window_extent() for item in items]) 

    return bbox.expanded(1.0 + pad, 1.0 + pad) 

# Make an example plot with two subplots... 
fig = plt.figure() 
ax1 = fig.add_subplot(2,1,1) 
ax1.plot(range(10), 'b-') 

ax2 = fig.add_subplot(2,1,2) 
ax2.plot(range(20), 'r^') 

# Save the full figure... 
fig.savefig('full_figure.png') 

# Save just the portion _inside_ the second axis's boundaries 
extent = full_extent(ax2).transformed(fig.dpi_scale_trans.inverted()) 
# Alternatively, 
# extent = ax.get_tightbbox(fig.canvas.renderer).transformed(fig.dpi_scale_trans.inverted()) 
fig.savefig('ax2_figure.png', bbox_inches=extent) 

veren Axes.get_tightbbox() kullanabilirsiniz ama itibar puanı

+0

Bu cevap, + = [ax.get_xaxis(). Get_label(), ax.get_yaxis(). Get_label()] öğelerini ekleyerek metin etiketlerini içerecek şekilde genişletilebilir. Bunu eklemeden önce kesildiler. – Erotemic