python - Multiple imshow-subplots, each with colorbar -


i want have figure consisting of, let's say, 4 subplots. 2 of them usual line-plots, 2 of them imshow-images.

i can format imshow-images proper plots itself, because every single 1 of them needs own colorbar, modified axis , other axis removed. this, however, seems absolutely useless subplotting. can me that?

i use displaying data of "regular" plots above colormap (by scaling input-array i [ i, i, i, i, i, ] 2d , calling imshow() it).

the following code first displays need subplot , second 1 shows can do, not sufficient.

#!/usr/bin/env python  import matplotlib.pyplot plt matplotlib.colors import lognorm  s = { 't':1, 'x':[1,2,3,4,5,6,7,8], 'd':[0.3,0.5,0.2,0.3,0.5,0.5,0.3,0.4] } width = 40  # how in 1 plot tot = [] in range(width):     tot.append(s['d'])  plt.imshow(tot, norm=lognorm(vmin=0.001, vmax=1)) plt.colorbar() plt.axes().axes.get_xaxis().set_visible(false) plt.yticks([0, 2, 4, 6], [s['x'][0], s['x'][2], s['x'][4], s['x'][6]])  plt.show()   f = plt.figure(figsize=(20,20))  plt.subplot(211) plt.plot(s['x'], s['d']) plt.ylim([0, 1])  #colorplot sp = f.add_subplot(212)  #reshape (just necessary see something) tot = [] in range(width):     tot.append(s['d'])  sp.imshow(tot, norm=lognorm(vmin=0.001, vmax=1))      #what can't needs done:     #sp.colorbar() #sp.axes().axes.get_xaxis().set_visible(false) #sp.yticks([0, 200, 400, 600, 800, 1000], [s['x'][0], s['x'][200], s['x'][400], s['x'][600], s['x'][800], s['x'][1000]])  plt.show() 

you can make use of matplotlibs object oriented interface rather state-machine interace in order better control on each axes. also, control on height/width of colorbar can make use of axesgrid toolkit of matplotlib.

for example:

import matplotlib.pyplot plt import numpy np mpl_toolkits.axes_grid1 import make_axes_locatable matplotlib.colors import lognorm matplotlib.ticker import multiplelocator  s = {'t': 1,      'x': [1, 2, 3, 4, 5, 6, 7, 8],      't': [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8],      'd': [0.3, 0.5, 0.2, 0.3, 0.5, 0.5, 0.3, 0.4]}  width = 40  tot = np.repeat(s['d'],width).reshape(len(s['d']), width) tot2 = np.repeat(s['t'],width).reshape(len(s['d']), width)  fig, (ax1, ax2, ax3, ax4) = plt.subplots(1,4)  fig.suptitle('title of figure', fontsize=20)  # line plots ax1.set_title('title of ax1') ax1.plot(s['x'], s['t']) ax1.set_ylim(0,1)  ax2.set_title('title of ax2') ax2.plot(s['x'], s['d']) # set locations of ticks on y-axis (at every multiple of 0.25) ax2.yaxis.set_major_locator(multiplelocator(0.25)) # set locations of ticks on x-axis (at every multiple of 2) ax2.xaxis.set_major_locator(multiplelocator(2)) ax2.set_ylim(0,1)  ax3.set_title('title of ax3') # display image, `aspect='auto'` makes fill whole `axes` (ax3) im3 = ax3.imshow(tot, norm=lognorm(vmin=0.001, vmax=1), aspect='auto') # create divider existing axes instance divider3 = make_axes_locatable(ax3) # append axes right of ax3, 20% width of ax3 cax3 = divider3.append_axes("right", size="20%", pad=0.05) # create colorbar in appended axes # tick locations can set kwarg `ticks` # , format of ticklabels kwarg `format` cbar3 = plt.colorbar(im3, cax=cax3, ticks=multiplelocator(0.2), format="%.2f") # remove xticks ax3 ax3.xaxis.set_visible(false) # manually set ticklocations ax3.set_yticks([0.0, 2.5, 3.14, 4.0, 5.2, 7.0])  ax4.set_title('title of ax4') im4 = ax4.imshow(tot2, norm=lognorm(vmin=0.001, vmax=1), aspect='auto') divider4 = make_axes_locatable(ax4) cax4 = divider4.append_axes("right", size="20%", pad=0.05) cbar4 = plt.colorbar(im4, cax=cax4) ax4.xaxis.set_visible(false) # manually set ticklabels (not ticklocations, remain unchanged) ax4.set_yticklabels([0, 50, 30, 'foo', 'bar', 'baz'])  plt.tight_layout() # make space title plt.subplots_adjust(top=0.85) plt.show() 

enter image description here


you can change locations , labels of ticks on either axis set_ticks , set_ticklabels methods in example above.


as make_axes_locatable function does, matplotlib site axesgrid toolkit:

the axes_divider module provides helper function make_axes_locatable, can useful. takes existing axes instance , create divider it.

ax = subplot(1,1,1) divider = make_axes_locatable(ax) 

make_axes_locatable returns instance of axeslocator class, derived locator. provides append_axes method creates new axes on given side of (“top”, “right”, “bottom” , “left”) of original axes.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -