Python matplotlib 子图:并排放置水平条形图,并在轴上放置不需要的数字标签

Python matplotlib subplots: putting horizontal bar charts side by side and unwanted numerical labels on axes

考虑以下虚假数据:

fake_data = pd.DataFrame(
    {"index": ["A", "B", "C"], 0: [0.1, 0.2, 0.3], 1: [2, 4, 6], 2: [1, 3, 5]}
)
fake_data.set_index("index")

我想绘制一系列水平条形图,一行三列。每个条形图的 y 轴包含分类变量 ABC。每个条形图的 x 轴应该是数字。

我试过以下方法:

fig, ax = plt.subplots(1, 3, sharex = True, sharey = True, figsize = (6, 8), frameon = False)
ax1 = fig.add_subplot(1,3,1)
fake_data[0].plot.barh()
ax2 = fig.add_subplot(1,3,2)
fake_data[1].plot.barh()
ax3 = fig.add_subplot(1,3,3)
fake_data[2].plot.barh()

输出为:

我尝试通过修改上面的代码来调整坐标轴标签和刻度:

fig, ax = plt.subplots(1, 3, sharex = True, sharey = True, figsize = (6, 8), frameon = False)
ax1 = fig.add_subplot(1,3,1)
fake_data[0].plot.barh()
ax1.set_yticks([])
ax2 = fig.add_subplot(1,3,2)
fake_data[1].plot.barh()
ax2.set_xticks([])
ax3 = fig.add_subplot(1,3,3)
fake_data[2].plot.barh()

输出为:

如您所见,分类标签现已消失。

我的问题是:

  1. 假设 y 轴应该有一个分类变量,我怎样才能去掉最左边水平条形图上 y 轴上的数字刻度/标签?

  2. 如何去除第二个和第三个条形图上的标签和刻度以及 y 轴名称“index”?

  3. 与 (1) 类似,如何去除 x 轴上与正确刻度/标签重叠的数字刻度/标签?

  4. 如何使水平条形图的所有x轴都处于同一比例,使得第一个(最左边)水平条形图的数量级为第二个和第三个的十分之一条形图?

您已经使用 plt.subplots 创建了 axes。您可以将它们传递给每个 pd.Series.plot 方法,它将解决您的问题:

fig, (ax1, ax2, ax3) = plt.subplots(
    1, 3, sharex=True, sharey=True, figsize=(6, 8), frameon=False
)
fake_data[0].plot.barh(ax=ax1)
fake_data[1].plot.barh(ax=ax2)
fake_data[2].plot.barh(ax=ax3)