Python Matplotlib多个子地块图形间距符合要求

Python Matplotlib多个子地块图形间距符合要求,python,pandas,matplotlib,Python,Pandas,Matplotlib,一个关于matplotlib子地块间距的非常简单的问题,尽管在plt.tight_布局或plt.subplot_调整上进行了多次迭代。我仍然无法在子地块之间留出适当的空间 下面是一个示例python代码 import pandas as pd import matplotlib.pyplot as plt %matplotlib inline dict1 = {'importance': {'var1': 67.18,'var2': 50.33,'var3': 29.33, 'var4

一个关于matplotlib子地块间距的非常简单的问题,尽管在plt.tight_布局或plt.subplot_调整上进行了多次迭代。我仍然无法在子地块之间留出适当的空间

下面是一个示例python代码

import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline


dict1 = {'importance': {'var1': 67.18,'var2': 50.33,'var3': 29.33,
   'var4': 27.17, 'var5': 24.880}}

dict2 = {'importance': {'var3': 50.18,'var1': 30.33,'var2': 29.33,
  'var4': 24.17, 'var5': 7.880}}


df1 = pd.DataFrame.from_dict(dict1)
df2 = pd.DataFrame.from_dict(dict2)


fig, (ax1, ax2) = plt.subplots(1,2)
fig = plt.figure(figsize=(60,60))
fig.subplots_adjust(wspace=10)
df1.plot(kind = 'barh', ax = ax1)
df2.plot(kind = 'barh', ax = ax2)
所以这个图表是


这当然不是我要找的,我要找的是一个广泛分布的子地块。请帮助您的问题是
fig=plt.figure(figsize=(60,60))
创建了第二个图形,因此当您调用
fig.subplots\u adjust(wspace=10)
时,它会修改第二个图形,而不是有打印轴的图形。纠正这种情况的一种方法是,首先在对子批次
fig(ax1,ax2)=plt.子批次(1,2,figsize=(60,60)
)的调用中包含关键字figsize,然后删除对下方
图的调用,最后调整
wspace
值。

您可以做的是:

fig, (ax1, ax2) = plt.subplots(1,2,figsize=(10,10))
fig.subplots_adjust(wspace=0.5)
df1.plot(kind = 'barh', ax = ax1)
df2.plot(kind = 'barh', ax = ax2)
plt.show()
输出:

解释得很好,非常感谢!