pyecharts是一款将python与echarts结合的强大的数据可视化工具,官网有学习手册,pyecharts 用于 web 绘图,有较多的绘图种类,且代码量比较少。而Echarts 是百度开源的一个可视化 JavaScript 库。
#可视化2D中国地图
from pyecharts import options as opts
from pyecharts.charts import Map
c=(Map()
.add("中部战区",[['北京',11],['天津',12],['河北',13],['山西',14],['河南',15],['湖北',16],['陕西',17]])
.add("北部战区",[['辽宁',31],['吉林',32],['黑龙江',33],['内蒙古',34],['山东',35]])
.add("东部战区",[['上海',51],['江苏',52],['浙江',53],['安徽',54],['福建',55],['江西',56],['台湾',57]])
.add("南部战区",[['湖南',71],['广东',72],['广西',73],['海南',74],['云南',75],['贵州',76],['香港',77],['澳门',78]])
.add("西部战区",[['四川',91],['重庆',92],['西藏',93],['甘肃',94],['宁夏',95],['青海',96],['新疆',97]])
.set_global_opts(title_opts=opts.TitleOpts(title='china_map'),#左上角title
visualmap_opts=opts.VisualMapOpts(max_=100,is_piecewise=True),#地图颜色最大值是100,热力图是块状说明
legend_opts=opts.LegendOpts(is_show=True))#五个地区分布的块状说明
.render("map_china.html")
)
from pyecharts import options as opts
from pyecharts.charts import Map
country=['China','Russia','USA','France','UK']
value=[10,30,50,70,90]
c=(
Map()
.add('',[list(z) for z in zip(country,value)],'world',is_map_symbol_show=True)#标出这几个点
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))#不显示每个地方的名字
.set_global_opts(title_opts=opts.TitleOpts(title='world map'),visualmap_opts=opts.VisualMapOpts(max_=100))
.render('world map.html')
)
from pyecharts import options as opts
from pyecharts.charts import Map
from pyecharts.charts import MapGlobe
country=['China','Russia','USA','France','UK']
value=[10,30,50,70,90]
c=(
MapGlobe(init_opts=opts.InitOpts(width='1200px',height='800px',page_title='3d_map',bg_color='#00CFFF'))#导入这么大的3D球,颜色天蓝
.add('',[list(z) for z in zip(country,value)],'world',is_map_symbol_show=True)#标出这几个点
.set_series_opts(label_opts=opts.LabelOpts(is_show=True))#不显示每个地方的名字
.set_global_opts(title_opts=opts.TitleOpts(title='3d_world map'),visualmap_opts=opts.VisualMapOpts(max_=100))
.render('3D_map.html')
)
这里我在html中录制了gif图片,用到了screentogif工具。
pyecharts 可以将图片保存为多种格式,但需要插件,否则只能保存为 html 格式。
make_snapshot 用于 pyecharts 直接生成图片。
安装:pip install snapshot-selenium
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.render import make_snapshot
from snapshot_selenium import snapshot
def bar_chart() -> Bar:
c = (
Bar()
.add_xaxis(["衬衫", "毛衣", "领带", "裤子", "风衣", "高跟鞋", "袜子"])
.add_yaxis("商家A", [114, 55, 27, 101, 125, 27, 105])
.add_yaxis("商家B", [57, 134, 137, 129, 145, 60, 49])
.reversal_axis()
.set_series_opts(label_opts=opts.LabelOpts(position="right"))
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-测试渲染图片"))
)
return c
make_snapshot(snapshot, bar_chart().render(), "bar0.png")
安装:pip install snapshot-phantomjs
我在安装时出现了一点儿小问题,大概率没这个问题所以这部分可以跳过,于是就先安装了nodejs(node -v 和npm -v都有版本信息时说明安装成功),然后安装了phantomjs(npm install -g phantomjs-prebuilt或者官网下载phantomjs.exe文件放到对应环境的scripts文件夹下再加入到环境变量path中)和pyecharts-snapshot(pip install pyecharts-snapshot )。
# 使用方式
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.render import make_snapshot
from snapshot_phantomjs import snapshot
def bar_chart() -> Bar:
c = (
Bar()
.add_xaxis(["衬衫", "毛衣", "领带", "裤子", "风衣", "高跟鞋", "袜子"])
.add_yaxis("商家A", [114, 55, 27, 101, 125, 27, 105])
.add_yaxis("商家B", [57, 134, 137, 129, 145, 60, 49])
.reversal_axis()
.set_series_opts(label_opts=opts.LabelOpts(position="right"))
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-测试渲染图片"))
)
return c
make_snapshot(snapshot, bar_chart().render(), "bar0.png")
下载:pip install snapshot-pyppeteer
pyppeteer-install
# 使用方式
from snapshot_pyppeteer import snapshot
from pyecharts.charts import Bar
from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.render import make_snapshot
def bar_base() -> Bar:
c = (
Bar()
.add_xaxis(Faker.choose())
.add_yaxis("商家A", Faker.values())
.add_yaxis("商家B", Faker.values())
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-基本示例", subtitle="我是副标题"))
)
make_snapshot(snapshot, c.render(), "bar.png")
if __name__ == '__main__':
bar_base()
渲染图片需要白底儿黑色内容,如下图用的黑色心型形状做的词云图。
from pyecharts import options as opts
from pyecharts.charts import WordCloud
words = [
("花鸟市场", 1446),("汽车", 928),("视频", 906),("电视", 825),("Lover Boy 88", 514),("动漫", 486),("音乐", 53),("直播", 163),
("广播电台", 86),("戏曲曲艺", 17),("演出票务", 6),("给陌生的你听", 1),("资讯", 1437),("商业财经", 422),("娱乐八卦", 353),
("军事", 331),("科技资讯", 313),("社会时政", 307),("时尚", 43),("网络奇闻", 15),("旅游出行", 438),("景点类型", 957),
("国内游", 927),("远途出行方式", 908),("酒店", 693),("关注景点", 611),("旅游网站偏好", 512),("出国游", 382),("交通票务", 312),
("旅游方式", 187),("旅游主题", 163),("港澳台", 104),("本地周边游", 3),("小卖家", 1331),("全日制学校", 941),("基础教育科目", 585),
]
c = (
WordCloud()
.add("", words, word_size_range=[12, 55], mask_image='C:/Users/dz/Downloads/x.png')#
.set_global_opts(title_opts=opts.TitleOpts(title="WordCloud-自定义图片"))
.render("wordcloud_custom_mask_image.html")
)
import pyecharts.options as opts
from pyecharts.charts import WordCloud
data = [
("生活资源", "999"),("供热管理", "375"),("市容环卫", "355"),("自然资源管理", "355"),("大气污染", "223"),("医疗纠纷", "152"),
("执法监督", "152"),("设备安全", "152"),("政务建设", "152"),("县区、开发区", "152"),("宏观经济", "152"),("教育管理", "112"),
("社会保障", "112"),("文娱市场管理", "72"),("主网原因", "71"),("集中供热", "71"),("客运管理", "71"),
("国有公交(大巴)管理", "71"),("物业资质管理", "21"),("树木管理", "11"),("农村基础设施", "11"),("市政府工作部门(含部门管理机构、直属单位)", "11"),
("燃气管理", "11"),("市容环卫", "11")
]
(
WordCloud()
.add(series_name="热点分析", data_pair=data, word_size_range=[26, 66])
.set_global_opts(title_opts=opts.TitleOpts(title="热点分析", title_textstyle_opts=opts.TextStyleOpts(font_size=23)),
tooltip_opts=opts.TooltipOpts(is_show=True))
.render("basic_wordcloud.html")
)
from pyecharts import options as opts
from pyecharts.charts import WordCloud
from pyecharts.globals import SymbolType
words = [("Sam S Club", 10000),("Macys", 6181),("Amy Schumer", 4386),("Jurassic World", 4055),("Charter Communications", 2467),
("Chick Fil A", 2244),("Planet Fitness", 1868),("Pitch Perfect", 1484),("Express", 1112),("Home", 865),
("Johnny Depp", 847),("Lena Dunham", 582),("Lewis Hamilton", 555),("KXAN", 550),("Mary Ellen Mark", 462),
("Farrah Abraham", 366),("Rita Ora", 360),("Serena Williams", 282),("NCAA baseball tournament", 273),("Point Break", 265),
]
c = (
WordCloud()
.add("", words, word_size_range=[20, 100], shape=SymbolType.ARROW)#SymbolType的选择类型不同,词云图的类型不同
.set_global_opts(title_opts=opts.TitleOpts(title="WordCloud-shape-diamond"))
.render("wordcloud_diamond.html")
)
from pyecharts import options as opts
from pyecharts.charts import WordCloud
words = [
("花鸟市场", 1446),("商业财经", 422),("时尚", 43),("景点类型", 957),("关注景点", 611),("旅游方式", 187),
("旅游主题", 163),("小卖家", 1331),("基础教育科目", 585),("留学", 246),("艺术培训", 194),("IT培训", 87),
("高等教育专业", 63),("体育培训", 23),("金融财经", 1328),("保险", 415),("基金", 211),("P2P", 116),
("贵金属", 98),("信托", 90),("公积金", 40),("典当", 7),("汽车档次", 965),("购车阶段", 461),("新能源汽车", 173),
("汽车维修", 155),("违章查询", 76),("路况查询", 32),("网络购物", 1275),("体育健身", 1234),("办公室健身", 3),
("商务服务", 1201),("知识产权", 32),("摄影", 393),("棋牌桌游", 17),("家电数码", 1111),("办公数码设备", 113)
]
c = (
WordCloud()
.add("",words,word_size_range=[20, 100],textstyle_opts=opts.TextStyleOpts(font_family="cursive"))#设置字体格式
.set_global_opts(title_opts=opts.TitleOpts(title="WordCloud-自定义文字样式"))
.render("wordcloud_custom_font_style.html")
)
from pyecharts.components import Image
from pyecharts.options import ComponentTitleOpts
image = Image()
img_src = ("C:/Users/dz/Downloads/Jennifer.jpg")
image.add(src=img_src,style_opts={"width": "200px", "height": "200px", "style": "margin-top: 20px"})
image.set_global_opts(title_opts=ComponentTitleOpts(title="Image-基本示例", subtitle="我是副标题支持换行哦"))
image.render("image_base.html")
参考文献
【1】https://blog.csdn.net/lhas_dfghjkl/article/details/119926737?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522166952793216782425610534%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=166952793216782425610534&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allfirst_rank_ecpm_v1~rank_v31_ecpm-11-119926737-null-null.142v66control,201v3add_ask,213v2t3_esquery_v2&utm_term=python%E7%94%BB%E6%95%B0%E5%AD%97%E5%9C%B0%E7%90%83&spm=1018.2226.3001.4187