python - imshow and plot side by side -


i'm trying put side-by-side numpy array displayed image , seaborn distplot of same array. i've came following function:

def visualize(arr):     f, (ax1, ax2) = plt.subplots(1, 2, gridspec_kw = {'width_ratios': [1, 3]})      ax1.imshow(arr)      flat = arr.flatten()     x = flat[~np.isnan(flat)]     sns.distplot(x, ax=ax2)     plt.show() 

which produces: enter image description here

as can see, image has smaller height plot. how can modify function in order have same height plot , imshow?

i want following placement of image , plot: enter image description here

there many ways tackle this. of following give more or less same image

enter image description here

a. reduce available space

you may reduce available space such both plots constrained same vertical margins. can done by

  1. reducing figure height

    fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(6,2.3), ...) 

  2. using subplots_adjust limit margins

    fig.subplots_adjust(top=0.7, bottom=0.3) 

b. use insetposition

you may use mpl_toolkits.axes_grid1.inset_locator.insetposition adjust coordinates of second axes match of first one.

import seaborn sns import numpy np import matplotlib.pyplot plt mpl_toolkits.axes_grid1.inset_locator import insetposition  def visualize(arr):     fig, (ax1, ax2) = plt.subplots(1, 2,                                gridspec_kw = {'width_ratios': [1, 3]})      ax1.imshow(arr)      flat = arr.flatten()     x = flat[~np.isnan(flat)]     sns.distplot(x, ax=ax2)      ip = insetposition(ax1, [1.5,0,3,1])      ax2.set_axes_locator(ip)      plt.show()  arr = np.random.randn(200,120) visualize(arr) 

c. use axes divider

you may create axes image , use mpl_toolkits.axes_grid1.make_axes_locatable create new axes next it.

import seaborn sns import numpy np import matplotlib.pyplot plt mpl_toolkits.axes_grid1 import make_axes_locatable  def visualize(arr):     fig, ax = plt.subplots()     divider = make_axes_locatable(ax)     ax2 = divider.new_horizontal(size="300%", pad=0.5)     fig.add_axes(ax2)      ax.imshow(arr)      flat = arr.flatten()     x = flat[~np.isnan(flat)]     sns.distplot(x, ax=ax2)      plt.show()  arr = np.random.randn(200,120) visualize(arr) 

d. calculate desired aspect ratio

import seaborn sns import numpy np import matplotlib.pyplot plt  def visualize(arr):     gkw = {'width_ratios':[1, 3] }     fig, (ax1, ax2) = plt.subplots(1, 2,  gridspec_kw = gkw )      ax1.imshow(arr)      flat = arr.flatten()     x = flat[~np.isnan(flat)]     sns.distplot(x, ax=ax2)      ya = np.diff(np.array(ax2.get_ylim()))[0]     xa = np.diff(np.array(ax2.get_xlim()))[0]     wa = gkw['width_ratios'][0]/float(gkw['width_ratios'][1])     ia = arr.shape[0]/float(arr.shape[1])     ax2.set_aspect(float(wa*ia/(ya/xa)))      plt.show()  arr = np.random.randn(200,120) visualize(arr) 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -