2013-05-17 22 views
6

Công cụ make_axes_locatable của Matplotlib cho phép bạn tack trục mới vào bên cạnh trục hiện có. Tuy nhiên, nó thay đổi kích thước trục chính. Có cách nào để tránh điều này không?Tạo dải phân cách mà không thay đổi kích thước của trục gốc?

Dưới đây là một ví dụ hoàn chỉnh cho thấy vấn đề và làm thế nào để tái tạo nó:

import matplotlib.pyplot as pl 
from mpl_toolkits.axes_grid import make_axes_locatable 
import matplotlib.axes as maxes 


fig = pl.figure() 
ax1=pl.subplot(1,3,1) 
ax1.imshow([[0,1],[2,0]]) 
ax1.yaxis.set_visible(False) 
ax1.xaxis.set_visible(False) 
ax2=pl.subplot(1,3,2) 
ax2.imshow([[0,1],[2,0]]) 
ax2.yaxis.set_visible(False) 
ax2.xaxis.set_visible(False) 
ax3=pl.subplot(1,3,3) 
ax3.imshow([[0,1],[2,0]]) 
ax3.yaxis.set_visible(False) 
ax3.xaxis.set_visible(False) 
pl.subplots_adjust(wspace=0) 


divider = make_axes_locatable(ax1) 
cax1 = divider.new_horizontal(size=0.2, pad=0.0, pack_start=True, axes_class=maxes.Axes) 
pl.colorbar(ax1.images[0],cax=cax1) 
cax1.yaxis.set_label_position('left') 
cax1.yaxis.set_ticks_position('left') 
fig.add_axes(cax1) 

divider = make_axes_locatable(ax2) 
cax2 = divider.new_vertical(size=0.2, pad=0.0, pack_start=True, axes_class=maxes.Axes) 
fig.add_axes(cax2) 
pl.colorbar(ax2.images[0],cax=cax2,orientation='horizontal') 
# thin out the tick labels for visibility 
for t in cax2.xaxis.get_majorticklabels()[::2]: 
    t.set_visible(False) 


divider = make_axes_locatable(ax3) 
cax3 = divider.new_horizontal(size=0.2, pad=0.0, pack_start=False, axes_class=maxes.Axes) 
pl.colorbar(ax3.images[0],cax=cax3) 
fig.add_axes(cax3) 

image with missized parents

Vấn đề là các ô phụ đang kích cỡ khác nhau. Tôi nghĩ rằng bên trái và bên phải đã bị thu hẹp, nhưng phần giữa không thay đổi.

Trả lời

2

Tôi đã có thể tránh việc thay đổi kích thước của cốt truyện gốc bằng cách sửa đổi mã của bạn để tạo các trục mới cho mỗi thanh màu & rồi đặt từng cái theo cách thủ công. Đó là một công việc nhiều hơn một chút, nhưng tôi nghĩ rằng nó gần với kết quả bạn đang tìm kiếm. Lưu ý rằng sự xuất hiện thẩm mỹ thực tế của các ô có chút khác biệt so với bạn - có lẽ vì tôi đang sử dụng phiên bản mới hơn của matplotlib (1.2.1).

%pylab inline 
import matplotlib.pyplot as pl 

fig = pl.figure() 
ax1=pl.subplot(1,3,1) 
ax1.imshow([[0,1],[2,0]]) 
ax1.yaxis.set_visible(False) 
ax1.xaxis.set_visible(False) 
ax2=pl.subplot(1,3,2) 
ax2.imshow([[0,1],[2,0]]) 
ax2.yaxis.set_visible(False) 
ax2.xaxis.set_visible(False) 
ax3=pl.subplot(1,3,3) 
ax3.imshow([[0,1],[2,0]]) 
ax3.yaxis.set_visible(False) 
ax3.xaxis.set_visible(False) 
pl.subplots_adjust(wspace=0) 

#Give the colorbar its own axis to avoid resizing the parent axis: 
width = 0.02 
height = 0.38 
vertical_position = 0.32 
horizontal_position = 0.1 
axColor = pl.axes([horizontal_position, vertical_position, width, height]) #the new axis for first colorbar 
pl.colorbar(ax1.images[0],cax=axColor,orientation='vertical') 
axColor.yaxis.set_label_position('left') 
axColor.yaxis.set_ticks_position('left') 

#likewise for the other colorbars with appropriately adjusted positions/ orientations: 
horizontal_position= 0.38 
vertical_position = 0.29 
height = 0.03 
width = 0.26 
axColor2 = pl.axes([horizontal_position, vertical_position, width, height]) #the new axis for second colorbar 
pl.colorbar(ax2.images[0],cax=axColor2,orientation='horizontal') 
# thin out the tick labels for visibility 
for t in axColor2.xaxis.get_majorticklabels()[::2]: 
    t.set_visible(False) 

width = 0.02 
height = 0.38 
vertical_position = 0.32 
horizontal_position = 0.905 
axColor3 = pl.axes([horizontal_position, vertical_position, width, height]) #the new axis for third colorbar  
pl.colorbar(ax3.images[0],cax=axColor3,orientation='vertical') 

enter image description here

+0

Sự khác biệt "thẩm mỹ" là vì mục cấu hình mặc định của tôi; Tôi có 'interpolation = 'recent'' được thiết lập theo mặc định. – keflavich

+0

Cách tiếp cận chung này dường như hoạt động, nhưng sẽ tốt hơn nếu có cách tạo chiều rộng/chiều cao/vị trí dọc/ngang tự động hơn là mã hóa chúng. – keflavich

Các vấn đề liên quan