python - Matplotlib colorbar moves second x axis -


i'm trying add second x axis top of plot using twiny.

if make simple scatter plot no colorbar, top x axis correctly aligned bottom x axis (mwe below):

enter image description here

if add colorbar though, top x axis displaced:

enter image description here

how can fix this?


mwe

import numpy np import matplotlib.pyplot plt mpl_toolkits.axes_grid1 import make_axes_locatable import matplotlib.gridspec gridspec   x = np.array([0., 0.5, 1., 1.5, 2., 2.5, 3., 3.5, 4.]) x2 = np.array([122, 85, 63, 50, 23, 12, 7, 5, 2]) y = np.cos(x*20) z = np.sin(x*20)  fig = plt.figure() gs = gridspec.gridspec(1, 2) ax1 = plt.subplot(gs[1]) ax2 = ax1.twiny()  ax1.set_xlim(-0.2, max(x)+0.2) plt.tick_params(axis='both', which='major', labelsize=10) ax1.minorticks_on() ax1.grid(b=true, which='major', color='gray', linestyle='--', lw=0.3)  sc = ax1.scatter(x, y, c=z) ax1.set_xlabel("original x-axis")  ax2.set_xlim(ax1.get_xlim()) ax2.set_xticks(x) ax2.set_xticklabels(x2) ax2.set_xlabel("second x-axis")  # colorbar. the_divider = make_axes_locatable(ax1) color_axis = the_divider.append_axes("right", size="2%", pad=0.1) cbar = plt.colorbar(sc, cax=color_axis) cbar.set_label('b', fontsize=10, labelpad=4, y=0.5) cbar.ax.tick_params(labelsize=10)  plt.show() 

you can have colorbar 'steal' space more 1 ax

import numpy np import matplotlib.pyplot plt mpl_toolkits.axes_grid1 import make_axes_locatable import matplotlib.gridspec gridspec  x = np.array([0., 0.5, 1., 1.5, 2., 2.5, 3., 3.5, 4.]) x2 = np.array([122, 85, 63, 50, 23, 12, 7, 5, 2]) y = np.cos(x*20) z = np.sin(x*20)  fig = plt.figure() gs = gridspec.gridspec(1, 2) ax1 = plt.subplot(gs[1]) ax2 = ax1.twiny()  ax1.set_xlim(-0.2, max(x)+0.2) plt.tick_params(axis='both', which='major', labelsize=10) ax1.minorticks_on() ax1.grid(b=true, which='major', color='gray', linestyle='--', lw=0.3)  sc = ax1.scatter(x, y, c=z, cmap='viridis') ax1.set_xlabel("original x-axis")  ax2.set_xlim(ax1.get_xlim()) ax2.set_xticks(x) ax2.set_xticklabels(x2) ax2.set_xlabel("second x-axis")  # colorbar. cbar = plt.colorbar(sc, ax=[ax1, ax2]) cbar.set_label('b', fontsize=10, labelpad=4, y=0.5) cbar.ax.tick_params(labelsize=10)  plt.show() 

which think un-block use case.

limits bit different because sitting on current master branch.

enter image description here

if need use tight_layout (which requires tuning on padding etc):

import numpy np import matplotlib.pyplot plt import matplotlib.gridspec gridspec   x = np.array([0., 0.5, 1., 1.5, 2., 2.5, 3., 3.5, 4.]) x2 = np.array([122, 85, 63, 50, 23, 12, 7, 5, 2]) y = np.cos(x*20) z = np.sin(x*20)  fig = plt.figure() gs = gridspec.gridspec(1, 2) right_gs = gridspec.gridspecfromsubplotspec(1, 2, width_ratios=[30, 1], subplot_spec=gs[1], wspace=0.05)  ax1 = fig.add_subplot(right_gs[0]) color_axis = fig.add_subplot(right_gs[1])    ax2 = ax1.twiny()  ax1.set_xlim(-0.2, max(x)+0.2) plt.tick_params(axis='both', which='major', labelsize=10) ax1.minorticks_on() ax1.grid(b=true, which='major', color='gray', linestyle='--', lw=0.3)  sc = ax1.scatter(x, y, c=z, cmap='viridis') ax1.set_xlabel("original x-axis")  ax2.set_xlim(ax1.get_xlim()) ax2.set_xticks(x) ax2.set_xticklabels(x2) ax2.set_xlabel("second x-axis")  cbar = fig.colorbar(sc, cax=color_axis) cbar.set_label('b', fontsize=10, labelpad=4, y=0.5) cbar.ax.tick_params(labelsize=10)  fig.tight_layout() plt.show() 

enter image description here


Comments