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

How to draw a bubble chart using ECharts in Python

PHPz
Release: 2023-12-17 19:17:59
Original
1804 people have browsed it

How to draw a bubble chart using ECharts in Python

How to use ECharts to draw bubble charts in Python

ECharts is a data visualization library based on JavaScript, which provides a variety of chart types, including bar charts and line charts. Graphs, pie charts, etc. In ECharts, bubble chart is a commonly used chart type, which can show the distribution of data through dots of different sizes. This article will introduce in detail how to use ECharts to draw bubble charts in Python and provide specific code examples.

Step 1: Install the necessary libraries and tools
To use ECharts in Python, you first need to install the pyecharts library. It can be installed through the pip command:

pip install pyecharts
Copy after login

In addition, in order to achieve dynamic effects in Python, you also need to install the moviepy library:

pip install moviepy
Copy after login

Step 2: Create data
First, we need to create Some sample data to draw a bubble chart. Suppose we have a data set about cities, which includes the city's name, longitude, latitude, population and GDP value. We can use a dictionary list to store this data, as shown below:

data = [
    {"name": "北京", "lng": 116.40717, "lat": 39.90469, "population": 2154, "gdp": 30320},
    {"name": "上海", "lng": 121.4737, "lat": 31.23037, "population": 2424, "gdp": 32680},
    {"name": "广州", "lng": 113.27323, "lat": 23.15792, "population": 1505, "gdp": 19612},
    {"name": "深圳", "lng": 114.06667, "lat": 22.54892, "population": 1303, "gdp": 24222},
    {"name": "杭州", "lng": 120.15507, "lat": 30.27408, "population": 981, "gdp": 13468},
]
Copy after login

Step 3: Draw a bubble chart
Next, we can use the pyecharts library to draw a bubble chart. First, you need to import the required classes and functions:

from pyecharts.charts import Geo
from pyecharts import options as opts
Copy after login

Then, create a Geo object and specify the title and data of the chart:

geo = (
    Geo()
    .set_global_opts(
        title_opts=opts.TitleOpts(title="中国城市气泡图"),
        visualmap_opts=opts.VisualMapOpts(max_=5000),
    )
    .add_schema(
        maptype="china",
        itemstyle_opts=opts.ItemStyleOpts(color="#323c48", border_color="#111"),
    )
    .add("气泡图", [list(d.values()) for d in data], type_=GeoType.EFFECT_SCATTER)
)
Copy after login

Here, we use set_global_opts() to set the title and visualization options, use add_schema() to set the map type and style, and use add() to add data and set the chart type.

Finally, use render() to save the chart as an HTML file:

geo.render("bubble_chart.html")
Copy after login

Step 4: Run the code and view the results
After running the above code, you will see A file named bubble_chart.html is generated in the current directory. Open the file with a browser to view the bubble chart drawn.

Summary:
This article introduces how to use ECharts to draw bubble charts in Python and provides specific code examples. By using the pyecharts library, we can easily implement rich chart visualization effects in the Python environment. I hope that readers can further master the method of drawing bubble charts using ECharts through the introduction of this article and apply it to actual projects.

The above is the detailed content of How to draw a bubble chart using ECharts in Python. 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