Python pyecharts

Bar/line/pie charts, rendering to HTML, and embedding in a page.

Beginner
Key Pointspyecharts generates standalone HTML with the ECharts runtime — perfect for static reports, not for real-time dashboards.

Install and First Chart

pyecharts wraps ECharts and outputs a self-contained HTML file — open it directly, no server needed.

Bash
pip install pyecharts
Python
from pyecharts.charts import Bar
from pyecharts import options as opts

bar = (
    Bar()
    .add_xaxis(["Java", "Python", "Go", "Rust"])
    .add_yaxis("教程数", [17, 32, 8, 8])
    .set_global_opts(
        title_opts=opts.TitleOpts(title="各语言教程数量"),
        yaxis_opts=opts.AxisOpts(name="课数"),
    )
)
bar.render("bar.html")
print("ok")
Output
ok

Line and Pie

Lines suit time series; pies only work for part-to-whole with five or fewer slices.

Python
from pyecharts.charts import Line, Pie
from pyecharts import options as opts

line = (
    Line()
    .add_xaxis(["Mon", "Tue", "Wed", "Thu"])
    .add_yaxis("PV", [120, 200, 150, 260], is_smooth=True)
    .set_global_opts(title_opts=opts.TitleOpts(title="Weekly PV"))
)
line.render("line.html")

pie = (
    Pie()
    .add("", [("Java", 40), ("Python", 35), ("Go", 15), ("其他", 10)])
    .set_global_opts(title_opts=opts.TitleOpts(title="语言占比"))
)
pie.render("pie.html")

Embedding

render_embed() returns a <div> plus <script> you can paste into an existing page; render_notebook() is for Jupyter.

Python
html = bar.render_embed()
with open("report.html", "w", encoding="utf-8") as f:
    f.write(f"<html><body>{html}</body></html>")
The generated HTML inlines ~1MB of ECharts JS. For production, point InitOpts at a CDN or export just the option JSON.

Friends & Partners