首页 / PYTHON / 【Python教程】绘制瀑布图
【Python教程】绘制瀑布图
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了【Python教程】绘制瀑布图,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1818字,纯文字阅读大概需要3分钟。
内容图文
瀑布图是由麦肯锡顾问公司所独创的图表类型,因为形似瀑布流水而称之为瀑布图( Waterfall Plot)。此种图表采用绝对值与相对值结合的方式,多适用于表达多个特定数值之间的数量变化关系。本文简单介绍如何利用Python绘制该图。命令如下
1)导入程序包 import numpy as np import pandas as pd import matplotlib.pyplot as plt from matplotlib.ticker import FuncFormatter 2)导入及清理数据 def money(x, pos): return "${:,.0f}".format(x) formatter = FuncFormatter(money) index = ['sales','returns','credit fees','rebates','late charges','shipping'] data = {'amount': [350000,-30000,-7500,-25000,95000,-7000]} trans = pd.DataFrame(data=data,index=index) blank = trans.amount.cumsum().shift(1).fillna(0) total = trans.sum().amount trans.loc["net"]= total blank.loc["net"] = total step = blank.reset_index(drop=True).repeat(3).shift(-1) step[1::3] = np.nan blank.loc["net"] = 0 3)绘制图像 my_plot = trans.plot(kind='bar', stacked=True, bottom=blank,legend=None, figsize=(10, 5), title="2014 Sales Waterfall") my_plot.plot(step.index, step.values,'k') my_plot.set_xlabel("Transaction Types") my_plot.yaxis.set_major_formatter(formatter) y_height = trans.amount.cumsum().shift(1).fillna(0) max = trans.max() neg_offset = max / 25 pos_offset = max / 50 plot_offset = int(max / 15) loop = 0 for index, row in trans.iterrows(): if row['amount'] == total: y = y_height[loop] else: y = y_height[loop] + row['amount'] if row['amount'] > 0: y += pos_offset else: y -= neg_offset my_plot.annotate("{:,.0f}".format(row['amount']),(loop,y),ha="center") loop+=1 my_plot.set_ylim(0,blank.max()+int(plot_offset)) my_plot.set_xticklabels(trans.index,rotation=0) my_plot.get_figure().savefig("waterfall.png",dpi=200,bbox_inches='tight')
输出如下
以上就是【Python教程】绘制瀑布图的内容,更多相关内容请关注PHP中文网(www.gxlcms.com)!
内容总结
以上是互联网集市为您收集整理的【Python教程】绘制瀑布图全部内容,希望文章能够帮你解决【Python教程】绘制瀑布图所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。