2012-08-23 13 views
5

मेरे पास matplotlib में कुछ बॉक्सप्लॉट हैं जो मैं inset axes का उपयोग कर किसी विशेष वाई-रेंज ([0,0.1]) पर ज़ूम इन करना चाहता हूं। प्रलेखन में example से मुझे यह स्पष्ट नहीं है कि मुझे एक ही आंकड़े पर एकाधिक बॉक्सप्लॉट्स के लिए यह कैसे करना चाहिए। मैं इस उदाहरण को प्रदान किए गए कोड को संशोधित करने की कोशिश कर रहा था, लेकिन बहुत अधिक अनावश्यक जटिलता थी। मेरा कोड बहुत आसान है:matplotlib: एकाधिक बॉक्सप्लॉट्स के लिए इन्सेट अक्षएं

# dataToPlot is a list of lists, containing some data. 
plt.figure() 
plt.boxplot(dataToPlot) 
plt.savefig('image.jpeg', bbox_inches=0) 

मैं दो के पहले बॉक्सप्लॉट पर इन्सेट अक्ष और ज़ूम कैसे जोड़ूं? मैं दोनों के लिए यह कैसे कर सकता हूं?

संपादित करें: मैं नीचे दिए गए कोड की कोशिश की, लेकिन यहाँ मैं क्या मिला है: enter image description here

क्या गड़बड़ी हुई?

# what's the meaning of these two parameters? 
fig = plt.figure(1, [5,4]) 
# what does 111 mean? 
ax = fig.add_subplot(111) 
ax.boxplot(data) 
# ax.set_xlim(0,21) # done automatically based on the no. of samples, right? 
# ax.set_ylim(0,300) # done automatically based on max value in my samples, right? 
# Create the zoomed axes 
axins = zoomed_inset_axes(ax, 6, loc=1) # zoom = 6, location = 1 (upper right) 
axins.boxplot(data) 
# sub region of the original image 
#here I am selecting the first boxplot by choosing appropriate values for x1 and x2 
# on the y-axis, I'm selecting the range which I want to zoom in, right? 
x1, x2, y1, y2 = 0.9, 1.1, 0.0, 0.01 
axins.set_xlim(x1, x2) 
axins.set_ylim(y1, y2) 
# even though it's false, I still see all numbers on both axes, how do I remove them? 
plt.xticks(visible=False) 
plt.yticks(visible=False) 
# draw a bbox of the region of the inset axes in the parent axes and 
# connecting lines between the bbox and the inset axes area 
# what are fc and ec here? where do loc1 and loc2 come from? 
mark_inset(ax, axins, loc1=2, loc2=4, fc="none", ec="0.5") 
plt.savefig('img.jpeg', bbox_inches=0) 
+0

मुझे यकीन है कि मैं जानता हूँ कि आप "एक ही आंकड़ा पर कई boxplots" द्वारा क्या मतलब है नहीं कर रहा हूँ। क्या आपके पास एकाधिक सबप्लॉट हैं? – samb8s

+0

नहीं, 'डेटा टॉप्लॉट' में डेटा के एक से अधिक नमूने हैं, और 'plt.boxplot' इस तरह से व्यवहार करता है: यह कई इनपुट प्लॉट्स खींचता है क्योंकि इसके इनपुट में नमूने हैं। –

+0

तो, क्या आप बस एक और 'axins = zoomed_inset_axes (ax, 6, loc = 2)' और इस अगली साजिश के लिए अलग समन्वय सीमा सेट नहीं कर सकते हैं? – samb8s

उत्तर

14

loc ज़ूम अक्ष के स्थान, upper right के लिए 1, upper left के लिए 2 और इतने पर निर्धारित करता है। मैंने एकाधिक ज़ूम किए गए अक्ष उत्पन्न करने के लिए उदाहरण कोड को थोड़ा सा संशोधित किया।

import matplotlib.pyplot as plt 

from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes 
from mpl_toolkits.axes_grid1.inset_locator import mark_inset 

import numpy as np 

def get_demo_image(): 
    from matplotlib.cbook import get_sample_data 
    import numpy as np 
    f = get_sample_data("axes_grid/bivariate_normal.npy", asfileobj=False) 
    z = np.load(f) 
    # z is a numpy array of 15x15 
    return z, (-3,4,-4,3) 


fig = plt.figure(1, [5,4]) 
ax = fig.add_subplot(111) 

# prepare the demo image 
Z, extent = get_demo_image() 
Z2 = np.zeros([150, 150], dtype="d") 
ny, nx = Z.shape 
Z2[30:30+ny, 30:30+nx] = Z 

# extent = [-3, 4, -4, 3] 
ax.imshow(Z2, extent=extent, interpolation="nearest", 
      origin="lower") 

axins = zoomed_inset_axes(ax, 6, loc=1) # zoom = 6 
axins.imshow(Z2, extent=extent, interpolation="nearest", 
      origin="lower") 

# sub region of the original image 
x1, x2, y1, y2 = -1.5, -0.9, -2.5, -1.9 
axins.set_xlim(x1, x2) 
axins.set_ylim(y1, y2) 

axins1 = zoomed_inset_axes(ax, 8, loc=2) # zoom = 6 
axins1.imshow(Z2, extent=extent, interpolation="nearest", 
      origin="lower") 

# sub region of the original image 
x1, x2, y1, y2 = -1.2, -0.9, -2.2, -1.9 
axins1.set_xlim(x1, x2) 
axins1.set_ylim(y1, y2) 

plt.xticks(visible=False) 
plt.yticks(visible=False) 

# draw a bbox of the region of the inset axes in the parent axes and 
# connecting lines between the bbox and the inset axes area 
mark_inset(ax, axins, loc1=2, loc2=4, fc="none", ec="0.5") 
mark_inset(ax, axins1, loc1=2, loc2=4, fc="none", ec="0.5") 

plt.draw() 
plt.show() 

enter image description here

Edit1:

इसी तरह, आप भी ज़ूम अक्ष boxplot में जोड़ सकते हैं। यहाँ एक उदाहरण

from pylab import * 
from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes 
from mpl_toolkits.axes_grid1.inset_locator import mark_inset 

# fake up some data 
spread= rand(50) * 100 
center = ones(25) * 50 
flier_high = rand(10) * 100 + 100 
flier_low = rand(10) * -100 
data =concatenate((spread, center, flier_high, flier_low), 0) 

# fake up some more data 
spread= rand(50) * 100 
center = ones(25) * 40 
flier_high = rand(10) * 100 + 100 
flier_low = rand(10) * -100 
d2 = concatenate((spread, center, flier_high, flier_low), 0) 
data.shape = (-1, 1) 
d2.shape = (-1, 1) 
data = [data, d2, d2[::2,0]] 

# multiple box plots on one figure 
fig = plt.figure(1, [5,4]) 
ax = fig.add_subplot(111) 
ax.boxplot(data) 
ax.set_xlim(0.5,5) 
ax.set_ylim(0,300) 

# Create the zoomed axes 
axins = zoomed_inset_axes(ax, 3, loc=1) # zoom = 3, location = 1 (upper right) 
axins.boxplot(data) 

# sub region of the original image 
x1, x2, y1, y2 = 0.9, 1.1, 125, 175 
axins.set_xlim(x1, x2) 
axins.set_ylim(y1, y2) 
plt.xticks(visible=False) 
plt.yticks(visible=False) 

# draw a bbox of the region of the inset axes in the parent axes and 
# connecting lines between the bbox and the inset axes area 
mark_inset(ax, axins, loc1=2, loc2=4, fc="none", ec="0.5") 

show() 

enter image description here

EDIT2

मामले में वितरण विषम है, यानी, अधिकांश मान कुछ बहुत बड़ी मूल्यों के साथ छोटे हैं है, ऊपर ज़ूमिंग प्रक्रिया काम नहीं कर सकते, क्योंकि यह x दोनों के साथ-साथ y धुरी ज़ूम करेगा। उस स्थिति में y-axis से log के पैमाने को बदलना बेहतर है।

from pylab import * 

# fake up some data 
spread= rand(50) * 1 
center = ones(25) * .5 
flier_high = rand(10) * 100 + 100 
flier_low = rand(10) * -100 
data =concatenate((spread, center, flier_high, flier_low), 0) 

# fake up some more data 
spread= rand(50) * 1 
center = ones(25) * .4 
flier_high = rand(10) * 100 + 100 
flier_low = rand(10) * -100 
d2 = concatenate((spread, center, flier_high, flier_low), 0) 
data.shape = (-1, 1) 
d2.shape = (-1, 1) 
data = [data, d2, d2[::2,0]] 

# multiple box plots on one figure 
fig = plt.figure(1, [5,4]) # Figure Size 
ax = fig.add_subplot(111) # Only 1 subplot 
ax.boxplot(data) 
ax.set_xlim(0.5,5) 
ax.set_ylim(.1,300) 
ax.set_yscale('log') 

show() 

enter image description here

+0

धन्यवाद। इस पर प्रलेखन अभी भी मेरे उद्देश्यों के लिए बहुत जटिल है। मैंने अपनी मूल पोस्ट को तनाव में संपादित किया जो मैं अभी कर रहा हूं और ऑनलाइन उदाहरण से सभी अनावश्यक लाइनों को हटा दिया। क्या आप कृपया मेरे कोड को संशोधित कर सकते हैं, इसलिए मैं देख सकता हूं कि मुझे वास्तव में क्या करना चाहिए? धन्यवाद। –

+1

संपादित उत्तर की जांच करें। यदि आपको कोड के किसी भी विशिष्ट भाग को समझने में कठिनाई है, तो मुझे बताएं। – imsc

+0

धन्यवाद। मैंने अपनी पोस्ट संपादित की और इस्तेमाल किए गए कुछ पैरामीटर पर आपके कोड और प्रश्नों के आउटपुट को जोड़ा। –