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
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
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
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 theRadar()
function, usingadd_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 theset_series_opts()
method to hide the display of data labels.
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 theadd()
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.
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 thetitle_opts
parameter to set the title of the chart to "Radar Chart Example", uselegend_opts
The parameter sets the legend to be displayed centered at the bottom.
Rendering and Saving Charts
After all settings are completed, the radar chart can be rendered to an HTML file via therender()
method and can be rendered viarender_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:
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!