大家好,我是 【Python当打之年(点击跳转)】
本期是《Pyecharts绘图教程》 的第 1 期 ,该系列从0到1不断进阶深入,专门 针对零基础和需要进阶提升的小伙伴,逐步掌握使用Pyecharts库进行数据可视化的技能 ,希望对大家有所帮助,如有疑问或者需要改进的地方可以联系小编。
Echarts
是一个由百度开源的数据可视化,凭借着良好的交互性,精巧的图表设计,得到了众多开发者的认可。而 Python 是一门富有表达力的语言,很适合用于数据处理。当数据分析遇上数据可视化时,Pyecharts 诞生了。
可以理解为 Pyecharts 是实现 Echarts 与 Python 对接的一个库。
Echarts官网
:https://echarts.apache.org/zh/index.html
Pyecharts一共有三个大版本:V0.X 、 V1.X 、V2.X
,版本之间差别比较大!
本系列用的 Pyecharts 版本是1.9.0 !
本系列用的 Pyecharts 版本是1.9.0 !
本系列用的 Pyecharts 版本是1.9.0 !
谨记:不要装错啦!不同版本有些图参数设置不一样!
直接 pip 安装:
pip install pyecharts==1.9.0
查看已安装版本:
import pyecharts
print(pyecharts.__version__)
pip install xxx(库名)
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ xxx(库名)
常用的国内镜像源:
https://pypi.tuna.tsinghua.edu.cn/simple/
https://mirrors.huaweicloud.com/
http://mirrors.aliyun.com/pypi/simple/
http://pypi.douban.com/simple/
http://pypi.mirrors.ustc.edu.cn/simple/
http://mirrors.cloud.tencent.com/pypi/simple
http://mirrors.163.com/
http://mirrors.sohu.com/
后期涉及到地图部分的可视化,所以这里就把地图包先装上(如果自己的可视化不包含地图,可以先不装,需要的时候再装也可以)
echarts-countries-pypkg
echarts-china-provinces-pypkg
echarts-china-cities-pypkg
pip install pyecharts
pip install echarts-countries-pypkg
pip install echarts-china-provinces-pypkg
pip install echarts-china-cities-pypkg
# 将图形渲染到 notebook
def render_notebook()
# 渲染图表到 HTML 文件
def render(
# 生成图片路径
path: str = "render.html",
# 模板路径
template_name: str = "simple_chart.html",
# jinja2.Environment 类实例,可以配置各类环境参数
env: Optional[Environment] = None,
)
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.faker import Faker
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="我是副标题"))
.render("bar_base.html")
)
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.commons.utils import JsCode
from pyecharts.faker import Faker
c = (
Bar()
.add_xaxis(Faker.choose())
.add_yaxis("商家A", Faker.values(), category_gap="60%")
.set_series_opts(
itemstyle_opts={
"normal": {
"color": JsCode(
"""new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
offset: 0,
color: 'rgba(0, 244, 255, 1)'
}, {
offset: 1,
color: 'rgba(0, 77, 167, 1)'
}], false)"""
),
"barBorderRadius": [30, 30, 30, 30],
"shadowColor": "rgb(0, 160, 221)",
}
}
)
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-渐变圆柱"))
.render("bar_border_radius.html")
)
import pyecharts.options as opts
from pyecharts.charts import Line
from pyecharts.commons.utils import JsCode
js_formatter = """function (params) {
console.log(params);
return '降水量 ' + params.value + (params.seriesData.length ? ':' + params.seriesData[0].data : '');
}"""
(
Line()
.add_xaxis(
xaxis_data=[
"2016-1",
"2016-2",
"2016-3",
"2016-4",
"2016-5",
"2016-6",
"2016-7",
"2016-8",
"2016-9",
"2016-10",
"2016-11",
"2016-12",
]
)
.extend_axis(
xaxis_data=[
"2015-1",
"2015-2",
"2015-3",
"2015-4",
"2015-5",
"2015-6",
"2015-7",
"2015-8",
"2015-9",
"2015-10",
"2015-11",
"2015-12",
],
xaxis=opts.AxisOpts(
type_="category",
axistick_opts=opts.AxisTickOpts(is_align_with_label=True),
axisline_opts=opts.AxisLineOpts(
is_on_zero=False, linestyle_opts=opts.LineStyleOpts(color="#6e9ef1")
),
axispointer_opts=opts.AxisPointerOpts(
is_show=True, label=opts.LabelOpts(formatter=JsCode(js_formatter))
),
),
)
.add_yaxis(
series_name="2015 降水量",
is_smooth=True,
symbol="emptyCircle",
is_symbol_show=False,
# xaxis_index=1,
color="#d14a61",
y_axis=[2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3],
label_opts=opts.LabelOpts(is_show=False),
linestyle_opts=opts.LineStyleOpts(width=2),
)
.add_yaxis(
series_name="2016 降水量",
is_smooth=True,
symbol="emptyCircle",
is_symbol_show=False,
color="#6e9ef1",
y_axis=[3.9, 5.9, 11.1, 18.7, 48.3, 69.2, 231.6, 46.6, 55.4, 18.4, 10.3, 0.7],
label_opts=opts.LabelOpts(is_show=False),
linestyle_opts=opts.LineStyleOpts(width=2),
)
.set_global_opts(
legend_opts=opts.LegendOpts(),
tooltip_opts=opts.TooltipOpts(trigger="none", axis_pointer_type="cross"),
xaxis_opts=opts.AxisOpts(
type_="category",
axistick_opts=opts.AxisTickOpts(is_align_with_label=True),
axisline_opts=opts.AxisLineOpts(
is_on_zero=False, linestyle_opts=opts.LineStyleOpts(color="#d14a61")
),
axispointer_opts=opts.AxisPointerOpts(
is_show=True, label=opts.LabelOpts(formatter=JsCode(js_formatter))
),
),
yaxis_opts=opts.AxisOpts(
type_="value",
splitline_opts=opts.SplitLineOpts(
is_show=True, linestyle_opts=opts.LineStyleOpts(opacity=1)
),
),
)
.render("multiple_x_axes.html")
)
from pyecharts import options as opts
from pyecharts.charts import Pie
from pyecharts.faker import Faker
c = (
Pie()
.add("", [list(z) for z in zip(Faker.choose(), Faker.values())])
.set_global_opts(title_opts=opts.TitleOpts(title="Pie-基本示例"))
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
.render("pie_base.html")
)
from pyecharts import options as opts
from pyecharts.charts import Scatter
from pyecharts.faker import Faker
c = (
Scatter()
.add_xaxis(Faker.choose())
.add_yaxis("商家A", Faker.values())
.add_yaxis("商家B", Faker.values())
.set_global_opts(
title_opts=opts.TitleOpts(title="Scatter-VisualMap(Size)"),
visualmap_opts=opts.VisualMapOpts(type_="size", max_=150, min_=20),
)
.render("scatter_visualmap_size.html")
)
from pyecharts import options as opts
from pyecharts.charts import Kline
data = [
[2320.26, 2320.26, 2287.3, 2362.94],
[2300, 2291.3, 2288.26, 2308.38],
[2295.35, 2346.5, 2295.35, 2345.92],
[2347.22, 2358.98, 2337.35, 2363.8],
[2360.75, 2382.48, 2347.89, 2383.76],
[2383.43, 2385.42, 2371.23, 2391.82],
[2377.41, 2419.02, 2369.57, 2421.15],
[2425.92, 2428.15, 2417.58, 2440.38],
[2411, 2433.13, 2403.3, 2437.42],
[2432.68, 2334.48, 2427.7, 2441.73],
[2430.69, 2418.53, 2394.22, 2433.89],
[2416.62, 2432.4, 2414.4, 2443.03],
[2441.91, 2421.56, 2418.43, 2444.8],
[2420.26, 2382.91, 2373.53, 2427.07],
[2383.49, 2397.18, 2370.61, 2397.94],
[2378.82, 2325.95, 2309.17, 2378.82],
[2322.94, 2314.16, 2308.76, 2330.88],
[2320.62, 2325.82, 2315.01, 2338.78],
[2313.74, 2293.34, 2289.89, 2340.71],
[2297.77, 2313.22, 2292.03, 2324.63],
[2322.32, 2365.59, 2308.92, 2366.16],
[2364.54, 2359.51, 2330.86, 2369.65],
[2332.08, 2273.4, 2259.25, 2333.54],
[2274.81, 2326.31, 2270.1, 2328.14],
[2333.61, 2347.18, 2321.6, 2351.44],
[2340.44, 2324.29, 2304.27, 2352.02],
[2326.42, 2318.61, 2314.59, 2333.67],
[2314.68, 2310.59, 2296.58, 2320.96],
[2309.16, 2286.6, 2264.83, 2333.29],
[2282.17, 2263.97, 2253.25, 2286.33],
[2255.77, 2270.28, 2253.31, 2276.22],
]
c = (
Kline()
.add_xaxis(["2017/7/{}".format(i + 1) for i in range(31)])
.add_yaxis("kline", data)
.set_global_opts(
yaxis_opts=opts.AxisOpts(is_scale=True),
xaxis_opts=opts.AxisOpts(is_scale=True),
title_opts=opts.TitleOpts(title="Kline-基本示例"),
)
.render("kline_base.html")
)
from pyecharts import options as opts
from pyecharts.charts import Gauge
c = (
Gauge()
.add("", [("完成率", 66.6)])
.set_global_opts(title_opts=opts.TitleOpts(title="Gauge-基本示例"))
.render("gauge_base.html")
)
import json
from pyecharts import options as opts
from pyecharts.charts import PictorialBar
location = ["山西", "四川", "西藏", "北京", "上海", "内蒙古", "云南", "黑龙江", "广东", "福建"]
values = [13, 42, 67, 81, 86, 94, 166, 220, 249, 262]
with open("symbol.json", "r", encoding="utf-8") as f:
symbols = json.load(f)
c = (
PictorialBar()
.add_xaxis(["reindeer", "ship", "plane", "train", "car"])
.add_yaxis(
"2015",
[
{"value": 157, "symbol": symbols["reindeer"]},
{"value": 21, "symbol": symbols["ship"]},
{"value": 66, "symbol": symbols["plane"]},
{"value": 78, "symbol": symbols["train"]},
{"value": 123, "symbol": symbols["car"]},
],
label_opts=opts.LabelOpts(is_show=False),
symbol_size=22,
symbol_repeat="fixed",
symbol_offset=[0, 5],
is_symbol_clip=True,
)
.add_yaxis(
"2016",
[
{"value": 184, "symbol": symbols["reindeer"]},
{"value": 29, "symbol": symbols["ship"]},
{"value": 73, "symbol": symbols["plane"]},
{"value": 91, "symbol": symbols["train"]},
{"value": 95, "symbol": symbols["car"]},
],
label_opts=opts.LabelOpts(is_show=False),
symbol_size=22,
symbol_repeat="fixed",
symbol_offset=[0, -25],
is_symbol_clip=True,
)
.reversal_axis()
.set_global_opts(
title_opts=opts.TitleOpts(title="PictorialBar-Vehicles in X City"),
xaxis_opts=opts.AxisOpts(is_show=False),
yaxis_opts=opts.AxisOpts(
axistick_opts=opts.AxisTickOpts(is_show=False),
axisline_opts=opts.AxisLineOpts(
linestyle_opts=opts.LineStyleOpts(opacity=0)
),
),
)
.render("pictorialbar_multi_custom_symbols.html")
)
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),
("考试培训", 473),
("语言学习", 358),
("留学", 246),
("K12课程培训", 207),
("艺术培训", 194),
("技能培训", 104),
("IT培训", 87),
("高等教育专业", 63),
("家教", 48),
("体育培训", 23),
("职场培训", 5),
("金融财经", 1328),
("银行", 765),
("股票", 452),
("保险", 415),
("贷款", 253),
("基金", 211),
("信用卡", 180),
("外汇", 138),
("P2P", 116),
("贵金属", 98),
("债券", 93),
("网络理财", 92),
("信托", 90),
("征信", 76),
("期货", 76),
("公积金", 40),
("银行理财", 36),
("银行业务", 30),
("典当", 7),
("海外置业", 1),
("汽车", 1309),
("汽车档次", 965),
("汽车品牌", 900),
("汽车车型", 727),
("购车阶段", 461),
("二手车", 309),
("汽车美容", 260),
("新能源汽车", 173),
("汽车维修", 155),
("租车服务", 136),
("车展", 121),
("违章查询", 76),
("汽车改装", 62),
("汽车用品", 37),
("路况查询", 32),
("汽车保险", 28),
("陪驾代驾", 4),
("网络购物", 1275),
("做我的猫", 1088),
("只想要你知道", 907),
("团购", 837),
("比价", 201),
("海淘", 195),
("移动APP购物", 179),
("支付方式", 119),
("代购", 43),
("体育健身", 1234),
("体育赛事项目", 802),
("运动项目", 405),
("体育类赛事", 337),
("健身项目", 199),
("健身房健身", 78),
("运动健身", 77),
("家庭健身", 36),
("健身器械", 29),
("办公室健身", 3),
("商务服务", 1201),
("法律咨询", 508),
("化工材料", 147),
("广告服务", 125),
("会计审计", 115),
("人员招聘", 101),
("印刷打印", 66),
("知识产权", 32),
("翻译", 22),
("安全安保", 9),
("公关服务", 8),
("商旅服务", 2),
("展会服务", 2),
("特许经营", 1),
("休闲爱好", 1169),
("收藏", 412),
("摄影", 393),
("温泉", 230),
("博彩彩票", 211),
("美术", 207),
("书法", 139),
("DIY手工", 75),
("舞蹈", 23),
("钓鱼", 21),
("棋牌桌游", 17),
("KTV", 6),
("密室", 5),
("采摘", 4),
("电玩", 1),
("真人CS", 1),
("轰趴", 1),
("家电数码", 1111),
("手机", 885),
("电脑", 543),
("大家电", 321),
("家电关注品牌", 253),
("网络设备", 162),
("摄影器材", 149),
("影音设备", 133),
("办公数码设备", 113),
("生活电器", 67),
("厨房电器", 54),
("智能设备", 45),
("个人护理电器", 22),
("服饰鞋包", 1047),
("服装", 566),
("饰品", 289),
("鞋", 184),
("箱包", 168),
("奢侈品", 137),
("母婴亲子", 1041),
("孕婴保健", 505),
("母婴社区", 299),
("早教", 103),
("奶粉辅食", 66),
("童车童床", 41),
("关注品牌", 271),
("宝宝玩乐", 30),
("母婴护理服务", 25),
("纸尿裤湿巾", 16),
("妈妈用品", 15),
("宝宝起名", 12),
("童装童鞋", 9),
("胎教", 8),
("宝宝安全", 1),
("宝宝洗护用品", 1),
("软件应用", 1018),
("系统工具", 896),
("理财购物", 440),
("生活实用", 365),
("影音图像", 256),
("社交通讯", 214),
("手机美化", 39),
("办公学习", 28),
("应用市场", 23),
("母婴育儿", 14),
("游戏", 946),
("手机游戏", 565),
("PC游戏", 353),
("网页游戏", 254),
("游戏机", 188),
("模拟辅助", 166),
("个护美容", 942),
("护肤品", 177),
("彩妆", 133),
("美发", 80),
("香水", 50),
("个人护理", 46),
("美甲", 26),
("SPA美体", 21),
("花鸟萌宠", 914),
("绿植花卉", 311),
("狗", 257),
("其他宠物", 131),
("水族", 125),
("猫", 122),
("动物", 81),
("鸟", 67),
("宠物用品", 41),
("宠物服务", 26),
("书籍阅读", 913),
("网络小说", 483),
("关注书籍", 128),
("文学", 105),
("报刊杂志", 77),
("人文社科", 22),
("建材家居", 907),
("装修建材", 644),
("家具", 273),
("家居风格", 187),
("家居家装关注品牌", 140),
("家纺", 107),
("厨具", 47),
("灯具", 43),
("家居饰品", 29),
("家居日常用品", 10),
("生活服务", 883),
("物流配送", 536),
("家政服务", 108),
("摄影服务", 49),
("搬家服务", 38),
("物业维修", 37),
("婚庆服务", 24),
("二手回收", 24),
("鲜花配送", 3),
("维修服务", 3),
("殡葬服务", 1),
("求职创业", 874),
("创业", 363),
("目标职位", 162),
("目标行业", 50),
("兼职", 21),
("期望年薪", 20),
("实习", 16),
("雇主类型", 10),
("星座运势", 789),
("星座", 316),
("算命", 303),
("解梦", 196),
("风水", 93),
("面相分析", 47),
("手相", 32),
("公益", 90),
]
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 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.country, Faker.values())], "world")
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(
title_opts=opts.TitleOpts(title="Map-世界地图"),
visualmap_opts=opts.VisualMapOpts(max_=200),
)
.render("map_world.html")
)
点击跳转:【全部可视化项目源码+数据】
以上就是本期为大家整理的全部内容了,赶快练习起来吧,原创不易,喜欢的朋友可以点赞、收藏也可以分享(注明出处)让更多人知道。