2012-08-23 50 views
5

我在matplotlib中使用inset axes來放大特定y範圍([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) 

如何添加插入軸並放大兩者的第一個boxplot?我怎樣才能做到這一點?

編輯:我想下面的代碼,但這裏是我的了: 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

我不知道我知道你所說的「在同一圖中多個箱線圖」的意思。你有多個子圖嗎? – samb8s

+0

不,'dataToPlot'包含多個數據樣本,而'plt.boxplot'則將其視爲:它會繪製儘可能多的箱型圖,因爲其輸入中有樣本。 –

+0

所以,你不能只是做另一個 'axins = zoomed_inset_axes(ax,6,loc = 2)' 併爲這個下一個圖設置不同的座標範圍嗎? – samb8s

回答

14

loc的確定縮放軸的位置,1 upper right,2 upper left等。我稍微修改了示例代碼以生成多個縮放軸。

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:

同樣的,你也可以在箱線圖添加縮放軸。下面是一個例子

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

謝謝。我編輯了我的文章,並添加了代碼的輸出和關於所使用的一些參數的問題。 –