【发布时间】:2019-06-24 08:02:25
【问题描述】:
我想使用 matplotlib 库(或其他库,如果可能的话)绘制几个条形图,并使用 subplot 将每个图形放在它的位置。
我还使用 groupby 对每个类别进行分组并对值求和。然后我只想显示三列(Num1,Num2,Num3):
#Build subplot with three rows and two columns
fig, axes = plt.subplots(figsize=(12, 8) , nrows = 3, ncols = 2)
fig.tight_layout()
#five categorical columns and three numerical columns of interest
for i, category in enumerate(['Cat1', 'Cat2', 'Cat3', 'Cat4', 'Cat5']):
ax = fig.add_subplot(3,2,i+1)
data.groupby(category).sum()[['Num1','Num2','Num3']].plot.bar(rot=0)
plt.xticks(rotation = 90)
我得到的是 6 个空地块,排列成 3 行和 2 列,然后是 5 个正确的地块依次排列在一列中。 照片中显示了一个地块的示例。
感谢您的帮助和建议。
【问题讨论】:
-
您的代码是多余的。您可以通过
plt.subplots或 创建子图,您可以通过fig.add_subplot添加子图。两者都不是。
标签: python matplotlib bar-chart subplot