当前位置:   article > 正文

python pyecharts数据可视化库_python导入echarts

python导入echarts

首先需要在激活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中显示。

  1. from pyecharts import options as opts
  2. from pyecharts.charts import Bar
  3. from pyecharts.commons.utils import JsCode
  4. from pyecharts.globals import ThemeType
  5. list2 = [
  6. {"value": 12, "percent": 12 / (12 + 3)},
  7. {"value": 23, "percent": 23 / (23 + 21)},
  8. {"value": 33, "percent": 33 / (33 + 5)},
  9. {"value": 3, "percent": 3 / (3 + 52)},
  10. {"value": 33, "percent": 33 / (33 + 43)},
  11. ]
  12. list3 = [
  13. {"value": 3, "percent": 3 / (12 + 3)},
  14. {"value": 21, "percent": 21 / (23 + 21)},
  15. {"value": 5, "percent": 5 / (33 + 5)},
  16. {"value": 52, "percent": 52 / (3 + 52)},
  17. {"value": 43, "percent": 43 / (33 + 43)},
  18. ]
  19. c = (
  20. Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))
  21. .add_xaxis([1, 2, 3, 4, 5])
  22. .add_yaxis("product1", list2, stack="stack1", category_gap="50%")
  23. .add_yaxis("product2", list3, stack="stack1", category_gap="50%")
  24. .set_series_opts(
  25. label_opts=opts.LabelOpts(
  26. position="right",
  27. formatter=JsCode(
  28. "function(x){return Number(x.data.percent * 100).toFixed() + '%';}"
  29. ),
  30. )
  31. )
  32. )
  33. c.render_notebook()

  1. from pyecharts import options as opts
  2. from pyecharts.charts import Bar
  3. from pyecharts.faker import Faker
  4. c = (
  5. Bar()
  6. .add_xaxis(Faker.choose())
  7. .add_yaxis("商家A", Faker.values())
  8. .add_yaxis("商家B", Faker.values())
  9. .set_global_opts(title_opts=opts.TitleOpts(title="Bar-基本示例", subtitle="我是副标题"))
  10. #.render("bar_base.html")
  11. )
  12. c.render_notebook()

 

 

  1. from pyecharts import options as opts
  2. from pyecharts.charts import Pie
  3. from pyecharts.faker import Faker
  4. c = (
  5. Pie()
  6. .add("", [list(z) for z in zip(Faker.choose(), Faker.values())])
  7. .set_global_opts(title_opts=opts.TitleOpts(title="Pie-基本示例"))
  8. .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
  9. #.render("pie_base.html")
  10. )
  11. c.render_notebook()

  1. import pyecharts.options as opts
  2. from pyecharts.charts import Line
  3. from pyecharts.faker import Faker
  4. c = (
  5. Line()
  6. .add_xaxis(Faker.choose())
  7. .add_yaxis("商家A", Faker.values())
  8. .add_yaxis("商家B", Faker.values())
  9. .set_global_opts(title_opts=opts.TitleOpts(title="Line-基本示例"))
  10. #.render("line_base.html")
  11. )
  12. c.render_notebook()

  1. from pyecharts import options as opts
  2. from pyecharts.charts import Tree
  3. data = [
  4. {
  5. "children": [
  6. {"name": "B"},
  7. {
  8. "children": [{"children": [{"name": "I"}], "name": "E"}, {"name": "F"}],
  9. "name": "C",
  10. },
  11. {
  12. "children": [
  13. {"children": [{"name": "J"}, {"name": "K"}], "name": "G"},
  14. {"name": "H"},
  15. ],
  16. "name": "D",
  17. },
  18. ],
  19. "name": "A",
  20. }
  21. ]
  22. c = (
  23. Tree()
  24. .add("", data)
  25. .set_global_opts(title_opts=opts.TitleOpts(title="Tree-基本示例"))
  26. #.render("tree_base.html")
  27. )
  28. c.render_notebook()

  1. from pyecharts import options as opts
  2. from pyecharts.charts import Scatter
  3. from pyecharts.faker import Faker
  4. c = (
  5. Scatter()
  6. .add_xaxis(Faker.choose())
  7. .add_yaxis("商家A", Faker.values())
  8. .set_global_opts(
  9. title_opts=opts.TitleOpts(title="Scatter-显示分割线"),
  10. xaxis_opts=opts.AxisOpts(splitline_opts=opts.SplitLineOpts(is_show=True)),
  11. yaxis_opts=opts.AxisOpts(splitline_opts=opts.SplitLineOpts(is_show=True)),
  12. )
  13. #.render("scatter_splitline.html")
  14. )
  15. c.render_notebook()

 

  1. from pyecharts import options as opts
  2. from pyecharts.charts import Graph
  3. nodes = [
  4. {"name": "结点1", "symbolSize": 10},
  5. {"name": "结点2", "symbolSize": 20},
  6. {"name": "结点3", "symbolSize": 30},
  7. {"name": "结点4", "symbolSize": 40},
  8. {"name": "结点5", "symbolSize": 50},
  9. {"name": "结点6", "symbolSize": 40},
  10. {"name": "结点7", "symbolSize": 30},
  11. {"name": "结点8", "symbolSize": 20},
  12. ]
  13. links = []
  14. for i in nodes:
  15. for j in nodes:
  16. links.append({"source": i.get("name"), "target": j.get("name")})
  17. c = (
  18. Graph()
  19. .add("", nodes, links, repulsion=8000)
  20. .set_global_opts(title_opts=opts.TitleOpts(title="Graph-基本示例"))
  21. #render("graph_base.html")
  22. )
  23. c.render_notebook()

 

  1. import datetime
  2. import random
  3. from pyecharts import options as opts
  4. from pyecharts.charts import Calendar
  5. begin = datetime.date(2017, 1, 1)
  6. end = datetime.date(2017, 12, 31)
  7. data = [
  8. [str(begin + datetime.timedelta(days=i)), random.randint(1000, 25000)]
  9. for i in range((end - begin).days + 1)
  10. ]
  11. c = (
  12. Calendar()
  13. .add("", data, calendar_opts=opts.CalendarOpts(range_="2017"))
  14. .set_global_opts(
  15. title_opts=opts.TitleOpts(title="Calendar-2017年微信步数情况"),
  16. visualmap_opts=opts.VisualMapOpts(
  17. max_=20000,
  18. min_=500,
  19. orient="horizontal",
  20. is_piecewise=True,
  21. pos_top="230px",
  22. pos_left="100px",
  23. ),
  24. )
  25. #.render("calendar_base.html")
  26. )
  27. c.render_notebook()

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/286670
推荐阅读
相关标签
  

闽ICP备14008679号