当 Pandas 在多个带有颜色标签的子图中创建散点图时无法设置 xlabel

Unable to set xlabel when when Pandas creates scatter plot in multiple subplots with color label

时,我无法设置绘图的xlabel

例如,这很好用:我可以看到 xlabel:

import numpy as np
import pandas as pd
import pylab as plt
plt.ion()

foo = pd.DataFrame(np.random.randn(5, 5), columns='a b c d e'.split())

fig, (ax1, ax2) = plt.subplots(nrows=2, sharex=True)
foo.plot.scatter(x='a', y='b', ax=ax1)
foo.plot.scatter(x='a', y='c', ax=ax2)
ax2.set_xlabel('xxx')  # works

然而,下面的轻微扭曲,我设置颜色 c 字段,没有设置 xlabel:

fig, (ax1, ax2) = plt.subplots(nrows=2, sharex=True)
foo.plot.scatter(x='a', y='b', ax=ax1, c='c')
foo.plot.scatter(x='a', y='c', ax=ax2, c='c')
ax2.set_xlabel('xx')  # NO x label

plt.xlabel 也不行。 ax2.get_xlabel() returns 我期望的 "xx",但它不可见:

在这种情况下,如何获得 xlabel? Pandas Github repo 有 3000 多个未解决的问题,与其将其作为错误提交,我宁愿找到一个面向 Matplotlib 的解决方法来呈现 xlabel。 (Python 3.8.1,Pandas 1.0.3,Matplotlib 3.2.0。)

编辑:从数字列名称移至文本列名称,因为它会引起混淆。

出于某种原因,xlabel 的可见性被设置为 False,要绕过它,您只需要做

fig, (ax1, ax2) = plt.subplots(nrows=2, sharex=True)
foo.plot.scatter(x='a', y='b', ax=ax1, c='c')
foo.plot.scatter(x='a', y='c', ax=ax2, c='c')
ax2.set_xlabel('xxx')
ax2.xaxis.get_label().set_visible(True)

这会给你