最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

【Python教程】绘制瀑布图

来源:动视网 责编:小采 时间:2020-11-27 14:16:49
文档

【Python教程】绘制瀑布图

【Python教程】绘制瀑布图:瀑布图是由麦肯锡顾问公司所独创的图表类型,因为形似瀑布流水而称之为瀑布图( Waterfall Plot)。此种图表采用绝对值与相对值结合的方式,多适用于表达多个特定数值之间的数量变化关系。本文简单介绍如何利用Python绘制该图。命令如下1)导入程序包 imp
推荐度:
导读【Python教程】绘制瀑布图:瀑布图是由麦肯锡顾问公司所独创的图表类型,因为形似瀑布流水而称之为瀑布图( Waterfall Plot)。此种图表采用绝对值与相对值结合的方式,多适用于表达多个特定数值之间的数量变化关系。本文简单介绍如何利用Python绘制该图。命令如下1)导入程序包 imp
 瀑布图是由麦肯锡顾问公司所独创的图表类型,因为形似瀑布流水而称之为瀑布图( 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教程】绘制瀑布图

【Python教程】绘制瀑布图:瀑布图是由麦肯锡顾问公司所独创的图表类型,因为形似瀑布流水而称之为瀑布图( Waterfall Plot)。此种图表采用绝对值与相对值结合的方式,多适用于表达多个特定数值之间的数量变化关系。本文简单介绍如何利用Python绘制该图。命令如下1)导入程序包 imp
推荐度:
标签: 教程 绘制 python
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top