Make a separator without resizing the original axis?

The Matplotlib make_axes_locatable allows make_axes_locatable to snap a new axis to the side of an existing axis. However, it resizes the parent axis. Is there any way to avoid this?

The following is a complete example showing the problem and how to reproduce it:

 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

The problem is that the subheadings now have different sizes. I think the left and right have contracted, but the middle has not changed.

+6
source share
1 answer

I managed to avoid resizing the parent area by changing the code to create new axes for each color panel, and then placing them manually. This is a bit more work, but I think it is close to the result you are looking for. Please note that the actual aesthetic appearance of the sites is slightly different from yours - perhaps because I am using a newer version of 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

+2
source

Source: https://habr.com/ru/post/945304/


All Articles