如何在使用 Pandas' plot 创建的绘图中添加共享 x 标签和 y 标签?(Matplotlib)


要添加共享 x 标签和共享 y 标签,我们可以使用 plot() 方法,并使用 kind="bar", sharex=True and sharey=True.

步骤

  • 设置图表大小,调整子图之间及周围的间距。
  • 创建一个二维、尺寸可变的潜在异构表格数据。
  • 使用 kind="bar", sharex=True and sharey=True. 绘制数据框。
  • 要显示图表,请使用 show() 方法。

示例

import pandas as pd
import matplotlib.pyplot as plt

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

df = pd.DataFrame(
   {'First': [0.3, 0.2, 0.5, 0.2], 'Second': [0.1, 0.0, 0.3, 0.1],
   'Third': [0.2, 0.5, 0.0, 0.7], 'Fourth': [0.6, 0.3, 0.4, 0.6]},
   index=list('1234'))

axes = df.plot(kind="bar", subplots=True, layout=(2, 2),
               sharey=True, sharex=True)

plt.show()

输出

更新日期:16-Jun-2021

706 次浏览

开启你的 职业生涯

通过完成课程获取认证

开始
广告
© . All rights reserved.