首页 > 解决方案 > Matplotlib 图 '.supxlabel' 不起作用

问题描述

我正在尝试为我的条件六边形分箱图设置图形标签,但是当我运行此代码时,我得到 Attribute Error: 'Figure'object has no attribute 'supxlabel'。任何有关此问题的帮助将不胜感激。

zip_codes = [98188, 98105, 98108, 98126]

def hexbin_zips(ax, zipcode):
    kc_tax_zip = kc_tax_stripped.loc[kc_tax_stripped['ZipCode'] == zipcode]
    out = ax.hexbin(kc_tax_zip['SqFtTotLiving'], kc_tax_zip['TaxAssessedValue'], gridsize=30)
    return out

fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, figsize=(12, 10), sharex='col', sharey='row')
axes = [ax1, ax2, ax3, ax4]

for n in range(4):
    hexbin_zips(axes[n], zip_codes[n])
    axes[n].set_title(str(zip_codes[n]))

fig.supxlabel('Total Living Space in Square Feet', fontsize=16.)
fig.supylabel('Tax-Assessed Value', fontsize=16.)

标签: pythonmatplotlibplotattributeerror

解决方案


使用此处给出的答案时,我遇到了同样的问题。事实证明,您需要两件事:

  • Python 至少 3.7 版;
  • Matplotlib 至少版本 3.4 ( pip install --upgrade matplotlib)。

如果您的 Python 版本低于 3.7,则安装 3.4 版matplotlib将无法正常工作(至少在我的情况下没有)。


推荐阅读