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.
- 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)]
- 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.
- 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!

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

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

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

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

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

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

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


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

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
Chinese version, very easy to use

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Linux new version
SublimeText3 Linux latest version