Skip to content

Python pyecharts Module

pyecharts is a Python data visualization library based on ECharts, allowing users to generate various types of interactive charts and data visualizations using the Python language.

ECharts is an open-source visualization library implemented in JavaScript, and Pyecharts is a Python wrapper for ECharts, making it more convenient to use ECharts in Python.

pyecharts provides a set of simple and flexible APIs that enable users to easily create various charts including but not limited to line charts, bar charts, scatter plots, pie charts, maps, etc.

With pyecharts, users can process and prepare data using the Python language, and then use concise code to generate interactive charts that can be embedded in web applications or saved as static files.

pyecharts Features and Functions:

  • Easy to Use: Pyecharts provides an intuitive and friendly API, allowing users to quickly get started and easily generate various charts.

  • Rich Chart Types: Supports a variety of common chart types, including line charts, bar charts, scatter plots, pie charts, maps, etc., meeting the needs of different scenarios.

  • Supports Mainstream Data Formats: Capable of handling common data formats such as lists, dictionaries, Pandas DataFrames, etc.

  • Interactivity: Generated charts can be interactive, allowing users to interact with charts through mouse hovering, zooming, etc.

  • Rich Configuration Options: Provides rich configuration options, allowing users to customize chart styles, layouts, and other properties.

  • Theme Support: Provides multiple themes, allowing users to choose the appropriate theme to make charts more consistent with the overall style of the application.

pip installation:

pip install pyecharts

Source installation:

$ git clone https://github.com/pyecharts/pyecharts.git
$ cd pyecharts
$ pip install -r requirements.txt
$ python setup.py install
# Or run python install.py

After successful installation, you can check the pyecharts version:

import pyecharts

print(pyecharts.__version__)

Output is as follows:

2.0.4

pyecharts supports the following chart types:

Chart Type pyecharts Class Import
Line Chart Line from pyecharts.charts import Line
Bar Chart Bar from pyecharts.charts import Bar
Scatter Plot Scatter from pyecharts.charts import Scatter
Pie Chart Pie from pyecharts.charts import Pie
Radar Chart Radar from pyecharts.charts import Radar
Heatmap HeatMap from pyecharts.charts import HeatMap
K-line Chart Kline from pyecharts.charts import Kline
Box Plot Boxplot from pyecharts.charts import Boxplot
Map Map from pyecharts.charts import Map
Word Cloud WordCloud from pyecharts.charts import WordCloud
Gauge Gauge from pyecharts.charts import Gauge
Funnel Chart Funnel from pyecharts.charts import Funnel
Tree Chart Tree from pyecharts.charts import Tree
Parallel Coordinates Parallel from pyecharts.charts import Parallel
Sankey Diagram Sankey from pyecharts.charts import Sankey
Geo Chart Geo from pyecharts.charts import Geo
Timeline Timeline from pyecharts.charts import Timeline
3D Scatter Plot Scatter3D from pyecharts.charts import Scatter3D
3D Bar Chart Bar3D from pyecharts.charts import Bar3D
3D Surface Plot Surface3D from pyecharts.charts import Surface3D

Next, we use Pyecharts to create a simple bar chart showing sales for five months:

from pyecharts.charts import Bar

# Prepare data
x_data = ['January', 'February', 'March', 'April', 'May']
y_data = [10, 20, 15, 25, 30]

# Create bar chart
bar_chart = Bar()
bar_chart.add_xaxis(x_data)
bar_chart.add_yaxis("Sales", y_data)

# You can also pass a path parameter, e.g., bar_chart.render("bar_chart.html")
bar_chart.render()

If no file path is specified in bar_chart.render(), Pyecharts will by default generate a file named “render.html” in the current working directory, meaning the generated chart will be saved in the “render.html” file.

The execution result of the above code is:

If you want the chart file name to have a certain convention or want to specify a save path, you can provide a file path parameter in the render() method. For example:

bar_chart.render("my_bar_chart.html")

This way, the generated chart will be saved in the “my_bar_chart.html” file in the current working directory.

Setting Chart Configuration Options

In the example, the chart title is “Monthly Sales Bar Chart”, the horizontal axis is the month, and the vertical axis is sales. You can adjust the data and chart configuration according to actual needs:

from pyecharts import options as opts
from pyecharts.charts import Bar

# Prepare data
x_data = ['January', 'February', 'March', 'April', 'May']
y_data = [10, 20, 15, 25, 30]

# Create bar chart
bar_chart = Bar()
bar_chart.add_xaxis(x_data)
bar_chart.add_yaxis("Sales", y_data)

# Configure chart
bar_chart.set_global_opts(
    title_opts=opts.TitleOpts(title="Monthly Sales Bar Chart"),
    xaxis_opts=opts.AxisOpts(name="Month"),
    yaxis_opts=opts.AxisOpts(name="Sales (10k RMB)"),
)

# Render chart
bar_chart.render("bar_chart.html")

Description:

  • Bar(): Creates a bar chart object.
  • add_xaxis and add_yaxis: Used to add horizontal and vertical axis data respectively.
  • set_global_opts: Configures global options, including title, axis names, etc.

The generated chart will be saved as the “bar_chart.html” file. You can open this file in a browser to view the generated bar chart.

pyecharts supports theme switching, allowing users to choose the appropriate theme to change the chart style according to their needs.

pyecharts provides 10+ built-in themes, and developers can also customize their preferred themes.

The following is a simple example demonstrating how to switch themes in pyecharts:

from pyecharts import options as opts
from pyecharts.charts import Bar
# Built-in theme types can be viewed in pyecharts.globals.ThemeType
from pyecharts.globals import ThemeType

# Prepare data
x_data = ['January', 'February', 'March', 'April', 'May']
y_data = [10, 20, 15, 25, 30]

# Create bar chart
bar_chart = Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))  # Initial theme is light
bar_chart.add_xaxis(x_data)
bar_chart.add_yaxis("Sales", y_data)

# Configure chart
bar_chart.set_global_opts(
    title_opts=opts.TitleOpts(title="Monthly Sales Bar Chart"),
    xaxis_opts=opts.AxisOpts(name="Month"),
    yaxis_opts=opts.AxisOpts(name="Sales (10k RMB)"),
)

# Switch to dark theme
bar_chart.set_global_opts(theme=ThemeType.DARK)

# Render chart
bar_chart.render("themed_bar_chart.html")

The above example demonstrates how to use ThemeType in pyecharts to switch themes. The theme types supported by pyecharts include LIGHT, DARK, etc. You can choose the appropriate theme according to your needs.

  • init_opts=opts.InitOpts(theme=ThemeType.LIGHT): When creating a chart object, specify the initial theme of the chart through the init_opts parameter. Here it is set to light.

The generated chart is as follows:

The following is the list of themes supported by pyecharts:

Light Themes:

  • "LIGHT": Default light theme
  • "WESTEROS": Classic warm-tone theme
  • "CHALK": Chalk-style theme
  • "ESSOS": Mild green-tone theme
  • "INFOGRAPHIC": Infographic theme
  • "MACARONS": Delicious candy-color theme

Dark Themes:

  • "DARK": Default dark theme
  • "PURPLE-PASSION": Deep purple-tone theme
  • "SHINE": Simple black-tone theme
  • "VINTAGE": Vintage-style theme
  • "ROMA": Ancient Roman-style theme
  • "WALDEN": Forest dark-tone theme

Users can create custom themes by setting custom colors and styles.


set_global_opts is a method in pyecharts used to set global configuration options. This method allows you to configure some global properties of the entire chart, such as title, axes, legend, etc.

The following are some commonly used global configuration options:

bar_chart.set_global_opts(
    title_opts=opts.TitleOpts(title="Monthly Sales Bar Chart", subtitle="Subtitle"),
    xaxis_opts=opts.AxisOpts(name="Month"),
    yaxis_opts=opts.AxisOpts(name="Sales (10k RMB)"),
    legend_opts=opts.LegendOpts(pos_left="center", pos_top="top"),
    toolbox_opts=opts.ToolboxOpts(),
    tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="cross"),
)

Description:

  • title_opts: Title configuration options, can set the main title and subtitle, as well as related style settings.
  • xaxis_opts and yaxis_opts: x-axis and y-axis configuration options, can set axis names, axis line styles, etc.
  • legend_opts: Legend configuration options, can set legend position, style, etc.
  • toolbox_opts: Toolbox configuration options, used to add some interactive tools, such as save as image, data view, etc.
  • tooltip_opts: Tooltip configuration options, can set tooltip trigger method, style, etc.
from pyecharts import options as opts
from pyecharts.charts import Bar

# Prepare data
x_data = ['January', 'February', 'March', 'April', 'May']
y_data = [10, 20, 15, 25, 30]

# Create bar chart
bar_chart = Bar()
bar_chart.add_xaxis(x_data)
bar_chart.add_yaxis("Sales", y_data)

# Configure global properties
bar_chart.set_global_opts(
    title_opts=opts.TitleOpts(title="Monthly Sales Bar Chart", subtitle="Subtitle"),
    xaxis_opts=opts.AxisOpts(name="Month"),
    yaxis_opts=opts.AxisOpts(name="Sales (10k RMB)"),
    legend_opts=opts.LegendOpts(pos_left="center", pos_top="top"),
    toolbox_opts=opts.ToolboxOpts(),
    tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="cross"),
)

# Render chart
bar_chart.render("global_options_bar_chart.html")

The generated chart is as follows:

For more pyecharts module content, refer to: https://pyecharts.org/#/zh-cn/intro