python - Remove boxes around imshow when sharing X axis -
when try stack several imshow
elements, white space appears around them in vertical axis , titles appear close other figures.
i think both issues caused sharex=true
not know how solve them.
fig.tight_layout()
solves problem, incompatible colour bar on side , makes squares smaller others.
the code generates image is
# values [(ndarray, string)] fig, axes = plt.subplots(len(values), sharex=true) ax, (value, plot_name) in zip(axes, values): im = ax.imshow(value, vmax=1.0, vmin=0.0) ax.set_title(plot_name) # (hack) apply on last 1 plt.xticks(range(values.shape[1]), ticks, rotation=90) plt.colorbar(im, ax=axes.ravel().tolist()) fig.savefig(output_name, bbox_inches="tight")
unfortunately aspect of plots cannot set "equal"
when sharex=true
used. there might 2 solutions:
not sharing axes
sharing axes not necessary, since subplots anyway have same dimension. idea not share axes, remove ticklabels of upper plots.
import matplotlib.pyplot plt import numpy np values = [np.random.rand(3,10) in range(3)] fig, axes = plt.subplots(len(values)) i, (ax, value) in enumerate(zip(axes, values)): im = ax.imshow(value, vmax=1.0, vmin=0.0) ax.set_title("title") ax.set_xticks(range(value.shape[1])) if != len(axes)-1: ax.set_xticklabels([]) else: plt.setp(ax.get_xticklabels(), rotation=90) plt.colorbar(im, ax=axes.ravel().tolist()) plt.show()
using imagegrid
using imagegrid
mpl_toolkits.axes_grid1
module provides grid plots of equal aspects. can used follows. 1 main advantage here colorbar automatically same size subplots.
import numpy np import matplotlib.pyplot plt mpl_toolkits.axes_grid1 import imagegrid values = [np.random.rand(3,10) in range(3)] axes = imagegrid(plt.figure(), 111, nrows_ncols=(3,1), axes_pad=0.3, share_all=true, cbar_location="right", cbar_mode="single", cbar_size="2%", cbar_pad=0.15, label_mode = "l" ) i, (ax, value) in enumerate(zip(axes, values)): im = ax.imshow(value, vmax=1.0, vmin=0.0) ax.set_title("title") ax.set_xticks(range(value.shape[1])) plt.setp(ax.get_xticklabels(), rotation=90) ax.cax.colorbar(im) plt.show()
Comments
Post a Comment