Software Testing | Python Data Visualization Artifact - pyecharts Tutorial (10)

Draw funnel chart using pyecharts

Introduction

A Funnel Chart is a chart type used to visualize data flow or conversion rates. It usually consists of a series of stages, each with a name and a value that represents the amount of conversions or progress in the data flow during that stage. The funnel chart gets its name from its appearance, which resembles an actual funnel shape, with it being wider at the top and narrower at the bottom, consistent with the gradual reduction or filtering of data. In this post, we’ll cover how to create a funnel chart using Python’s Pyecharts library to showcase various stages in a data flow or conversion rate.

Environmental preparation

To draw a funnel chart, first we need to install pyechartsthe library. The installation command is as follows:

pip install pyecharts

Import funnel chart class

Import the relevant modules in Pyecharts and other libraries that need to be used:

from pyecharts import options as opts
from pyecharts.charts import Funnel

data preparation

Before drawing a funnel chart, we need to prepare the data. Funnel charts typically consist of a series of stages, each with a name and a value that represents the number of conversions during that stage. as follows:

data = [("访问网站", 100),
        ("浏览商品", 80),
        ("加入购物车", 60),
        ("完成购买", 30)]

Create a funnel chart

Use Pyecharts’ Funnel class to create a funnel chart. Set the basic parameters of the funnel chart, such as title, width, height, etc. code show as below:

funnel = (
    Funnel()
    .add(
        "转化率",
        data,
        gap=20,
        tooltip_opts=opts.TooltipOpts(trigger="item", formatter="{a} <br/>{b}: {c}%"),
    )
    .set_global_opts(
        title_opts=opts.TitleOpts(title="漏斗图示例"),
        tooltip_opts=opts.TooltipOpts(trigger="item", formatter="{a} <br/>{b}: {c}%"),
    )
)

In the above code, we used gapparameters to set the spacing between each funnel stage and added tooltips to display the stage name and conversion rate.

Draw a funnel chart

Finally, use the render method to save the funnel chart as an HTML file or display it in a Jupyter Notebook. as follows:

funnel.render("funnel_chart.html")

Usage example

Here is our complete code for the steps to draw a funnel chart:

from pyecharts import options as opts
from pyecharts.charts import Funnel

data = [("访问网站", 100),
        ("浏览商品", 80),
        ("加入购物车", 60),
        ("完成购买", 30)]

funnel = (
    Funnel()
    .add(
        "转化率",
        data,
        gap=20,
        tooltip_opts=opts.TooltipOpts(trigger="item", formatter="{a} <br/>{b}: {c}%"),
    )
    .set_global_opts(
        title_opts=opts.TitleOpts(title="漏斗图示例"),
        tooltip_opts=opts.TooltipOpts(trigger="item", formatter="{a} <br/>{b}: {c}%"),
    )
)

funnel.render("funnel_chart.html")

After running the above code, an HTML file named "funnel_chart.html" will be generated, which contains our funnel chart. The generated image is as follows:

Insert image description here

Other examples

  1. Set label position

We can see that the above example label is outside the funnel, and we can control the position of the label by setting the properties label_optsof the parameter position. The position parameter can be set to one of the following values:

  • "inside": The label is displayed inside the funnel chart, the default position.
  • "outside": The label appears outside the funnel chart.

For example, use position="inside"can display labels inside a funnel chart. code show as below:

from pyecharts import options as opts
from pyecharts.charts import Funnel

data = [("访问网站", 100),
        ("浏览商品", 80),
        ("加入购物车", 60),
        ("完成购买", 30)]

funnel = (
    Funnel()
    .add(
        "转化率",
        data,
        gap=20,
        tooltip_opts=opts.TooltipOpts(trigger="item", formatter="{a} <br/>{b}: {c}%"),
        label_opts= opts.LabelOpts(position="inside")
    )
    .set_global_opts(
        title_opts=opts.TitleOpts(title="漏斗图示例2"),
        tooltip_opts=opts.TooltipOpts(trigger="item", formatter="{a} <br/>{b}: {c}%"),
    )
)

funnel.render("funnel_chart2.html")

Run the code and open the generated HTML file in the browser. The image is as follows:

Insert image description here

  1. Percent Funnel Chart

To display the percentage of each stage in a funnel chart, you can set it using label_optsparameters and properties. formatterHere is the modified code:


Run the script and the generated HTML file will be opened in the browser as shown below:

Insert image description here

Summarize

This article mainly introduces the use of pyecharts to draw funnel charts. Funnel charts are a powerful tool for visualizing and analyzing data processes, especially suitable for business scenarios that require understanding of conversion rates and processes. I hope this article can help you use funnel charts for data analysis.

Guess you like

Origin blog.csdn.net/Tester_muller/article/details/132810028