Home  >  Article  >  Backend Development  >  How to draw scatter plots using ECharts and Python interface

How to draw scatter plots using ECharts and Python interface

WBOY
WBOYOriginal
2023-12-17 16:06:54797browse

How to draw scatter plots using ECharts and Python interface

How to draw scatter plots using ECharts and Python interface

ECharts is an excellent open source visualization library, developed based on JavaScript language, and can be configured through simple Realize the drawing and interactive effects of various charts. Python is a powerful programming language with a rich data processing and visualization toolkit, which provides a lot of convenience for data analysis and visualization.

This article will introduce how to use ECharts and Python interfaces to draw scatter plots, and give specific code examples. First, we need to make sure that Python and ECharts' Python interface library pyecharts are installed.

The first step is to import the required libraries and modules:

from pyecharts import options as opts
from pyecharts.charts import Scatter
from pyecharts.commons.utils import JsCode

The second step is to prepare the data. Let's take a student's performance data as an example. Assume there are 5 students. Each student's English score and math score are as follows:

data = [
    [80, 90],
    [85, 95],
    [70, 75],
    [60, 80],
    [75, 85]
]

The third step is to create a scatter chart object using the Scatter class of ECharts. And configure the basic parameters of the chart:

scatter = (
    Scatter()
    .add_xaxis([x for x, y in data])
    .add_yaxis(
        "",
        [y for x, y in data],
        symbol_size=20,
        label_opts=opts.LabelOpts(is_show=False),
    )
    .set_series_opts()
    .set_global_opts(
        title_opts=opts.TitleOpts(title="学生英语成绩与数学成绩散点图"),
        xaxis_opts=opts.AxisOpts(name="英语成绩"),
        yaxis_opts=opts.AxisOpts(name="数学成绩"),
    )
)

Here, we use the add_xaxis and add_yaxis methods to pass in the x-axis and y-axis data respectively, and set the scatter point size to 20 and the label to not be displayed. Then use the set_series_opts and set_global_opts methods for series and global configuration, and set the title of the chart, the name of the x-axis, and the name of the y-axis.

The fourth step is to generate the HTML file and open it in the browser.

scatter.render("scatter.html")

After executing the above code, an HTML file named scatter.html will be generated. We can open the file directly in the browser to see the generated scatter plot.

The above is how to draw a scatter plot using ECharts and Python interface. I hope it can help you. Through simple configuration and code examples, we can easily implement various types of scatter plots, and customize the settings and style of the charts. At the same time, ECharts’ interactive functions and rich visualization effects also provide great convenience for data analysis and visualization.

The above is the detailed content of How to draw scatter plots using ECharts and Python interface. 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