【问题标题】:Remove Horizontal Whitespace Between Matplotlib Plots删除 Matplotlib 绘图之间的水平空白
【发布时间】: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


【解决方案1】:

我无法运行您提供的代码,因为有太多未定义的变量。

但是,当我运行一个最小版本时,我得到了你想要的:

from matplotlib import pyplot, gridspec 
fig, ax = pyplot.subplots(figsize=(14,8), sharex=True, sharey=True)
gs = gridspec.GridSpec(4, 1, height_ratios=[1, 1, 1, 11.5])

botax = fig.add_subplot(gs[0])
midax = fig.add_subplot(gs[1], sharex=botax)
upperax = fig.add_subplot(gs[2], sharex=botax)
topax = fig.add_subplot(gs[3], sharex=botax)

gs.update(wspace=0.00, hspace=0.00)

【讨论】:

  • 在将 gs.update 与 Jody 的 plt.imshow(aspect='auto') 调用配对时,我确实得到了类似的结果。运行此程序时,我在图的左侧和右侧得到了很多额外的空白 [figsize=(14,8)]。当我用 gs.tight_layout(fig) 纠正这个问题时,我失去了 gs.update 的紧密间距。任何想法为什么?
猜你喜欢
  • 2022-06-10
  • 2012-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-22
相关资源
最近更新 更多