Home > Backend Development > Python Tutorial > Method to generate horizontal histogram using ECharts and Python interface

Method to generate horizontal histogram using ECharts and Python interface

WBOY
Release: 2023-12-18 18:48:56
Original
1067 people have browsed it

Method to generate horizontal histogram using ECharts and Python interface

Method of generating horizontal histogram using ECharts and Python interface

ECharts is a visual chart library developed based on JavaScript, which can easily create various data visualization charts. Combined with the Python interface, data processing and visualization can be more conveniently performed.

This article will introduce the method of generating horizontal histograms using ECharts and Python interfaces, and provide specific code examples.

  1. Prepare data

First, we need to prepare the data. Here we take the scores of students in a certain class as an example. Suppose we have the following data:

Name 中文 English
张三 90 80 75
李四 75 95 85
王五 80 90 70
Zhao Liu 85 75 90

We can use the Pandas library in Python to read data and process it. The specific code is as follows:

import pandas as pd

# 读取数据
df = pd.read_csv('成绩表.csv')

# 将姓名作为索引
df.set_index('姓名', inplace=True)

# 取出各科成绩
chinese = df['语文']
math = df['数学']
english = df['英语']

# 计算平均成绩
average = df.mean(axis=1)
Copy after login
  1. Use ECharts to draw a horizontal histogram

Next, we use ECharts to draw a horizontal histogram. The specific code is as follows:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>水平柱状图示例</title>
    <script src="https://cdn.staticfile.org/echarts/5.0.2/echarts.min.js"></script>
</head>
<body>
    <div id="main" style="height: 500px;"></div>
    <script>
        // 初始化echarts实例
        var myChart = echarts.init(document.getElementById('main'))

        // 配置项
        var option = {
            title: {
                text: '某班级成绩'
            },
            tooltip: {},
            legend: {
                data:['语文', '数学', '英语', '平均分']
            },
            xAxis: {
                type: 'value',
                axisLabel: {
                    show: false
                }
            },
            yAxis: {
                type: 'category',
                data: ['张三', '李四', '王五', '赵六']
            },
            series: [
                {
                    name: '语文',
                    type: 'bar',
                    stack: '总成绩',
                    label: {
                        show: true,
                        position: 'right'
                    },
                    data: chinese
                },
                {
                    name: '数学',
                    type: 'bar',
                    stack: '总成绩',
                    label: {
                        show: true,
                        position: 'right'
                    },
                    data: math
                },
                {
                    name: '英语',
                    type: 'bar',
                    stack: '总成绩',
                    label: {
                        show: true,
                        position: 'right'
                    },
                    data: english
                },
                {
                    name: '平均分',
                    type: 'bar',
                    stack: '总成绩',
                    label: {
                        show: true,
                        position: 'right'
                    },
                    data: average
                }
            ]
        };

        myChart.setOption(option)
    </script>
</body>
</html>
Copy after login

Run the code, and we will see a beautiful horizontal bar chart.

  1. Summary

This article introduces the method of generating horizontal histograms using ECharts and Python interfaces, and provides specific code examples. Through this method, we can easily process and visualize the data, making the data more intuitive and easy to understand.

The above is the detailed content of Method to generate horizontal histogram using ECharts and Python interface. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template