これは、カラーバーを画像と同じ高さにするため、make_axes_locatable
からの関数を使用したかなり単純な解決策です。mpl_toolkits.axes_grid1
さらに、カラーバーの配置、幅、およびパディングを に対して設定するのは非常に簡単Axes
です。
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.axes_grid1 import make_axes_locatable
from matplotlib import cm
from numpy.random import randn
# Make plot with vertical (default) colorbar
fig = plt.figure()
ax = fig.add_subplot(121, aspect='equal')
ax2 = fig.add_subplot(122, aspect='equal')
ax2.axis('off')
divider = make_axes_locatable(ax)
# Specify placement, width and padding of colorbar
cax = divider.append_axes("right", size="10%", pad=0.1)
data = np.clip(randn(250, 250), -1, 1)
im = ax.imshow(data, interpolation='nearest', cmap=cm.coolwarm)
ax.set_title('Title')
# Add colorbar, make sure to specify tick locations to match desired ticklabels
cbar = fig.colorbar(im, cax=cax, ticks=[-1, 0, 1])
cbar.ax.set_yticklabels(['< -1', '0', '> 1'])# vertically oriented colorbar
# Add text
boxtext = \
"""Text box
Second line
Third line"""
props = dict(boxstyle='round, pad=1', facecolor='white', edgecolor='black')
ax2.text(0.15, 0.85, boxtext, ha='left', va='top', transform=ax2.transAxes, bbox=props)
#plt.tight_layout()
plt.savefig(r'D:\image.png', bbox_inches='tight', dpi=150)
