【问题标题】:Pandas: using a pivot table to make bar graphs in subplotsPandas:使用数据透视表在子图中制作条形图
【发布时间】:2020-04-06 03:53:19
【问题描述】:

我正在研究公司的年龄和时间如何影响因不满而辞职。

我有一个包含这些列的数据框(“combined_updated”):

"age_updated", “service_cat”,以及 “不满意。”

“age_updated”和“service_cat”是字符串。 “age_updated”包括年龄范围,“service_cat”是描述员工职业阶段的字符串(即“New”、“Experienced”等)。

“不满意”是布尔值,在数据透视表中 True 为 1,False 为 0。因此,数据透视表显示了某些组的不满意百分比。

我想在一个子图中制作四个条形图,其中一个图表查看每个职业阶段,y 轴表示不满意百分比,x 轴表示年龄。

到目前为止,我已经编写了将所有内容放在一个图表上的代码:

dis_pt = combined_updated.pivot_table(index=“service_cat”,columns=“age_cleaned”,values=“dissatisfied”)
dis_pt.plot(kind=“bar”)

Pivot Table

Graph so far

有谁知道如何将其分解为带有适当标签的子图?谢谢!

【问题讨论】:

标签: python matplotlib pivot-table data-science subplot


【解决方案1】:

您可以单独为每个绘图设置轴。


ax = plt.subplot("410")
dis_plt.iloc[0,:].plot(kind="bar", ax= ax)

ax = plt.subplot("411")
dis_plt.iloc[1,:].plot(kind="bar", ax= ax)

ax = plt.subplot("412")
dis_plt.iloc[2,:].plot(kind="bar", ax= ax)

ax = plt.subplot("413")
dis_plt.iloc[3,:].plot(kind="bar", ax= ax)

plt.show()

【讨论】:

    猜你喜欢
    • 2019-09-30
    • 2016-09-07
    • 1970-01-01
    • 2021-12-27
    • 2021-01-25
    • 1970-01-01
    • 2016-07-08
    • 1970-01-01
    • 2014-12-28
    相关资源
    最近更新 更多