使用python matplotlib创建水平瀑布图

2024-04-29 13:10:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个瀑布图,它类似于条形图,只是每个条形图从相邻条形图的末尾开始,在末尾或开头,因此您可以得到总数,并可以看到它是如何分解的。
我试图用python创建这个图表,但是在matplot.lib中没有称为瀑布的直接图表

我找到了垂直瀑布的代码,但无法将其转换为水平瀑布

例如,如何将barh图转换为水平瀑布图

我想创建一个水平瀑布

例如,我试图使matplotlib中的barh图表中的每一条从另一条的末尾开始,但我认为我处理问题的方法不正确,因为到目前为止我还没有结果。
应该是这样的:

enter image description here

创建绘图的代码:

my_plot = trans.plot(
    kind='barh', 
    stacked=True, 
    bottom=blank,legend=None, 
    figsize=(10, 5)
)

我怎样把这些栅栏分开

编辑

我发现了这个现成的python包,但它不适用于数据帧,所以我不能使用它

import waterfall_chart
from matplotlib import transforms

a = ['sales','returns','credit fees','rebates','late charges','shipping']
b = [10,-30,-7.5,-25,95,-7]

my_plot = waterfall_chart.plot(a, b, rotation_value=30, sorted_value=True, threshold=0.2,
                               formatting="$ {:,.1f}", net_label="end result", other_label="misc",
                               Title="chart", x_lab="X", y_lab="money", blue_color="blue",
                               green_color="#95ff24", red_color="r")

rot = transforms.Affine2D().rotate_deg(90)


my_plot.show()

我还发现了本教程,其中包含垂直瀑布图的代码。
https://pbpython.com/waterfall-chart.html.
它工作得很好,但我没有成功地将同样的东西复制到水平瀑布上


Tags: 代码importtrueplotmatplotlibmychart图表