search
HomeBackend DevelopmentPython TutorialHow to draw a stacked column chart using ECharts in Python
How to draw a stacked column chart using ECharts in PythonDec 17, 2023 am 09:48 AM
pythonechartsstacked column chart

How to draw a stacked column chart using ECharts in Python

In the field of data visualization, stacked histograms are a common visualization method. It draws multiple data series into a bar. Each bar is composed of multiple sub-items. Each sub-item corresponds to a data series and is displayed in the same coordinate system. This kind of chart can be used to compare the total size of different categories or data series, the proportion of components of each category or data series, etc. In Python, we can use the ECharts library to draw stacked histograms, and the library is richly customizable and interactive.

1. Install and import the ECharts library

Before using the ECharts library, we need to install it first. It can be installed through the pip command:

pip install pyecharts

After the installation is completed, we need to import the required components in the Python script, for example:

from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.faker import Faker

2. Draw a stacked histogram

Next, let's look at an example in which we will use the ECharts library to draw a stacked column chart to show sales for each month, where each column represents a month's sales, and each column is represented by Sales composition of different product categories.

  1. Prepare data

First, we need to prepare the data. In this example, we randomly generated sales data for 12 months, and each month included sales of 3 product categories. The code is as follows:

import random

# 随机生成12个月份的销售额数据
months = [str(i) + "月" for i in range(1, 13)]
type1_sales = [random.randint(100, 1000) for _ in range(12)]
type2_sales = [random.randint(100, 1000) for _ in range(12)]
type3_sales = [random.randint(100, 1000) for _ in range(12)]
  1. Draw the chart

Next, we add the data to the chart and customize it. The code is as follows:

# 实例化柱状图
bar = (
    Bar()
    # 添加X轴数据
    .add_xaxis(months)
    # 添加Y轴数据,并使用整数值格式化标签
    .add_yaxis("类别1", type1_sales, stack="stack1", label_opts=opts.LabelOpts(formatter="{value}元"))
    .add_yaxis("类别2", type2_sales, stack="stack1", label_opts=opts.LabelOpts(formatter="{value}元"))
    .add_yaxis("类别3", type3_sales, stack="stack1", label_opts=opts.LabelOpts(formatter="{value}元"))
    # 设置全局参数
    .set_global_opts(
        # 设置标题
        title_opts=opts.TitleOpts(title="堆叠柱状图"),
        # 设置X轴标签旋转角度
        xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-15)),
        # 设置Y轴的名称和最大值
        yaxis_opts=opts.AxisOpts(name="销售额", max_=3000),
        # 设置数据标签
        series_opts=opts.SeriesOpts(
            itemstyle_opts=opts.ItemStyleOpts(border_color="black", border_width=0),
            label_opts=opts.LabelOpts(is_show=True, position="inside", color="white")
            )
    )
)

In the above code, we instantiate a histogram and use add_xaxis() to add the X-axis data to the chart. Next, we use add_yaxis() to add three types of sales data to the chart. Since we need to stack three types of sales together, we set them all to stack1. At the same time, we use label_opts to set the formatting method of the label. Finally, we use set_global_opts() to set the global parameters of the chart, including the title, X-axis label rotation angle, Y-axis name and maximum value, and data label settings.

  1. Visualize and save the results

Finally, we use render() to visualize the results and display the results in Jupyter Notebook using render_notebook() or render(' filename.html') saves the results as an HTML file. The code is as follows:

# 在Jupyter Notebook中显示图表
bar.render_notebook()

# 将图表保存为HTML文件
bar.render("bar_chart.html")

After running the above code, we will get a clear stacked column chart, which shows the sales of each month and can reflect the proportion of sales of different product categories. than the situation.

3. Summary

This article introduces how to use the Bar component in the ECharts library to draw a stacked column chart, and uses specific code examples to show how to prepare data and how to add data to the chart. And how to customize and save charts. Of course, in actual operation, more detailed settings and adjustments to the parameters of specific components may be required to meet different visualization needs. But overall, ECharts provides Python users with a powerful and easy-to-use data visualization tool that helps to better present the results obtained during the data analysis process.

The above is the detailed content of How to draw a stacked column chart using ECharts in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
详细讲解Python之Seaborn(数据可视化)详细讲解Python之Seaborn(数据可视化)Apr 21, 2022 pm 06:08 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于Seaborn的相关问题,包括了数据可视化处理的散点图、折线图、条形图等等内容,下面一起来看一下,希望对大家有帮助。

详细了解Python进程池与进程锁详细了解Python进程池与进程锁May 10, 2022 pm 06:11 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于进程池与进程锁的相关问题,包括进程池的创建模块,进程池函数等等内容,下面一起来看一下,希望对大家有帮助。

Python自动化实践之筛选简历Python自动化实践之筛选简历Jun 07, 2022 pm 06:59 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于简历筛选的相关问题,包括了定义 ReadDoc 类用以读取 word 文件以及定义 search_word 函数用以筛选的相关内容,下面一起来看一下,希望对大家有帮助。

归纳总结Python标准库归纳总结Python标准库May 03, 2022 am 09:00 AM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于标准库总结的相关问题,下面一起来看一下,希望对大家有帮助。

Python数据类型详解之字符串、数字Python数据类型详解之字符串、数字Apr 27, 2022 pm 07:27 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于数据类型之字符串、数字的相关问题,下面一起来看一下,希望对大家有帮助。

分享10款高效的VSCode插件,总有一款能够惊艳到你!!分享10款高效的VSCode插件,总有一款能够惊艳到你!!Mar 09, 2021 am 10:15 AM

VS Code的确是一款非常热门、有强大用户基础的一款开发工具。本文给大家介绍一下10款高效、好用的插件,能够让原本单薄的VS Code如虎添翼,开发效率顿时提升到一个新的阶段。

详细介绍python的numpy模块详细介绍python的numpy模块May 19, 2022 am 11:43 AM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于numpy模块的相关问题,Numpy是Numerical Python extensions的缩写,字面意思是Python数值计算扩展,下面一起来看一下,希望对大家有帮助。

python中文是什么意思python中文是什么意思Jun 24, 2019 pm 02:22 PM

pythn的中文意思是巨蟒、蟒蛇。1989年圣诞节期间,Guido van Rossum在家闲的没事干,为了跟朋友庆祝圣诞节,决定发明一种全新的脚本语言。他很喜欢一个肥皂剧叫Monty Python,所以便把这门语言叫做python。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version