Home >Backend Development >Python Tutorial >How to draw a radar chart using ECharts in Python

How to draw a radar chart using ECharts in Python

王林
王林Original
2023-12-17 14:40:081168browse

How to draw a radar chart using ECharts in Python

How to use ECharts to draw a radar chart in Python

Abstract:
A radar chart is a multi-dimensional data visualization chart used to show the relationships between various dimensions relationship and degree of difference. This article will introduce using the ECharts library in Python to draw radar charts and provide detailed code examples.

Keywords: Python, ECharts, radar chart, visualization, code example

  1. Introduction
    In data visualization, radar charts are often used to display the relationship between multiple dimensions. relationship and degree of difference. Each dimension is represented as an angle in the radar chart, and the data values ​​are represented as the length of the radius along that dimension. ECharts in Python is a powerful visualization library that provides functions for drawing various types of charts, including radar charts.
  2. Install ECharts library
    Before starting, you need to install the ECharts library first. You can use the following command to install ECharts in the Python environment:

    pip install pyecharts
  3. Import ECharts library
    In the Python code, you first need to import the relevant modules of the ECharts library:

    from pyecharts import options as opts
    from pyecharts.charts import Radar
  4. Create Radar Chart
    To create a radar chart, you need to first define the chart object, and then set the relevant parameters and data. The following example creates a simple radar chart:
radar = (
    Radar()
    .add_schema(
        schema=[
            opts.RadarIndicatorItem(name="维度1", max_=100),
            opts.RadarIndicatorItem(name="维度2", max_=100),
            opts.RadarIndicatorItem(name="维度3", max_=100),
            opts.RadarIndicatorItem(name="维度4", max_=100),
            opts.RadarIndicatorItem(name="维度5", max_=100),
        ]
    )
    .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
)

In the above code, a radar chart object is created by calling the Radar() function, using add_schema()Method to set the dimensions and value range of the radar chart. In the example, 5 dimensions are defined, and the maximum value of each dimension is set to 100. Then use the set_series_opts() method to hide the display of data labels.

  1. Add data
    After creating the radar chart, you need to add the corresponding data. The following example demonstrates how to add data:
radar.add("系列1", [[90, 80, 70, 60, 50]], color="#FF0000")
radar.add("系列2", [[60, 70, 80, 90, 100]], color="#00FF00")
radar.add("系列3", [[70, 60, 50, 40, 30]], color="#0000FF")

In the above code, three series of data are added by calling the add() method. The data format of each series is a list, and each element in the list represents a data value in one dimension. In the example, the data of series 1 is [90, 80, 70, 60, 50], the data of series 2 is [60, 70, 80, 90, 100], and the data of series 3 is [70, 60, 50, 40, 30]. At the same time, different series of colors can be set through the color parameter.

  1. Set global parameters
    You can set global parameters, such as title, legend, etc., through the set_global_opts() method. The following example sets the title and legend:
radar.set_global_opts(title_opts=opts.TitleOpts(title="雷达图示例"),
                      legend_opts=opts.LegendOpts(pos_left="center",pos_top="bottom"))

In the above code, use the title_opts parameter to set the title of the chart to "Radar Chart Example", use legend_optsThe parameter sets the legend to be displayed centered at the bottom.

  1. Rendering and Saving Charts
    After all settings are completed, the radar chart can be rendered to an HTML file via the render() method and can be rendered via render_notebook()Method displays charts directly in Jupyter Notebook. The following example demonstrates how to save a radar chart as an HTML file:

    radar.render("radar_chart.html")

    A file named "radar_chart.html" is generated under the specified path, which can be opened through a browser to view the chart.

    Conclusion:
    This article introduces how to use the ECharts library in Python to draw radar charts and provides detailed code examples. By using the ECharts library, various types of charts can be easily created and customized, making data visualization easier and more intuitive. Readers can modify and expand the parameters and data of the radar chart according to their own needs to meet different visualization needs.

References:

  1. https://pyecharts.org/#/zh-cn/
  2. https://echarts. apache.org/examples/zh/editor.html?c=radar

The above is the detailed content of How to draw a radar 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