我正在尝试生成具有多个轴的图,每个轴都有自己的颜色条(下面的代码)。如果我使用默认的颜色条绘图,则在颜色条和颜色条之间会获得过多的水平间距:
如果我尝试使用make_axes_locatable()
方法,我将得到可怕的结果:
这是怎么回事,我该如何解决?
import numpy as np
from matplotlib import pyplot as plt
import matplotlib.gridspec as gridspec
from mpl_toolkits.axes_grid1 import make_axes_locatable
# Random data to plot
data = np.random.uniform(0., 1., (2, 100))
z = np.random.uniform(0., 10., 100)
# Define figure
fig = plt.figure(figsize=(30, 30))
gs = gridspec.GridSpec(12, 12)
for i in range(2):
ax = plt.subplot(gs[2 * i:2 + (2 * i), 0:8])
SC = plt.scatter(*data, c=z)
# Colorbar 1
cbar = plt.colorbar()
# Colorbar 2
# the_divider = make_axes_locatable(ax)
# color_axis = the_divider.append_axes("right", size="1%", pad=0.)
# cbar = plt.colorbar(SC, cax=color_axis)
cbar.set_label("test", fontsize=15, labelpad=10)
fig.tight_layout()
plt.savefig('test.png', dpi=300, bbox_inches='tight')