【发布时间】:2021-11-19 23:51:57
【问题描述】:
我正在 Matplotlib 中生成一个子图,其中包含四个垂直堆叠的子图。情节如下:
为了生成子图,我目前正在使用 GridSpec 来管理图之间的空白和每个图的整体布局。我已经包含了gs.update(wspace=0.00, hspace=0.00),但是这并没有像我预期的那样删除前三个子图之间的空格。扩展代码如下,请注意,为了简单起见,我只包含了关键部分:
fig, ax = plt.subplots(figsize=(14,8), sharex=True, sharey=True)
gs = gridspec.GridSpec(4, 1, height_ratios=[1, 1, 1, 11.5])
gs.update(wspace=0.00, hspace=0.00)
bar_width = 0.40
botax = plt.subplot(gs[0])
im = plt.imshow(clds_arr, cmap='Blues', vmin=0, vmax=100, interpolation='nearest')
for i in range(len(clds_arr)):
for k,j in zip(clds,range(len(objects_temps))):
if k in list(range(0,51)):
text = plt.text(j, i, clds_arr[i, j], ha="center", va="center", color="k", fontsize=11)
else:
text = plt.text(j, i, clds_arr[i, j], ha="center", va="center", color="w", fontsize=11)
midax = plt.subplot(gs[1], sharex=botax)
im = plt.imshow(temps_arr, cmap=cmap, norm=norm, interpolation='nearest')
for i in range(len(temps_arr)):
for k,j in zip(temps,range(len(objects_temps))):
if k in list(range(-10,5)) + list(range(15,25)) + list(range(88,99)):
text = plt.text(j, i, temps_arr[i, j], ha="center", va="center", color="w", fontsize=11)
else:
text = plt.text(j, i, temps_arr[i, j], ha="center", va="center", color="k", fontsize=11)
upperax = plt.subplot(gs[2], sharex=botax)
im = plt.imshow(dpts_arr, cmap=cmap, norm=norm, interpolation='nearest')
for i in range(len(dpts_arr)):
for k,j in zip(dpts,range(len(objects_temps))):
if k in list(range(-10,5)) + list(range(15,25)) + list(range(88,99)):
text = plt.text(j, i, dpts_arr[i, j], ha="center", va="center", color="w", fontsize=11)
else:
text = plt.text(j, i, dpts_arr[i, j], ha="center", va="center", color="k", fontsize=11)
topax = plt.subplot(gs[3], sharex=botax)
rectstop = plt.bar(ymax_pos, height=highheight, width=0.65, bottom=min_highhght, color='#1e90ff', edgecolor='black', linewidth=2, zorder=3)
for rect in rectstop:
y_value = rect.get_height()+min_highhght
x_value = rect.get_x() + rect.get_width() / 2
space = 2
va = 'bottom'
label = y_value
plttxt = plt.annotate(label, (x_value, y_value), xytext=(0, space), textcoords="offset points", ha='center', va=va)
plttxt.set_fontsize(13)
plttxt.set_weight('semibold')
需要哪些额外的代码来删除前三个子图之间的额外空白?谢谢!
【问题讨论】:
-
尝试将
gs.update放在脚本的末尾而不是开头 -
感谢@PaulH 的建议!我将它包含在子图块的末尾,并且它在上面的显示方式没有改变。我还注释掉了 fig.tight_layout() 看看这是否是罪魁祸首,但都没有产生预期的结果。
-
在您的 imshow 通话中尝试 aspect=“auto”。
标签: python python-3.x matplotlib subplot