如何向使用 Pandas 绘图创建的绘图添加共享 x 标签和 y 标签?(Matplotlib)
matplotlibpythondata visualization更新于 2025/7/10 17:16:17
要添加共享 x 标签和共享 y 标签,我们可以使用 plot() 方法,其中 kind="bar"、 sharex=True 和 sharey=True。
步骤
- 设置图形大小并调整子图之间和周围的填充。
- 创建二维、大小可变、可能异构的表格数据。
- 使用 kind="bar"、sharex=True 和 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()
输出

相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

