Fundamentals 10 min read

Creating Various Chart Types with Pyecharts in Python

This tutorial demonstrates how to generate a wide range of 2D and 3D charts—including line, bar, scatter, pie, map, word cloud, gauge, funnel, tree, parallel, sankey, geo, timeline, and 3D visualizations—using the Python library pyecharts, providing complete code snippets and rendering instructions for each chart type.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Creating Various Chart Types with Pyecharts in Python

This guide shows how to create many common chart types with pyecharts in Python.

Line chart :

from pyecharts.charts import Line
from pyecharts import options as opts
line = (
    Line()
    .add_xaxis(["周一", "周二", "周三", "周四", "周五", "周六", "周日"])
    .add_yaxis("系列1", [120, 200, 150, 80, 70, 110, 130])
    .set_global_opts(title_opts=opts.TitleOpts(title="折线图"))
)
line.render("line.html")

Bar chart :

from pyecharts.charts import Bar
bar = (
    Bar()
    .add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
    .add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
    .set_global_opts(title_opts=opts.TitleOpts(title="柱状图"))
)
bar.render("bar.html")

Scatter chart :

from pyecharts.charts import Scatter
scatter = (
    Scatter()
    .add_xaxis([1, 2, 3, 4, 5])
    .add_yaxis("series1", [10, 20, 30, 40, 50])
    .set_global_opts(title_opts=opts.TitleOpts(title="散点图"))
)
scatter.render("scatter.html")

Pie chart :

from pyecharts.charts import Pie
pie = (
    Pie()
    .add("", [list(z) for z in zip(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"], [5, 20, 36, 10, 75, 90])])
    .set_global_opts(title_opts=opts.TitleOpts(title="饼图"))
    .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c} ({d}%)"))
)
pie.render("pie.html")

Map (China example) :

from pyecharts.charts import Map
map_chart = (
    Map()
    .add("商家A", [list(z) for z in zip(["北京", "上海", "广东"], [55, 60, 70])], "china")
    .set_global_opts(title_opts=opts.TitleOpts(title="地图"), visualmap_opts=opts.VisualMapOpts(max_=200))
)
map_chart.render("map.html")

Word Cloud :

from pyecharts.charts import WordCloud
words = [("Sam S Club", 10000), ("Macys", 6181), ("Amy Schumer", 4386)]
wordcloud = (
    WordCloud()
    .add("", words, word_size_range=[20, 100], shape='circle')
    .set_global_opts(title_opts=opts.TitleOpts(title="词云图"))
)
wordcloud.render("wordcloud.html")

Gauge :

from pyecharts.charts import Gauge
gauge = (
    Gauge()
    .add("", [("完成率", 66.6)])
    .set_global_opts(title_opts=opts.TitleOpts(title="仪表盘"))
)
 gauge.render("gauge.html")

Funnel :

from pyecharts.charts import Funnel
funnel = (
    Funnel()
    .add("商品", [list(z) for z in zip(["展现", "点击", "访问", "咨询", "订单"], [100, 80, 60, 40, 20])])
    .set_global_opts(title_opts=opts.TitleOpts(title="漏斗图"))
)
funnel.render("funnel.html")

Tree :

from pyecharts.charts import Tree
data = [{"name": "树节点", "children": [{"name": "子节点1"}, {"name": "子节点2", "children": [{"name": "孙子节点"}]}]}]
tree = (
    Tree()
    .add("", data)
    .set_global_opts(title_opts=opts.TitleOpts(title="树图"))
)
tree.render("tree.html")

Parallel coordinates :

from pyecharts.charts import Parallel
parallel_data = [("一部", 99, 92, 95, 95), ("二部", 99, 92, 95, 95)]
parallel = (
    Parallel()
    .add_schema([
        opts.ParallelAxisOpts(dim=0, name="部门"),
        opts.ParallelAxisOpts(dim=1, name="综合素质", type_="value"),
        opts.ParallelAxisOpts(dim=2, name="学科1", type_="value"),
        opts.ParallelAxisOpts(dim=3, name="学科2", type_="value"),
        opts.ParallelAxisOpts(dim=4, name="学科3", type_="value"),
    ])
    .add("parallel", parallel_data)
    .set_global_opts(title_opts=opts.TitleOpts(title="平行坐标系图"))
)
parallel.render("parallel.html")

Sankey diagram :

from pyecharts.charts import Sankey
sankey_data = {"nodes": [{"name": "节点1"}, {"name": "节点2"}, {"name": "节点3"}, {"name": "节点4"}], "links": [{"source": "节点1", "target": "节点2", "value": 10}, {"source": "节点2", "target": "节点3", "value": 20}, {"source": "节点3", "target": "节点4", "value": 30}]}
sankey = (
    Sankey()
    .add("sankey", nodes=sankey_data["nodes"], links=sankey_data["links"], linestyle_opt=opts.LineStyleOpts(opacity=0.2, curve=0.5, color="source"), label_opts=opts.LabelOpts(position="right"))
    .set_global_opts(title_opts=opts.TitleOpts(title="桑基图"))
)
sankey.render("sankey.html")

Geo chart :

from pyecharts.charts import Geo
geo = (
    Geo()
    .add_schema(maptype="china")
    .add("geo", [("北京", 55), ("上海", 60), ("广州", 70)])
    .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
    .set_global_opts(visualmap_opts=opts.VisualMapOpts(), title_opts=opts.TitleOpts(title="地理坐标系图"))
)
geo.render("geo.html")

Timeline (bar chart over years) :

from pyecharts.charts import Timeline, Bar
timeline = Timeline()
for i in range(2015, 2020):
    bar = (
        Bar()
        .add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
        .add_yaxis("商家A", [5*i, 20*i, 36*i, 10*i, 75*i, 90*i])
    )
    timeline.add(bar, f"{i}年")

timeline.render("timeline.html")

3D Scatter (Scatter3D) :

from pyecharts.charts import Scatter3D
from pyecharts import options as opts
data = [[1,1,1],[2,2,2],[3,3,3],[4,4,4],[5,5,5]]
scatter3d = (
    Scatter3D()
    .add("", data)
    .set_global_opts(title_opts=opts.TitleOpts(title="3D 散点图"), visualmap_opts=opts.VisualMapOpts(range_color=['#313695','#4575b4','#74add1','#abd9e9','#e0f3f8','#ffffbf','#fee090','#fdae61','#f46d43','#d73027','#a50026']))
)
scatter3d.render("scatter3d.html")

3D Bar (Bar3D) :

from pyecharts.charts import Bar3D
from pyecharts import options as opts
data = [[i, j, i * j % 100] for i in range(20) for j in range(20)]
bar3d = (
    Bar3D()
    .add("", [[d[1], d[0], d[2]] for d in data], xaxis3d_opts=opts.Axis3DOpts(type_="category", data=[str(i) for i in range(20)]), yaxis3d_opts=opts.Axis3DOpts(type_="category", data=[str(i) for i in range(20)]), zaxis3d_opts=opts.Axis3DOpts(type_="value"))
    .set_global_opts(title_opts=opts.TitleOpts(title="3D 柱状图"), visualmap_opts=opts.VisualMapOpts(max_=100, min_=0, range_color=['#313695','#4575b4','#74add1','#abd9e9','#e0f3f8','#ffffbf','#fee090','#fdae61','#f46d43','#d73027','#a50026']))
)
bar3d.render("bar3d.html")

3D Surface (approximated with Bar3D) :

from pyecharts.charts import Bar3D
from pyecharts import options as opts
import numpy as np
x_data = y_data = list(range(-5, 5))
z_data = [[y, x, np.sin(np.sqrt(x**2 + y**2))] for x in x_data for y in y_data]
surface3d = (
    Bar3D()
    .add("", z_data, shading="color", xaxis3d_opts=opts.Axis3DOpts(type_="value"), yaxis3d_opts=opts.Axis3DOpts(type_="value"), zaxis3d_opts=opts.Axis3DOpts(type_="value"))
    .set_global_opts(title_opts=opts.TitleOpts(title="3D 曲面图(近似)"), visualmap_opts=opts.VisualMapOpts(max_=1, min_=-1, range_color=['#313695','#4575b4','#74add1','#abd9e9','#e0f3f8','#ffffbf','#fee090','#fdae61','#f46d43','#d73027','#a50026']))
)
surface3d.render("surface3d.html")

Each code block creates the chart object, configures its data and visual options, and finally renders an HTML file that can be opened in a browser to view the interactive visualization.

PythonTutorialData VisualizationpyechartsChart
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.