Echarts是百度开源的数据可视化JS工具,凭借着良好的交互性、精巧的图表设计,得到了很多开发者的青睐。而 Python 是一门富有表达力的语言,很适合用于数据处理。当数据分析遇上数据可视化时,pyecharts 诞生了。pyecharts-gallery画廊的工具,有丰富的示例。使用pip install pyecharts
安装可视化模块。
-
构建基础折线图
# 导入Line构建折线图对象 from pyecharts.charts import Line from pyecharts.options import TitleOpts, LegendOpts, ToolboxOpts, VisualMapOpts line = Line() # 添加x轴、y轴数据 line.add_xaxis(["China", "UK", "USA", "France"]) line.add_yaxis("GDP", [30, 20, 30, 20]) # 配置全局选项 line.set_global_opts( title_opts = TitleOpts(title = 'GDP展示', pos_left='center', pos_bottom='1%'), legend_opts = LegendOpts(is_show=True), toolbox_opts = VisualMapOpts(is_show=True) ) # 生成图表,生成render.html line.render()
-
使用全局配置项设置属性
set_global_opts方法配置全局选项,针对通用的对象设置。
-
TitleOpts:标题配置项
-
LegendOpts:图例配置项
-
ToolboxOpts:工具箱配置项
-
VisualMapOpts:视觉映射配置项
-
TooltipOpts:提示框配置项
-
DataZoomOpts:区域缩放配置项
- 绘制广东地图,模拟数据
from pyecharts import options as opts
from pyecharts.charts import Map
from pyecharts.faker import Faker
c = (
Map()
.add("商家A", [list(z) for z in zip(Faker.guangdong_city, Faker.values())], "广东")
.set_global_opts(
title_opts=opts.TitleOpts(title="Map-广东地图"), visualmap_opts=opts.VisualMapOpts()
)
.render("map_guangdong.html")
)