2010-12-01 16 views
23

में एक सबप्लॉट सहेजें, क्या मैटलप्लिब आकृति में एक व्यक्तिगत सबप्लॉट को सहेजना संभव है? मैंmatplotlib

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]) 

है यह अलग फ़ाइलों के लिए दो subplots में से प्रत्येक को बचाने के लिए या कम से कम उन्हें एक नया आंकड़ा करने के लिए अलग से कॉपी उन्हें बचाने के लिए संभव है का कहना है की सुविधा देता है?

मैं RHEL 5.

धन्यवाद पर matplotlib के संस्करण 1.0.0 का उपयोग कर रहा,

रॉबर्ट

उत्तर

40

जबकि @Eli काफी सही है कि वहां आम तौर पर करने के लिए एक की जरूरत के ज्यादा नहीं है यह, यह संभव है। savefigbbox_inches तर्क लेता है जिसका उपयोग किसी छवि के आंकड़े को केवल छवि में सहेजने के लिए किया जा सकता है।

यहां एक त्वरित उदाहरण है:

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)) 

पूरा आंकड़ा: Full Example Figure


क्षेत्र अंदर दूसरा subplot: Inside second subplot


y-दिशा में एक्स-दिशा में 10% और 20% से गद्देदार दूसरा subplot आसपास

क्षेत्र: here से 3 साल बाद Full second subplot

+4

+1 की कमी: वाह! काश मैं Matplotlib के बारे में अधिक जानने की कोशिश करते हुए इन तरीकों से आया था! यह बहुत अच्छा होगा अगर आधिकारिक दस्तावेज ने इच्छुक पाठकों को मैटलप्लिब के इन उपयोगी कोनों में निर्देशित किया, और यदि प्रासंगिक अवधारणाओं की प्रस्तुति अधिक संरचित थी। :) – EOL

+0

बहुत बहुत धन्यवाद, यही वह है जिसे मैं ढूंढ रहा था! –

+0

एक दिन जब आप कुछ नया नहीं सीखते हैं तो एक बुरा दिन है ... ठीक है ++ –

13

@Joe द्वारा एक जवाब में full_extent() समारोह को लागू करना, आप ठीक वही मिल सकता है जो ओपी ढूंढ रहा था। वैकल्पिक रूप से, आप Axes.get_tightbbox() जो एक छोटे से तंग सीमांकन बॉक्स

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) 

मैं एक तस्वीर पोस्ट चाहते हैं देता है का उपयोग कर सकते, लेकिन मैं प्रतिष्ठा अंक

+0

यह उत्तर आइटम + = [ax.get_xaxis()। Get_label(), ax.get_yaxis(), get_label()] जोड़कर टेक्स्ट लेबल को शामिल करने के लिए बढ़ाया जा सकता है। इससे पहले कि मैंने इसे जोड़ा तो उन्हें काट दिया गया। – Erotemic