PHP前端开发

【Python教程】绘制瀑布图

百变鹏仔 3小时前 #Python
文章标签 瀑布

瀑布图是由麦肯锡顾问公司所独创的图表类型,因为形似瀑布流水而称之为瀑布图( waterfall plot)。此种图表采用绝对值与相对值结合的方式,多适用于表达多个特定数值之间的数量变化关系。本文简单介绍如何利用python绘制该图。

命令如下

1)导入程序包import numpy as npimport pandas as pdimport matplotlib.pyplot as pltfrom matplotlib.ticker import FuncFormatter2)导入及清理数据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().amounttrans.loc["net"]= totalblank.loc["net"] = totalstep = blank.reset_index(drop=True).repeat(3).shift(-1)step[1::3] = np.nanblank.loc["net"] = 03)绘制图像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 / 25pos_offset = max / 50plot_offset = int(max / 15)loop = 0for 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_offsetelse:y -= neg_offsetmy_plot.annotate("{:,.0f}".format(row['amount']),(loop,y),ha="center")loop+=1my_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')

输出如下