赞
踩
首先需要在激活anaconda环境,并在anaconda的python环境中安装pyecharts数据可视化库。
pip install pyecharts
pyecharts官网:pyecharts - A Python Echarts Plotting Library built with love.
pyecharts示例代码:Document
注意将.render("xxx.html")注释,改成在最后调用c.render_notebook(),这样才能在jupyter中显示。
- from pyecharts import options as opts
- from pyecharts.charts import Bar
- from pyecharts.commons.utils import JsCode
- from pyecharts.globals import ThemeType
-
- list2 = [
- {"value": 12, "percent": 12 / (12 + 3)},
- {"value": 23, "percent": 23 / (23 + 21)},
- {"value": 33, "percent": 33 / (33 + 5)},
- {"value": 3, "percent": 3 / (3 + 52)},
- {"value": 33, "percent": 33 / (33 + 43)},
- ]
-
- list3 = [
- {"value": 3, "percent": 3 / (12 + 3)},
- {"value": 21, "percent": 21 / (23 + 21)},
- {"value": 5, "percent": 5 / (33 + 5)},
- {"value": 52, "percent": 52 / (3 + 52)},
- {"value": 43, "percent": 43 / (33 + 43)},
- ]
-
- c = (
- Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))
- .add_xaxis([1, 2, 3, 4, 5])
- .add_yaxis("product1", list2, stack="stack1", category_gap="50%")
- .add_yaxis("product2", list3, stack="stack1", category_gap="50%")
- .set_series_opts(
- label_opts=opts.LabelOpts(
- position="right",
- formatter=JsCode(
- "function(x){return Number(x.data.percent * 100).toFixed() + '%';}"
- ),
- )
- )
-
- )
- c.render_notebook()
- 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")
- )
- c.render_notebook()
- 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")
- )
- c.render_notebook()
- import pyecharts.options as opts
- from pyecharts.charts import Line
- from pyecharts.faker import Faker
-
- c = (
- Line()
- .add_xaxis(Faker.choose())
- .add_yaxis("商家A", Faker.values())
- .add_yaxis("商家B", Faker.values())
- .set_global_opts(title_opts=opts.TitleOpts(title="Line-基本示例"))
- #.render("line_base.html")
- )
- c.render_notebook()
- from pyecharts import options as opts
- from pyecharts.charts import Tree
-
-
- data = [
- {
- "children": [
- {"name": "B"},
- {
- "children": [{"children": [{"name": "I"}], "name": "E"}, {"name": "F"}],
- "name": "C",
- },
- {
- "children": [
- {"children": [{"name": "J"}, {"name": "K"}], "name": "G"},
- {"name": "H"},
- ],
- "name": "D",
- },
- ],
- "name": "A",
- }
- ]
- c = (
- Tree()
- .add("", data)
- .set_global_opts(title_opts=opts.TitleOpts(title="Tree-基本示例"))
- #.render("tree_base.html")
- )
- c.render_notebook()
- 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())
- .set_global_opts(
- title_opts=opts.TitleOpts(title="Scatter-显示分割线"),
- xaxis_opts=opts.AxisOpts(splitline_opts=opts.SplitLineOpts(is_show=True)),
- yaxis_opts=opts.AxisOpts(splitline_opts=opts.SplitLineOpts(is_show=True)),
- )
- #.render("scatter_splitline.html")
- )
- c.render_notebook()
- from pyecharts import options as opts
- from pyecharts.charts import Graph
-
- nodes = [
- {"name": "结点1", "symbolSize": 10},
- {"name": "结点2", "symbolSize": 20},
- {"name": "结点3", "symbolSize": 30},
- {"name": "结点4", "symbolSize": 40},
- {"name": "结点5", "symbolSize": 50},
- {"name": "结点6", "symbolSize": 40},
- {"name": "结点7", "symbolSize": 30},
- {"name": "结点8", "symbolSize": 20},
- ]
- links = []
- for i in nodes:
- for j in nodes:
- links.append({"source": i.get("name"), "target": j.get("name")})
- c = (
- Graph()
- .add("", nodes, links, repulsion=8000)
- .set_global_opts(title_opts=opts.TitleOpts(title="Graph-基本示例"))
- #render("graph_base.html")
- )
- c.render_notebook()
- import datetime
- import random
-
- from pyecharts import options as opts
- from pyecharts.charts import Calendar
-
-
- begin = datetime.date(2017, 1, 1)
- end = datetime.date(2017, 12, 31)
- data = [
- [str(begin + datetime.timedelta(days=i)), random.randint(1000, 25000)]
- for i in range((end - begin).days + 1)
- ]
-
- c = (
- Calendar()
- .add("", data, calendar_opts=opts.CalendarOpts(range_="2017"))
- .set_global_opts(
- title_opts=opts.TitleOpts(title="Calendar-2017年微信步数情况"),
- visualmap_opts=opts.VisualMapOpts(
- max_=20000,
- min_=500,
- orient="horizontal",
- is_piecewise=True,
- pos_top="230px",
- pos_left="100px",
- ),
- )
- #.render("calendar_base.html")
- )
- c.render_notebook()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。