ImportanceOfBeingErnest代码的更通用版本:def create_pareto_chart(df, by_variable, quant_variable):
df.index = by_variable
df["cumpercentage"] = quant_variable.cumsum()/quant_variable.sum()*100
fig, ax = plt.subplots()
ax.bar(df.index, quant_variable, color="C0")
ax2 = ax.twinx()
ax2.plot(df.index, df["cumpercentage"], color="C1", marker="D", ms=7)
ax2.yaxis.set_major_formatter(PercentFormatter())
ax.tick_params(axis="y", colors="C0")
ax2.tick_params(axis="y", colors="C1")
plt.show()
这一个也包括按阈值分组的Pareto。
例如:如果将其设置为70,则它会将70岁以上的少数民族分组为一个称为“其他”的组。def create_pareto_chart(by_variable, quant_variable, threshold):
total=quant_variable.sum()
df = pd.DataFrame({'by_var':by_variable, 'quan