Unlock Data Visualizations with pyecharts: Installation to Advanced Combinations
This comprehensive guide walks you through installing pyecharts, creating basic charts such as bar, line, scatter, boxplot, word cloud, and geographic maps, customizing themes, axes, data zoom, and styles, and mastering chart combinations like Grid, Overlap, Page, and Timeline to build sophisticated visualizations.
pyecharts is a Python interface for the Baidu ECharts JavaScript library, enabling powerful data visualizations. This article explains what pyecharts is, how to install it, and provides step‑by‑step examples of basic charts (bar, line, scatter, boxplot, word cloud, geo, map) with code snippets.
Installation
# Install pyecharts and optional snapshot package
!pip install pyecharts==0.5.11
!pip install pyecharts_snapshotBasic Charts
Bar Chart
from pyecharts import Bar
x = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
y1 = [5, 20, 36, 10, 75, 90]
y2 = [10, 25, 8, 60, 20, 80]
bar = Bar("产品月销量", width=600, height=420)
bar.add("商家A", x, y1)
bar.add("商家B", x, y2, is_xaxis_boundarygap=True)
bar.render('柱形图示范.html')Line Chart
from pyecharts import Line
x = [f"2018-{i:02d}" for i in range(1,13)]
y1 = [5,10,26,30,35,30,20,26,40,46,40,50]
y2 = [8,20,24,36,40,36,40,45,50,53,48,58]
line = Line("月销售总额", width=600, height=420)
line.add("商家A", x, y1)
line.add("商家B", x, y2, yaxis_min=0, yaxis_max=100, is_datazoom_show=True, datazoom_type='both')
line.render('折线图示范.html')Scatter Chart
from pyecharts import Scatter
import pandas as pd
# Sample data for two groups
boy = pd.DataFrame({"weight":[56,67,65,70,57,60,80,85,76,64], "height":[162,170,168,172,168,172,180,176,178,170]})
girl = pd.DataFrame({"weight":[50,62,60,70,57,45,62,65,70,56], "height":[155,162,165,170,166,158,160,170,172,165]})
scatter = Scatter("体格数据", width=600, height=420)
scatter.add("boy", boy['weight'], boy['height'])
scatter.add("girl", girl['weight'], girl['height'], yaxis_min=130, yaxis_max=200, xaxis_min=30, xaxis_max=100)
scatter.render('散点图示范.html')Boxplot
from pyecharts import Boxplot
x = ["1班","2班","3班","4班"]
y1 = [78,98,56,78,90,45,78,20,87,86,74,89,94]
y2 = [89,82,45,67,68,78,79,98,71,56,78,81,80]
y3 = [90,80,60,89,76,73,72,92,89,87,65,66,76]
y4 = [82,72,55,100,90,78,69,67,87,66,78,71,82]
box = Boxplot("考试成绩箱型图", width=600, height=420)
y_prepared = box.prepare_data([y1, y2, y3, y4])
box.add("", x, y_prepared)
box.render('箱型图示范.html')Word Cloud
from pyecharts import WordCloud
words = ["python","jupyter","numpy","pandas","matplotlib","sklearn","xgboost","lightGBM","simpy","keras","tensorflow","hive","hadoop","spark"]
counts = [100,90,65,95,50,60,70,70,20,70,80,80,60,60]
cloud = WordCloud("数据算法常用工具", width=600, height=420)
cloud.add("utils", words, counts, shape="circle", word_size_range=(10,70))
cloud.render('词云图示范.html')Geographic and Map Charts
# Install map packages
!pip install echarts-countries-pypkg
!pip install echarts-china-provinces-pypkg
!pip install echarts-china-cities-pypkg
from pyecharts import Geo, Map
# Geo example (city air quality)
city_data = [("海门",9),("鄂尔多斯",12),("招远",12),...]
geo = Geo("全国部分城市空气质量", width=800, height=600, background_color="#404a59")
attr, value = geo.cast(city_data)
geo.add("", attr, value, visual_range=[0,200], visual_text_color="#fff", symbol_size=15, is_visualmap=True)
geo.render('城市空气质量.html')
# Map example (province data)
attr = ["福建","山东","北京","上海","江西","新疆","内蒙古","云南","重庆"]
value = [155,10,66,78,44,38,88,50,20]
m = Map("全国省份地图", width=600, height=400)
m.add("", attr, value, maptype='china', is_visualmap=True, is_piecewise=True, pieces=[{'max':160,'min':81,'label':'高'},{'max':80,'min':51,'label':'中'},{'max':50,'min':0,'label':'低'}])
m.render('省份地图.html')Chart Configuration
pyecharts supports three levels of configuration: theme style, global attributes, and element‑specific attributes.
1. Theme Style
from pyecharts import Bar
bar = Bar("dark主题展示", "这里是副标题")
bar.use_theme('dark')
# add data as usual2. Global Attributes
from pyecharts import Pie
pie = Pie(title="销售额占比", width=600, height=420, title_pos='center', title_color='#0000ff', background_color='#aee')
pie.add('', ['衬衫','羊毛衫','雪纺衫','裤子','高跟鞋','袜子'], [11,12,13,10,10,10], is_label_show=True)3. Element‑Specific Attributes
# Example: configuring xyAxis and dataZoom for a line chart
from pyecharts import Line
x = [f"2018-{i:02d}" for i in range(1,13)]
y = [5,10,26,30,35,30,20,26,40,46,40,50]
line = Line("月销售总额", width=600, height=420)
line.add("商家A", x, y, yaxis_min=0, yaxis_max=100, xaxis_name='月份', yaxis_name='销售额', xaxis_rotate=30, is_datazoom_show=True, datazoom_type='both')Chart Combination
pyecharts provides four combination methods: Grid, Overlap, Page, and Timeline.
Grid – place multiple charts in a single canvas.
from pyecharts import Bar, Line, Grid
# Bar chart
bar = Bar('柱状图示例', height=720)
bar.add('商家A', x, y1, is_stack=True)
bar.add('商家B', x, y2, is_stack=True)
# Line chart
line = Line('折线图示例', title_top='50%')
line.add('最高气温', x_axis, y1)
line.add('最低气温', x_axis, y2, legend_top='50%')
# Combine
grid = Grid()
grid.add(bar, grid_bottom='60%')
grid.add(line, grid_top='60%')
grid.render('grid_combination.html')Overlap – overlay different chart types on the same axes.
from pyecharts import Bar, Line, Overlap
attr = ['A','B','C','D','E','F']
v1 = [10,20,30,40,50,60]
v2 = [38,28,58,48,78,68]
bar = Bar('Line‑Bar 示例')
bar.add('bar', attr, v1)
line = Line()
line.add('line', attr, v2)
overlap = Overlap()
overlap.add(bar)
overlap.add(line)
overlap.render('overlap.html')Page – sequence multiple charts in a single HTML page.
from pyecharts import Bar, Scatter3D, Page
page = Page()
# Bar chart
bar = Bar('柱状图数据堆叠示例', width=500, height=300)
bar.add('商家A', attr, v1, is_stack=True)
bar.add('商家B', attr, v2, is_stack=True)
page.add(bar)
# 3D Scatter chart
import random
data = [[random.randint(0,100), random.randint(0,100), random.randint(0,100)] for _ in range(80)]
scatter3d = Scatter3D('3D 散点图示例', width=500, height=300)
scatter3d.add('', data, is_visualmap=True)
page.add(scatter3d)
page.render('page.html')Timeline – animate a series of charts over time.
from pyecharts import Bar, Timeline
from random import randint
attr = ['衬衫','羊毛衫','雪纺衫','裤子','高跟鞋','袜子']
# Create yearly bar charts
bars = []
for year in range(2012, 2017):
bar = Bar(f"{year} 年销量", "数据纯属虚构")
for season in ['春季','夏季','秋季','冬季']:
values = [randint(10,100) for _ in range(6)]
bar.add(season, attr, values)
bars.append(bar)
# Build timeline
timeline = Timeline(is_auto_play=True, timeline_play_interval=800)
for bar, year in zip(bars, range(2012, 2017)):
timeline.add(bar, f"{year} 年")
timeline.render('timeline.html')By mastering these installation steps, basic chart types, configuration options, and combination techniques, you can create rich, interactive visualizations for data analysis, reporting, and presentation using pyecharts.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
