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

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.
