作者|黄伟呢
来源|数据分析与统计学之美
简介
瀑布图,由麦肯锡顾问公司所独创的图表类型,因为形似瀑布流水,所以被大家称之为瀑布图(Waterfall Plot),在企业经营分析、财务分析中使用较多,用以表示企业成本的构成、变化等情况。
瀑布图本质:堆积柱形图的绘制。
瀑布图的绘图原理
那么,如何用Python绘制这样一个瀑布图呢?
很多人不知道如何绘制瀑布图,是由于对瀑布图的绘图原理,不够清楚。
因此,这里先来讲述一下瀑布图的绘图原理。
有这样一份原始数据:
从最开始的销售,经过退货、信用费、回扣、滞纳金、运费,这一系列的操作后,最后肯定还有一个净总量。因此,我们需要添加这样一行。
注意:在瀑布图中,最初的“销售”与最后的“净总量”,都是以坐标轴原点为起点。
瀑布图本身就是要体现这个增减变动过程。因此,这里涉及到一个占位,如图所示:
除了首尾两个字段,都是以原点起步的,中间过程要体现这个波动过程。我特意用不同颜色,为大家圈出了这个占位的高度,但是在瀑布图中,我们并不显示出来。
文章最开始就说了,瀑布图的本质其实就是堆积的柱形图。
为了构造这个占位数据,我们需要对数据进行一定的处理。让首尾字段的数据都为0,中间部分,表示占位高度。
最后,我们利用原始数据与占位高度数据,做一个堆叠的柱形图,就可以展示出这个瀑布效果了。
瀑布图的绘图步骤
现在我就为大家讲解详细的步骤。
1. 导入相关库
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
2. 创建数据
index = ['销售','退货','信用费','回扣','滞纳金','运费']
data = {'金额': [350000,-30000,-7500,-25000,95000,-7000]}
df = pd.DataFrame(data=data,index=index)
df
结果如下:
瀑布图的最大问题就是弄清楚,底部堆积的条形图应该是什么。
首先,先求累积和。
df["金额"].cumsum()
结果如下:
原理部分,已经为大家讲述过,瀑布图 = 原始数据 + 占位数据,堆积而成。
因此,对于占位数据,它的首位数据,我们必须将其变为0.
total = df.sum()["金额"]
df.loc["净总量"] = total
blank = df["金额"].cumsum().shift(1).fillna(0)
blank.loc["净总量"] = total
blank.loc["净总量"] = 0
结果如下
其实,一行Python代码,就可以完成瀑布图的绘制。
df.plot(kind='bar', stacked=True, bottom=blank,legend=None, title="瀑布")
plt.xticks(rotation=60)
plt.ylim(-100000,400000)
plt.grid()
结果如下:
其实,到这里瀑布图的绘制已经结束了。但是,为了是这个增减变动更加清楚,我们可以添加一个阶梯。
# 多添加一个绘图代码
step = blank.reset_index(drop=True).repeat(3).shift(-1)
step[1::3] = np.nan
my_plot = df.plot(kind='bar', stacked=True, bottom=blank,legend=None, title="瀑布图")
my_plot.plot(step.index, step.values,'k')
plt.xticks(rotation=60)
plt.ylim(-100000,400000)
plt.grid()
结果如下:
值得注意的问题
在下面这行代码中,有一个bottom参数。
df.plot(kind='bar', stacked=True, bottom=blank,legend=None, title="瀑布图")
其实该参数,表示的是堆积柱形图的起始点,如果数据为正,则柱子在该起始点之上,如果数据为负,则柱子在该起始点之下。
我们为大家举例说明:
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(5)
a = np.array([10, 15, 20, 15, 5])
b = np.array([5, -20, 7, -25, 9])
plt.bar(x, b, bottom=a, label='b')
plt.grid()
plt.show()
结果如下:
观察上图,我为大家标注好了起始点,如果数据为正,柱子都是朝上,否则就朝下。
往
期
回
顾
资讯
AI 将有自我视觉?Facebook正在研究新系统
资讯
微软、英伟达联手推出最大语言模型
大赛
API 大赛决赛名单出炉!
技术
ST-GCN 实现人体姿态行为分类
分享
点收藏
点点赞
点在看
今天的文章瀑布图的制作步骤_Python 绘图[通俗易懂]分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/86433.html
![瀑布图的制作步骤_Python 绘图[通俗易懂]插图1 cb338e3800a678b55d1fb6c9f1dea9e0.gif](https://img.bianchenghao.cn/app/bianchenghao_cn/43d1eeb6634a44b29968242c8b2b1565.gif)
![瀑布图的制作步骤_Python 绘图[通俗易懂]插图3 66de08ed5823a2131f00a98a894a331e.png](https://img.bianchenghao.cn/app/bianchenghao_cn/1559842647ae4c9596efac51d0184e1a.png)
![瀑布图的制作步骤_Python 绘图[通俗易懂]插图5 69c62f4110b1b38897df79c704a2ca6f.png](https://img.bianchenghao.cn/app/bianchenghao_cn/cf3cdcaf1807456ab39812dffd45f2e7.png)
![瀑布图的制作步骤_Python 绘图[通俗易懂]插图7 79c6fdfc96629a3993fe19857397066c.png](https://img.bianchenghao.cn/app/bianchenghao_cn/0c0e8443f26048d698644dd5a3c27b61.png)
![瀑布图的制作步骤_Python 绘图[通俗易懂]插图9 97614006649609a989042369626bd9d7.png](https://img.bianchenghao.cn/app/bianchenghao_cn/307ba8ae3a794262b13693e969657ca9.png)
![瀑布图的制作步骤_Python 绘图[通俗易懂]插图11 15e119d87ff770751531e0fa7fea24f6.png](https://img.bianchenghao.cn/app/bianchenghao_cn/749b36374e314f03a36a98a8867f0196.png)
![瀑布图的制作步骤_Python 绘图[通俗易懂]插图13 370849f187f08cc40c1f116bf1faa036.png](https://img.bianchenghao.cn/app/bianchenghao_cn/81bd8bb99310475d901a11ca3ef31527.png)
![瀑布图的制作步骤_Python 绘图[通俗易懂]插图15 aa96fade766cadc8664ace1dcfdb4e5d.png](https://img.bianchenghao.cn/app/bianchenghao_cn/b55dc626280640e389efdb9d696adc23.png)
![瀑布图的制作步骤_Python 绘图[通俗易懂]插图17 4fd44afa24e0928ccd29f4d0a85a1521.png](https://img.bianchenghao.cn/app/bianchenghao_cn/8ac932207f294477959649bc34d2c8cb.png)
![瀑布图的制作步骤_Python 绘图[通俗易懂]插图19 7fcd80784b283bcc6170e493c92bf43b.png](https://img.bianchenghao.cn/app/bianchenghao_cn/e78d66219cc1401585838354ed7e136d.png)
![瀑布图的制作步骤_Python 绘图[通俗易懂]插图21 317f8ccc469fec786c919bf41894299e.png](https://img.bianchenghao.cn/app/bianchenghao_cn/ccbe58f124e5411daaf7f68ffcd17a7e.png)
![瀑布图的制作步骤_Python 绘图[通俗易懂]插图23 7b0c76e6203ecd60a6f24d77ff935675.png](https://img.bianchenghao.cn/app/bianchenghao_cn/a3a7de58a6ed4dbfaf6d8baee5b7730f.png)
![瀑布图的制作步骤_Python 绘图[通俗易懂]插图25 da5e107b71f462d302e79f8f63c7d24c.png](https://img.bianchenghao.cn/app/bianchenghao_cn/a4e1c12e9807473690cb7db289d97ac9.png)
![瀑布图的制作步骤_Python 绘图[通俗易懂]插图27 da7c51acdde9d4f272139be8e15aa529.png](https://img.bianchenghao.cn/app/bianchenghao_cn/929cac341da14a17881ce789d4b26035.png)
![瀑布图的制作步骤_Python 绘图[通俗易懂]插图29 e37ab1c6a0abc0b5491afc88c3b87876.gif](https://img.bianchenghao.cn/app/bianchenghao_cn/c8a4c9b67ccc471f80213c5db1f10bd2.gif)
![瀑布图的制作步骤_Python 绘图[通俗易懂]插图31 c24e3efa489aff07a23eec77b4a6d669.png](https://img.bianchenghao.cn/app/bianchenghao_cn/225e5436700e4d5683cbcf75f8acf9fd.png)
![瀑布图的制作步骤_Python 绘图[通俗易懂]插图33 b21a6b154fbdc1033a3531f02dbb30a8.png](https://img.bianchenghao.cn/app/bianchenghao_cn/987034d310a74da1a2aea21d5b988cde.png)
![瀑布图的制作步骤_Python 绘图[通俗易懂]插图35 51ee967103d8bdad0b303299c025a406.png](https://img.bianchenghao.cn/app/bianchenghao_cn/51b46a0a65cf4f13a67a4e00cc6947f6.png)
![瀑布图的制作步骤_Python 绘图[通俗易懂]插图37 88f7e49b158f4668993f21d16184ff9d.png](https://img.bianchenghao.cn/app/bianchenghao_cn/b54934e6d2794e41bbec54b4f3be4715.png)
![瀑布图的制作步骤_Python 绘图[通俗易懂]插图39 56b385c50d1142990daad304aa36b066.png](https://img.bianchenghao.cn/app/bianchenghao_cn/45229f1e5d774912929974840317a05d.png)