Of course! Let's dive deep into ECharts for Python. This is a fantastic library for creating beautiful, interactive, and publication-ready statistical graphics directly from your Python environment.
Here's a comprehensive guide covering what it is, how to install it, a quick start, key features, and advanced usage.
What is ECharts for Python?
ECharts for Python is a Python wrapper for Apache ECharts, a powerful, open-source JavaScript charting library. It allows you to use the familiar Python syntax to generate complex and interactive visualizations that are rendered using the robust ECharts JavaScript library on the backend.
The primary library that provides this wrapper is called pyecharts.
Key Advantages:
- Interactivity: Out-of-the-box features like zooming, panning, data zoom, tooltips, and legend toggling.
- Rich Chart Types: Supports a vast array of charts, including common ones (line, bar, pie) and advanced ones (graph, 3D charts, heatmap, gauge, sunburst).
- High Customization: Offers granular control over almost every visual aspect of the chart (colors, fonts, axes, labels, themes).
- Ease of Use: Simple Python API that is intuitive for Python developers.
- Exportability: Charts can be easily exported to static images (PNG, JPG) or interactive HTML files.
Installation
First, you need to install the pyecharts library. It's highly recommended to use a package manager like pip.
# Basic installation pip install pyecharts # For a specific version if needed pip install pyecharts==2.0.4 # Optional but recommended: For better performance in Jupyter environments pip install "pyecharts-jupyter>=2.0.0"
A Simple Example: Your First Chart
The core workflow for pyecharts involves three simple steps:
- Import the specific chart class you want to use.
- Instantiate the chart object and add your data.
- Render the chart to an HTML file or display it in a Jupyter Notebook.
Let's create a simple line chart.
Example: Line Chart
# Step 1: Import the necessary classes
from pyecharts.charts import Line
from pyecharts import options as opts
# Step 2: Prepare your data
# x-axis data (categories)
categories = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
# y-axis data (values)
values = [150, 230, 224, 218, 135, 147, 260]
# Step 3: Create and configure the chart
# Instantiate the Line chart
line_chart = Line()
# Add the x-axis data
line_chart.add_xaxis(categories)
# Add the y-axis data series, with a name for the legend
line_chart.add_yaxis("Sales", values)
# Set global options for the chart (title, tooltip, legend, etc.)
line_chart.set_global_opts(opts=opts.TitleOpts(title="Weekly Sales Report", subtitle="A simple line chart"),
tooltip_opts=opts.TooltipOpts(trigger="axis"), # Show tooltip on axis hover
legend_opts=opts.LegendOpts(is_show=True) # Show the legend
)
# Step 4: Render the chart to an HTML file
# This will create an 'interactive_line_chart.html' file in your current directory
line_chart.render("interactive_line_chart.html")
print("Chart has been rendered to interactive_line_chart.html")
When you run this code, a file named interactive_line_chart.html will be created. Open it in your web browser, and you'll see an interactive line chart!
Working in Jupyter Notebooks / Google Colab
The experience in Jupyter is even better. You can render the chart directly in the cell output.
Just add .render_notebook() to the end of your script instead of .render().
# Make sure you have pyecharts-jupyter installed
# pip install "pyecharts-jupyter>=2.0.0"
from pyecharts.charts import Bar
from pyecharts import options as opts
# Data
x_data = ["Product A", "Product B", "Product C", "Product D", "Product E"]
y_data = [5, 20, 36, 10, 75]
# Create the chart
bar_chart = (
Bar()
.add_xaxis(x_data)
.add_yaxis("Q1 Sales", y_data)
.set_global_opts(
title_opts=opts.TitleOpts(title="Product Sales Comparison"),
yaxis_opts=opts.AxisOpts(name="Units Sold"), # Custom axis name
)
)
# Render in the notebook
bar_chart.render_notebook()
This will display the interactive bar chart directly within your notebook cell.
Common Chart Types
pyecharts supports a huge variety of charts. Here are examples for a few common ones.
Bar Chart
from pyecharts.charts import Bar
bar = (
Bar()
.add_xaxis(["Shirts", "Cardigans", "Chiffons", "Pants", "Heels", "Socks"])
.add_yaxis("2025", 115, 82, 121, 56, 55, 40)
.add_yaxis("2025", 125, 95, 111, 69, 60, 55)
.set_global_opts(title_opts=opts.TitleOpts(title="Annual Clothing Sales"))
)
bar.render("bar_chart.html")
Pie Chart
from pyecharts.charts import Pie
pie = (
Pie()
.add(
"Browser Share",
[list(z) for z in zip(["Chrome", "Firefox", "Safari", "IE", "Others"], [45, 25, 15, 10, 5])],
radius=["40%", "75%"], # Inner and outer radius for a donut chart
)
.set_global_opts(title_opts=opts.TitleOpts(title="Browser Market Share"))
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {d}%")) # Custom label format
)
pie.render("pie_chart.html")
Scatter Plot
from pyecharts.charts import Scatter
scatter = (
Scatter()
.add_xaxis([i for i in range(50)])
.add_yaxis(
"series 1",
[i * 2 + 1 + i % 5 for i in range(50)],
symbol_size=10,
)
.set_global_opts(title_opts=opts.TitleOpts(title="Basic Scatter Chart"))
)
scatter.render("scatter_chart.html")
Key Features and Customization
This is where pyecharts shines. You have fine-grained control over your charts.
A. Themes
ECharts comes with several built-in themes. You can apply them when instantiating the chart.
from pyecharts.charts import Line
from pyecharts.globals import ThemeType
# Use a dark theme
dark_line_chart = Line(init_opts=opts.InitOpts(theme=ThemeType.DARK))
# ... add data and options as before
dark_line_chart.render("dark_line_chart.html")
Other themes include LIGHT, CHALK, ESSOS, MACARONS, PURPLE_PASSION, etc.
B. Data Zoom
The datazoom component is incredibly useful for large datasets.
from pyecharts.charts import Line
from pyecharts import options as opts
# Generate a lot of data
x_data = list(range(200))
y_data = [i * 1.5 + i % 5 for i in x_data]
line = (
Line()
.add_xaxis(x_data)
.add_yaxis("Big Data Series", y_data)
.set_global_opts(
title_opts=opts.TitleOpts(title="Data Zoom Example"),
# Add data zoom components
datazoom_opts=[
opts.DataZoomOpts(), # A slider at the bottom
opts.DataZoomOpts(type_="inside"), # Zoom controlled by mouse wheel
],
)
)
line.render("data_zoom_chart.html")
C. Exporting Charts
You can export charts as static images. This requires installing snapshot-selenium.
pip install snapshot-selenium
Then, you can use the render function with a different output path.
# ... (chart creation code from the first example)
# Render as a PNG image
line_chart.render("sales_report.png") # The file extension determines the format
Advanced: Combining Charts (Grid Layout)
You can combine multiple charts into a single page using the Grid chart.
from pyecharts.charts import Bar, Line, Grid
from pyecharts import options as opts
# Create two charts
bar = Bar().add_xaxis(["A", "B", "C", "D"]).add_yaxis("Data 1", [10, 20, 30, 40])
line = Line().add_xaxis(["A", "B", "C", "D"]).add_yaxis("Data 2", [35, 25, 45, 15])
# Create a grid and add the charts to it
# The chart_id is crucial for unique identification in the HTML
grid = (
Grid()
.add(bar, grid_opts=opts.GridOpts(pos_left="5%", pos_right="20%"))
.add(line, grid_opts=opts.GridOpts(pos_left="55%", pos_right="20%"))
)
grid.render("grid_layout_chart.html")
Comparison with Other Libraries
| Feature | pyecharts (ECharts) |
Matplotlib / Seaborn |
Plotly |
|---|---|---|---|
| Interactivity | Excellent (built-in) | Poor (requires Bokeh) | Excellent (built-in) |
| JavaScript Backend | Yes | No | Yes |
| 3D Charts | Yes | Yes (limited) | Yes |
| Customization | Very High | High | Very High |
| Python API | Declarative, chain-style | Imperative, stateful | Declarative, chain-style |
| Primary Use Case | Web dashboards, interactive reports | Static plots, publication figures | Interactive web dashboards, scientific plots |
When to use pyecharts:
- When you need highly interactive visualizations for a web page or dashboard.
- When you want the look and feel of modern web charts.
- When you need specific chart types that are well-supported in ECharts (e.g., graph, sunburst, gauge).
When to consider alternatives:
- For simple, static plots for a scientific paper or report,
MatplotlibandSeabornare the standard. - For 3D scientific visualizations,
Mayavimight be more suitable. Plotlyis a very strong competitor and is often chosen for its similar interactivity and Python-friendly API.
Conclusion
pyecharts is an incredibly powerful and flexible tool for Python developers who want to create beautiful, interactive, and professional-looking charts. Its declarative API makes it easy to learn, and its deep customization options allow you to create exactly the visualization you need. For anyone working on data products, web dashboards, or reports that will be viewed in a browser, pyecharts is an excellent choice to have in your toolkit.
