当前位置:   article > 正文

python可视化神器——pyecharts(词云图&雷达图&极坐标系)_pyechart词云图

pyechart词云图

与基本的Matplotlib绘图相比,pyecharts的交互以及可视化更为友好,更适合于项目开发、商业报告。

github.com/pyecharts/pyecharts

pyecharts 分为 v0.5.X 和 v1 两个大版本,v0.5.X 和 v1 间不兼容,v1 是一个全新的版本,很多函数的用法出现了变更

pip install pyecharts

第一个pyecharts程序

  • bar.add_x/yaxis() 添加横/纵坐标

  • bar.render() 存储文件,默认html文件

  1. #V1
  2. from pyecharts.charts import Bar
  3. bar = Bar()
  4. bar.add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
  5. bar.add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
  6. # render 会生成本地 HTML 文件,默认会在当前目录生成 render.html 文件
  7. # 也可以传入路径参数,如 bar.render("mycharts.html")
  8. bar.render()
  9. # render_notebook 则在notebook页面中直接显示
  10. bar.render_notebook()

常用图表绘制

v1就是正常的anaconda的python

v0.5的是pyecharts-v05的python环境     pyecharts==0.1.9.4

v1的样例:

https://github.com/pyecharts/pyecharts/tree/master/example

图云

  1. #V1
  2. from pyecharts import options as opts
  3. from pyecharts.charts import Page, WordCloud
  4. from pyecharts.globals import SymbolType
  5. words = [
  6. ("花鸟市场", 1446),("汽车", 928),("视频", 906),("电视", 825),("家居饰品", 29),("家居日常用品", 10),("生活服务", 883),("物流配送", 536),("家政服务", 108),("摄影服务", 49),("搬家服务", 38),("物业维修", 37),("婚庆服务", 24),("二手回收", 24),("鲜花配送", 3),("维修服务", 3)
  7. ]
  8. wordCloud = WordCloud()
  9. wordCloud.add("", words, word_size_range=[20, 100])
  10. wordCloud.render()

word_size_range是设置字体大小

  1. #v0.5
  2. from pyecharts import WordCloud
  3. name = ['Sam S Club', 'Macys', 'Amy Schumer', 'Jurassic World', 'Charter Communications',
  4. 'Chick Fil A', 'Planet Fitness', 'Pitch Perfect', 'Express', 'Home', 'Johnny Depp',
  5. 'Lena Dunham', 'Lewis Hamilton', 'KXAN', 'Mary Ellen Mark', 'Farrah Abraham',
  6. 'Rita Ora', 'Serena Williams', 'NCAA baseball tournament', 'Point Break']
  7. value = [10000, 6181, 4386, 4055, 2467, 2244, 1898, 1484, 1112, 965, 847, 582, 555,
  8. 550, 462, 366, 360, 282, 273, 265]
  9. wordcloud = WordCloud(width=1300, height=620)
  10. wordcloud.add("", name, value, word_size_range=[20, 100])
  11. wordcloud.show_config()
  12. wordcloud.render()

水滴球

  1. #v0.5
  2. from pyecharts import Liquid
  3. liquid = Liquid("水球图示例")
  4. liquid.add("Liquid", [0.6, 0.5, 0.4, 0.3], is_liquid_animation=False, shape='diamond')
  5. #liquid.show_config()
  6. liquid.render()

  1. #v0.5
  2. from pyecharts import Liquid
  3. liquid = Liquid("水球图示例")
  4. liquid.add("Liquid", [0.6])
  5. liquid.show_config()
  6. liquid.render()

这个是动态的,上面那个是静态的

  1. #v1
  2. from pyecharts import options as opts
  3. from pyecharts.charts import Grid, Liquid, Page
  4. from pyecharts.commons.utils import JsCode
  5. from pyecharts.faker import Collector
  6. from pyecharts.globals import SymbolType
  7. C = Collector()
  8. @C.funcs
  9. def liquid_base() -> Liquid:
  10. c = (
  11. Liquid()
  12. .add("lq", [0.6, 0.7])
  13. .set_global_opts(title_opts=opts.TitleOpts(title="Liquid-基本示例"))
  14. )
  15. return c
  16. @C.funcs
  17. def liquid_data_precision() -> Liquid:
  18. c = (
  19. Liquid()
  20. .add(
  21. "lq",
  22. [0.3254],
  23. label_opts=opts.LabelOpts(
  24. font_size=50,
  25. formatter=JsCode(
  26. """function (param) {
  27. return (Math.floor(param.value * 10000) / 100) + '%';
  28. }"""
  29. ),
  30. position="inside",
  31. ),
  32. )
  33. .set_global_opts(title_opts=opts.TitleOpts(title="Liquid-数据精度"))
  34. )
  35. return c
  36. @C.funcs
  37. def liquid_without_outline() -> Liquid:
  38. c = (
  39. Liquid()
  40. .add("lq", [0.6, 0.7, 0.8], is_outline_show=False)
  41. .set_global_opts(title_opts=opts.TitleOpts(title="Liquid-无边框"))
  42. )
  43. return c
  44. @C.funcs
  45. def liquid_shape_diamond() -> Liquid:
  46. c = (
  47. Liquid()
  48. .add("lq", [0.4, 0.7], is_outline_show=False, shape=SymbolType.DIAMOND)
  49. .set_global_opts(title_opts=opts.TitleOpts(title="Liquid-Shape-diamond"))
  50. )
  51. return c
  52. @C.funcs
  53. def liquid_shape_arrow() -> Liquid:
  54. c = (
  55. Liquid()
  56. .add("lq", [0.3, 0.7], is_outline_show=False, shape=SymbolType.ARROW)
  57. .set_global_opts(title_opts=opts.TitleOpts(title="Liquid-Shape-arrow"))
  58. )
  59. return c
  60. @C.funcs
  61. def liquid_shape_rect() -> Liquid:
  62. c = (
  63. Liquid()
  64. .add("lq", [0.3, 0.7], is_outline_show=False, shape=SymbolType.RECT)
  65. .set_global_opts(title_opts=opts.TitleOpts(title="Liquid-Shape-rect"))
  66. )
  67. return c
  68. @C.funcs
  69. def multiple_liquid() -> Grid:
  70. l1 = (
  71. Liquid()
  72. .add("lq", [0.6, 0.7], center=["60%", "50%"])
  73. .set_global_opts(title_opts=opts.TitleOpts(title="多个 Liquid 显示"))
  74. )
  75. l2 = Liquid().add(
  76. "lq",
  77. [0.3254],
  78. center=["25%", "50%"],
  79. label_opts=opts.LabelOpts(
  80. font_size=50,
  81. formatter=JsCode(
  82. """function (param) {
  83. return (Math.floor(param.value * 10000) / 100) + '%';
  84. }"""
  85. ),
  86. position="inside",
  87. ),
  88. )
  89. grid = Grid().add(l1, grid_opts=opts.GridOpts()).add(l2, grid_opts=opts.GridOpts())
  90. return grid
  91. Page().add(*[fn() for fn, _ in C.charts]).render()

仪表盘

  1. #v0.5
  2. from pyecharts import Gauge
  3. gauge = Gauge("仪表盘示例")
  4. gauge.add("业务指标", "完成率", 66.66)
  5. gauge.show_config()
  6. gauge.render()

  1. from pyecharts import options as opts
  2. from pyecharts.charts import Gauge, Page
  3. from pyecharts.faker import Collector
  4. C = Collector()
  5. @C.funcs
  6. def gauge_base() -> Gauge:
  7. c = (
  8. Gauge()
  9. .add("", [("完成率", 66.6)])
  10. .set_global_opts(title_opts=opts.TitleOpts(title="Gauge-基本示例"))
  11. )
  12. return c
  13. @C.funcs
  14. def gauge_color() -> Gauge:
  15. c = (
  16. Gauge()
  17. .add(
  18. "业务指标",
  19. [("完成率", 55.5)],
  20. axisline_opts=opts.AxisLineOpts(
  21. linestyle_opts=opts.LineStyleOpts(
  22. color=[(0.3, "#67e0e3"), (0.7, "#37a2da"), (1, "#fd666d")], width=30
  23. )
  24. ),
  25. )
  26. .set_global_opts(
  27. title_opts=opts.TitleOpts(title="Gauge-不同颜色"),
  28. legend_opts=opts.LegendOpts(is_show=False),
  29. )
  30. )
  31. return c
  32. @C.funcs
  33. def gauge_splitnum_label() -> Gauge:
  34. c = (
  35. Gauge()
  36. .add(
  37. "业务指标",
  38. [("完成率", 55.5)],
  39. split_number=5,
  40. axisline_opts=opts.AxisLineOpts(
  41. linestyle_opts=opts.LineStyleOpts(
  42. color=[(0.3, "#67e0e3"), (0.7, "#37a2da"), (1, "#fd666d")], width=30
  43. )
  44. ),
  45. detail_label_opts=opts.LabelOpts(formatter="{value}"),
  46. )
  47. .set_global_opts(
  48. title_opts=opts.TitleOpts(title="Gauge-分割段数-Label"),
  49. legend_opts=opts.LegendOpts(is_show=False),
  50. )
  51. )
  52. return c
  53. @C.funcs
  54. def gauge_label_title_setting() -> Gauge:
  55. c = (
  56. Gauge()
  57. .add(
  58. "",
  59. [("完成率", 66.6)],
  60. title_label_opts=opts.LabelOpts(
  61. font_size=40, color="blue", font_family="Microsoft YaHei"
  62. ),
  63. )
  64. .set_global_opts(title_opts=opts.TitleOpts(title="Gauge-改变轮盘内的字体"))
  65. )
  66. return c
  67. @C.funcs
  68. def gauge_change_radius() -> Gauge:
  69. c = (
  70. Gauge()
  71. .add("", [("完成率", 66.6)], radius="50%")
  72. .set_global_opts(title_opts=opts.TitleOpts(title="Gauge-修改 Radius 为 50%"))
  73. )
  74. return c
  75. Page().add(*[fn() for fn, _ in C.charts]).render()

雷达图

  1. #v0.5
  2. from pyecharts import Radar
  3. schema = [
  4. ("销售", 6500), ("管理", 16000), ("信息技术", 30000), ("客服", 38000), ("研发", 52000), ("市场", 25000)]
  5. v1 = [[4300, 10000, 28000, 35000, 50000, 19000]]
  6. v2 = [[5000, 14000, 28000, 31000, 42000, 21000]]
  7. radar = Radar()
  8. radar.config(schema)
  9. radar.add("预算分配", v1, is_splitline=True, is_axisline_show=True)
  10. radar.add("实际开销", v2, label_color=["#4e79a7"], is_area_show=False)
  11. radar.show_config()
  12. radar.render()

点击上方的红蓝点还可以显示/隐藏图线

  1. #v0.5
  2. from pyecharts import Radar
  3. value_bj = [
  4. [55, 9, 56, 0.46, 18, 6, 1], [25, 11, 21, 0.65, 34, 9, 2],
  5. [56, 7, 63, 0.3, 14, 5, 3], [33, 7, 29, 0.33, 16, 6, 4]]
  6. value_sh = [
  7. [91, 45, 125, 0.82, 34, 23, 1], [65, 27, 78, 0.86, 45, 29, 2],
  8. [83, 60, 84, 1.09, 73, 27, 3], [109, 81, 121, 1.28, 68, 51, 4]]
  9. c_schema= [{"name": "AQI", "max": 300, "min": 5},
  10. {"name": "PM2.5", "max": 250, "min": 20},
  11. {"name": "PM10", "max": 300, "min": 5},
  12. {"name": "CO", "max": 5},
  13. {"name": "NO2", "max": 200},
  14. {"name": "SO2", "max": 100}]
  15. radar = Radar()
  16. radar.config(c_schema=c_schema, shape='circle')
  17. radar.add("北京", value_bj, item_color="#f9713c", symbol=None)
  18. radar.add("上海", value_sh, item_color="#b3e4a1", symbol=None)
  19. radar.show_config()
  20. radar.render()

漏斗图

  1. #v0.5
  2. from pyecharts import Funnel
  3. attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
  4. value = [20, 40, 60, 80, 100, 120]
  5. funnel = Funnel("漏斗图示例")
  6. funnel.add("商品", attr, value, is_label_show=True, label_pos="inside", label_text_color="#fff")
  7. funnel.render()

  1. #v1
  2. from pyecharts import options as opts
  3. from pyecharts.charts import Funnel, Page
  4. from pyecharts.faker import Collector, Faker
  5. C = Collector()
  6. @C.funcs
  7. def funnel_base() -> Funnel:
  8. c = (
  9. Funnel()
  10. .add("商品", [list(z) for z in zip(Faker.choose(), Faker.values())])
  11. .set_global_opts(title_opts=opts.TitleOpts(title="Funnel-基本示例"))
  12. )
  13. return c
  14. @C.funcs
  15. def funnel_label_inside() -> Funnel:
  16. c = (
  17. Funnel()
  18. .add(
  19. "商品",
  20. [list(z) for z in zip(Faker.choose(), Faker.values())],
  21. label_opts=opts.LabelOpts(position="inside"),
  22. )
  23. .set_global_opts(title_opts=opts.TitleOpts(title="Funnel-Label(inside)"))
  24. )
  25. return c
  26. @C.funcs
  27. def funnel_sort_ascending() -> Funnel:
  28. c = (
  29. Funnel()
  30. .add(
  31. "商品",
  32. [list(z) for z in zip(Faker.choose(), Faker.values())],
  33. sort_="ascending",
  34. label_opts=opts.LabelOpts(position="inside"),
  35. )
  36. .set_global_opts(title_opts=opts.TitleOpts(title="Funnel-Sort(ascending)"))
  37. )
  38. return c
  39. Page().add(*[fn() for fn, _ in C.charts]).render()

日历图

  1. import datetime
  2. import random
  3. from pyecharts import options as opts
  4. from pyecharts.charts import Calendar, Page
  5. from pyecharts.faker import Collector
  6. C = Collector()
  7. @C.funcs
  8. def calendar_base() -> Calendar:
  9. begin = datetime.date(2017, 1, 1)
  10. end = datetime.date(2017, 12, 31)
  11. data = [
  12. [str(begin + datetime.timedelta(days=i)), random.randint(1000, 25000)]
  13. for i in range((end - begin).days + 1)
  14. ]
  15. c = (
  16. Calendar()
  17. .add("", data, calendar_opts=opts.CalendarOpts(range_="2017"))
  18. .set_global_opts(
  19. title_opts=opts.TitleOpts(title="Calendar-2017年微信步数情况"),
  20. visualmap_opts=opts.VisualMapOpts(
  21. max_=20000,
  22. min_=500,
  23. orient="horizontal",
  24. is_piecewise=True,
  25. pos_top="230px",
  26. pos_left="100px",
  27. ),
  28. )
  29. )
  30. return c
  31. @C.funcs
  32. def calendar_label_setting() -> Calendar:
  33. begin = datetime.date(2017, 1, 1)
  34. end = datetime.date(2017, 12, 31)
  35. data = [
  36. [str(begin + datetime.timedelta(days=i)), random.randint(1000, 25000)]
  37. for i in range((end - begin).days + 1)
  38. ]
  39. c = (
  40. Calendar()
  41. .add(
  42. "",
  43. data,
  44. calendar_opts=opts.CalendarOpts(
  45. range_="2017",
  46. daylabel_opts=opts.CalendarDayLabelOpts(name_map="cn"),
  47. monthlabel_opts=opts.CalendarMonthLabelOpts(name_map="cn"),
  48. ),
  49. )
  50. .set_global_opts(
  51. title_opts=opts.TitleOpts(title="Calendar-2017年微信步数情况(中文 Label)"),
  52. visualmap_opts=opts.VisualMapOpts(
  53. max_=20000,
  54. min_=500,
  55. orient="horizontal",
  56. is_piecewise=True,
  57. pos_top="230px",
  58. pos_left="100px",
  59. ),
  60. )
  61. )
  62. return c
  63. Page().add(*[fn() for fn, _ in C.charts]).render()

微博转发关系图

  1. #v0.5
  2. from pyecharts import Graph
  3. import json
  4. with open("weibo.json", "r", encoding="utf-8") as f:
  5. j = json.load(f)
  6. nodes, links, categories, cont, mid, userl = j
  7. graph = Graph("微博转发关系图", width=1200, height=600)
  8. graph.add("", nodes, links, categories, label_pos="right", repulsion=50, is_legend_show=False,
  9. line_curve=0.2, label_text_color=None)
  10. graph.show_config()
  11. graph.render()

weibo.json可以在这里下

github.com/pyecharts/pyecharts/tree/master/example/fixtures

  1. import json
  2. import os
  3. from pyecharts.commons.utils import JsCode
  4. #v1
  5. from pyecharts import options as opts
  6. from pyecharts.charts import Graph, Page
  7. from pyecharts.faker import Collector
  8. C = Collector()
  9. @C.funcs
  10. def graph_base() -> Graph:
  11. nodes = [
  12. {"name": "结点1", "symbolSize": 10},
  13. {"name": "结点2", "symbolSize": 20},
  14. {"name": "结点3", "symbolSize": 30},
  15. {"name": "结点4", "symbolSize": 40},
  16. {"name": "结点5", "symbolSize": 50},
  17. {"name": "结点6", "symbolSize": 40},
  18. {"name": "结点7", "symbolSize": 30},
  19. {"name": "结点8", "symbolSize": 20},
  20. ]
  21. links = []
  22. for i in nodes:
  23. for j in nodes:
  24. links.append({"source": i.get("name"), "target": j.get("name")})
  25. c = (
  26. Graph()
  27. .add("", nodes, links, repulsion=8000)
  28. .set_global_opts(title_opts=opts.TitleOpts(title="Graph-基本示例"))
  29. )
  30. return c
  31. @C.funcs
  32. def graph_with_opts() -> Graph:
  33. nodes = [
  34. opts.GraphNode(name="结点1", symbol_size=10),
  35. opts.GraphNode(name="结点2", symbol_size=20),
  36. opts.GraphNode(name="结点3", symbol_size=30),
  37. opts.GraphNode(name="结点4", symbol_size=40),
  38. opts.GraphNode(name="结点5", symbol_size=50),
  39. ]
  40. links = [
  41. opts.GraphLink(source="结点1", target="结点2"),
  42. opts.GraphLink(source="结点2", target="结点3"),
  43. opts.GraphLink(source="结点3", target="结点4"),
  44. opts.GraphLink(source="结点4", target="结点5"),
  45. opts.GraphLink(source="结点5", target="结点1"),
  46. ]
  47. c = (
  48. Graph()
  49. .add("", nodes, links, repulsion=4000)
  50. .set_global_opts(title_opts=opts.TitleOpts(title="Graph-GraphNode-GraphLink"))
  51. )
  52. return c
  53. @C.funcs
  54. def graph_with_edge_opts() -> Graph:
  55. nodes_data = [
  56. opts.GraphNode(name="结点1", symbol_size=10),
  57. opts.GraphNode(name="结点2", symbol_size=20),
  58. opts.GraphNode(name="结点3", symbol_size=30),
  59. opts.GraphNode(name="结点4", symbol_size=40),
  60. opts.GraphNode(name="结点5", symbol_size=50),
  61. opts.GraphNode(name="结点6", symbol_size=60),
  62. ]
  63. links_data = [
  64. opts.GraphLink(source="结点1", target="结点2", value=2),
  65. opts.GraphLink(source="结点2", target="结点3", value=3),
  66. opts.GraphLink(source="结点3", target="结点4", value=4),
  67. opts.GraphLink(source="结点4", target="结点5", value=5),
  68. opts.GraphLink(source="结点5", target="结点6", value=6),
  69. opts.GraphLink(source="结点6", target="结点1", value=7),
  70. ]
  71. c = (
  72. Graph()
  73. .add(
  74. "",
  75. nodes_data,
  76. links_data,
  77. repulsion=4000,
  78. edge_label=opts.LabelOpts(
  79. is_show=True, position="middle", formatter="{b} 的数据 {c}"
  80. ),
  81. )
  82. .set_global_opts(
  83. title_opts=opts.TitleOpts(title="Graph-GraphNode-GraphLink-WithEdgeLabel")
  84. )
  85. )
  86. return c
  87. @C.funcs
  88. def graph_weibo() -> Graph:
  89. with open(os.path.join("fixtures", "weibo.json"), "r", encoding="utf-8") as f:
  90. j = json.load(f)
  91. nodes, links, categories, cont, mid, userl = j
  92. c = (
  93. Graph()
  94. .add(
  95. "",
  96. nodes,
  97. links,
  98. categories,
  99. repulsion=50,
  100. linestyle_opts=opts.LineStyleOpts(curve=0.2),
  101. label_opts=opts.LabelOpts(is_show=False),
  102. )
  103. .set_global_opts(
  104. legend_opts=opts.LegendOpts(is_show=False),
  105. title_opts=opts.TitleOpts(title="Graph-微博转发关系图"),
  106. )
  107. )
  108. return c
  109. @C.funcs
  110. def graph_les_miserables():
  111. with open(
  112. os.path.join("fixtures", "les-miserables.json"), "r", encoding="utf-8"
  113. ) as f:
  114. j = json.load(f)
  115. nodes = j["nodes"]
  116. links = j["links"]
  117. categories = j["categories"]
  118. c = (
  119. Graph(init_opts=opts.InitOpts(width="1000px", height="600px"))
  120. .add(
  121. "",
  122. nodes=nodes,
  123. links=links,
  124. categories=categories,
  125. layout="circular",
  126. is_rotate_label=True,
  127. linestyle_opts=opts.LineStyleOpts(color="source", curve=0.3),
  128. label_opts=opts.LabelOpts(position="right"),
  129. )
  130. .set_global_opts(
  131. title_opts=opts.TitleOpts(title="Graph-Les Miserables"),
  132. legend_opts=opts.LegendOpts(
  133. orient="vertical", pos_left="2%", pos_top="20%"
  134. ),
  135. )
  136. )
  137. return c
  138. @C.funcs
  139. def graph_npm_dependencies() -> Graph:
  140. with open(os.path.join("fixtures", "npmdepgraph.json"), "r", encoding="utf-8") as f:
  141. j = json.load(f)
  142. nodes = [
  143. {
  144. "x": node["x"],
  145. "y": node["y"],
  146. "id": node["id"],
  147. "name": node["label"],
  148. "symbolSize": node["size"],
  149. "itemStyle": {"normal": {"color": node["color"]}},
  150. }
  151. for node in j["nodes"]
  152. ]
  153. edges = [
  154. {"source": edge["sourceID"], "target": edge["targetID"]} for edge in j["edges"]
  155. ]
  156. c = (
  157. Graph(init_opts=opts.InitOpts(width="1000px", height="600px"))
  158. .add(
  159. "",
  160. nodes=nodes,
  161. links=edges,
  162. layout="none",
  163. label_opts=opts.LabelOpts(is_show=False),
  164. linestyle_opts=opts.LineStyleOpts(width=0.5, curve=0.3, opacity=0.7),
  165. )
  166. .set_global_opts(title_opts=opts.TitleOpts(title="Graph-NPM Dependencies"))
  167. )
  168. return c
  169. Page().add(*[fn() for fn, _ in C.charts]).render()

带有涟漪特效动画的散点图

  1. #v0.5
  2. from pyecharts import EffectScatter
  3. es = EffectScatter("动态散点图各种图形示例")
  4. es.add("", [10], [10], symbol_size=20, effect_scale=3.5, effect_period=3, symbol="pin")
  5. es.add("", [20], [20], symbol_size=12, effect_scale=4.5, effect_period=4,symbol="rect")
  6. es.add("", [30], [30], symbol_size=30, effect_scale=5.5, effect_period=5,symbol="roundRect")
  7. es.add("", [40], [40], symbol_size=10, effect_scale=6.5, effect_brushtype='fill',symbol="diamond")
  8. es.add("", [50], [50], symbol_size=16, effect_scale=5.5, effect_period=3,symbol="arrow")
  9. es.add("", [60], [60], symbol_size=6, effect_scale=2.5, effect_period=3,symbol="triangle")
  10. es.render()

  1. #v0.5
  2. from pyecharts import EffectScatter
  3. es = EffectScatter('散点图举例',background_color = 'white',title_text_size = 25)
  4. v1 = [12,22,34,29,16,14,18]
  5. v2 = [23,45,68,58,32,28,36]
  6. es.add('', v1, v2,symbol = 'pin',effect_scale = 5.5,xaxis_min = 10)
  7. es.render()

  1. #v0.5
  2. from pyecharts import EffectScatter
  3. v1 = [10, 20, 30, 40, 50, 60]
  4. v2 = [25, 20, 15, 10, 60, 33]
  5. es = EffectScatter("动态散点图示例")
  6. es.add("effectScatter", v1, v2)
  7. es.render()

关系图——力引导布局

  1. #v0.5
  2. from pyecharts import Graph
  3. nodes = [{"name": "结点1", "symbolSize": 10},
  4. {"name": "结点2", "symbolSize": 20},
  5. {"name": "结点3", "symbolSize": 30},
  6. {"name": "结点4", "symbolSize": 40},
  7. {"name": "结点5", "symbolSize": 50},
  8. {"name": "结点6", "symbolSize": 40},
  9. {"name": "结点7", "symbolSize": 30},
  10. {"name": "结点8", "symbolSize": 20}]
  11. links = []
  12. for i in nodes:
  13. for j in nodes:
  14. links.append({"source": i.get('name'), "target": j.get('name')})
  15. graph = Graph("关系图-环形布局示例")
  16. graph.add("", nodes, links, is_label_show=True, repulsion=8000, layout='circular', label_text_color=None)
  17. graph.show_config()
  18. graph.render()

饼图

  1. #v0.5
  2. from pyecharts import Pie
  3. attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
  4. v1 = [11, 12, 13, 10, 10, 10]
  5. pie = Pie("饼图示例")
  6. pie.add("", attr, v1, is_label_show=True)
  7. pie.show_config()
  8. pie.render()

饼图—玫瑰图

  1. #v0.5
  2. from pyecharts import Pie
  3. attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
  4. v1 = [11, 12, 13, 10, 10, 10]
  5. v2 = [19, 21, 32, 20, 20, 33]
  6. pie = Pie("饼图-玫瑰图示例", title_pos='center', width=900)
  7. pie.add("商品A", attr, v1, center=[25, 50], is_random=True, radius=[30, 75], rosetype='radius')
  8. pie.add("商品B", attr, v2, center=[75, 50], is_random=True, radius=[30, 75], rosetype='area',
  9. is_legend_show=False, is_label_show=True)
  10. pie.show_config()
  11. pie.render()

  1. #v0.5
  2. from pyecharts import Pie
  3. pie =Pie('各类电影中"好片"所占的比例', "数据来着豆瓣", title_pos='center')
  4. pie.add("", ["剧情", ""], [25, 75], center=[10, 30], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None, )
  5. pie.add("", ["奇幻", ""], [24, 76], center=[30, 30], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None, legend_pos='left')
  6. pie.add("", ["爱情", ""], [14, 86], center=[50, 30], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None)
  7. pie.add("", ["惊悚", ""], [11, 89], center=[70, 30], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None)
  8. pie.add("", ["冒险", ""], [27, 73], center=[90, 30], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None)
  9. pie.add("", ["动作", ""], [15, 85], center=[10, 70], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None)
  10. pie.add("", ["喜剧", ""], [54, 46], center=[30, 70], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None)
  11. pie.add("", ["科幻", ""], [26, 74], center=[50, 70], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None)
  12. pie.add("", ["悬疑", ""], [25, 75], center=[70, 70], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None)
  13. pie.add("", ["犯罪", ""], [28, 72], center=[90, 70], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None, is_legend_show=True, legend_top="center")
  14. pie.show_config()
  15. pie.render()

极坐标系

  1. #v0.5
  2. from pyecharts import Polar
  3. radius = ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
  4. polar = Polar("极坐标系-堆叠柱状图示例", width=1200, height=600)
  5. polar.add("A", [1, 2, 3, 4, 3, 5, 1], radius_data=radius, type='barRadius', is_stack=True)
  6. polar.add("B", [2, 4, 6, 1, 2, 3, 1], radius_data=radius, type='barRadius', is_stack=True)
  7. polar.add("C", [1, 2, 3, 4, 1, 2, 5], radius_data=radius, type='barRadius', is_stack=True)
  8. polar.show_config()
  9. polar.render()

  1. #v0.5
  2. from pyecharts import Polar
  3. radius = ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
  4. polar = Polar("极坐标系-堆叠柱状图示例", width=1200, height=600)
  5. polar.add("", [1, 2, 3, 4, 3, 5, 1], radius_data=radius, type='barAngle', is_stack=True)
  6. polar.add("", [2, 4, 6, 1, 2, 3, 1], radius_data=radius, type='barAngle', is_stack=True)
  7. polar.add("", [1, 2, 3, 4, 1, 2, 5], radius_data=radius, type='barAngle', is_stack=True)
  8. polar.show_config()
  9. polar.render()

  1. #v1
  2. import math
  3. import random
  4. from pyecharts import options as opts
  5. from pyecharts.charts import Page, Polar
  6. from pyecharts.faker import Collector, Faker
  7. C = Collector()
  8. @C.funcs
  9. def polar_scatter0() -> Polar:
  10. data = [(i, random.randint(1, 100)) for i in range(101)]
  11. c = (
  12. Polar()
  13. .add("", data, type_="scatter", label_opts=opts.LabelOpts(is_show=False))
  14. .set_global_opts(title_opts=opts.TitleOpts(title="Polar-Scatter0"))
  15. )
  16. return c
  17. @C.funcs
  18. def polar_scatter1() -> Polar:
  19. c = (
  20. Polar()
  21. .add("", [(10, random.randint(1, 100)) for i in range(300)], type_="scatter")
  22. .add("", [(11, random.randint(1, 100)) for i in range(300)], type_="scatter")
  23. .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
  24. .set_global_opts(title_opts=opts.TitleOpts(title="Polar-Scatter1"))
  25. )
  26. return c
  27. @C.funcs
  28. def polar_effectscatter() -> Polar:
  29. data = [(i, random.randint(1, 100)) for i in range(10)]
  30. c = (
  31. Polar()
  32. .add(
  33. "",
  34. data,
  35. type_="effectScatter",
  36. effect_opts=opts.EffectOpts(scale=10, period=5),
  37. label_opts=opts.LabelOpts(is_show=False),
  38. )
  39. .set_global_opts(title_opts=opts.TitleOpts(title="Polar-EffectScatter"))
  40. )
  41. return c
  42. @C.funcs
  43. def polar_radiusaxis() -> Polar:
  44. c = (
  45. Polar()
  46. .add_schema(
  47. radiusaxis_opts=opts.RadiusAxisOpts(data=Faker.week, type_="category"),
  48. angleaxis_opts=opts.AngleAxisOpts(is_clockwise=True, max_=10),
  49. )
  50. .add("A", [1, 2, 3, 4, 3, 5, 1], type_="bar")
  51. .set_global_opts(title_opts=opts.TitleOpts(title="Polar-RadiusAxis"))
  52. .set_series_opts(label_opts=opts.LabelOpts(is_show=True))
  53. )
  54. return c
  55. @C.funcs
  56. def polar_angleaxis() -> Polar:
  57. c = (
  58. Polar()
  59. .add_schema(
  60. angleaxis_opts=opts.AngleAxisOpts(data=Faker.week, type_="category")
  61. )
  62. .add("A", [1, 2, 3, 4, 3, 5, 1], type_="bar", stack="stack0")
  63. .add("B", [2, 4, 6, 1, 2, 3, 1], type_="bar", stack="stack0")
  64. .add("C", [1, 2, 3, 4, 1, 2, 5], type_="bar", stack="stack0")
  65. .set_global_opts(title_opts=opts.TitleOpts(title="Polar-AngleAxis"))
  66. )
  67. return c
  68. @C.funcs
  69. def polar_love() -> Polar:
  70. data = []
  71. for i in range(101):
  72. theta = i / 100 * 360
  73. r = 5 * (1 + math.sin(theta / 180 * math.pi))
  74. data.append([r, theta])
  75. hour = [i for i in range(1, 25)]
  76. c = (
  77. Polar()
  78. .add_schema(
  79. angleaxis_opts=opts.AngleAxisOpts(
  80. data=hour, type_="value", boundary_gap=False, start_angle=0
  81. )
  82. )
  83. .add("love", data, label_opts=opts.LabelOpts(is_show=False))
  84. .set_global_opts(title_opts=opts.TitleOpts(title="Polar-Love"))
  85. )
  86. return c
  87. @C.funcs
  88. def polar_flower() -> Polar:
  89. data = []
  90. for i in range(361):
  91. t = i / 180 * math.pi
  92. r = math.sin(2 * t) * math.cos(2 * t)
  93. data.append([r, i])
  94. c = (
  95. Polar()
  96. .add_schema(angleaxis_opts=opts.AngleAxisOpts(start_angle=0, min_=0))
  97. .add("flower", data, label_opts=opts.LabelOpts(is_show=False))
  98. .set_global_opts(title_opts=opts.TitleOpts(title="Polar-Flower"))
  99. )
  100. return c
  101. Page().add(*[fn() for fn, _ in C.charts]).render()

极坐标系画图案

  1. #v0.5
  2. import math
  3. from pyecharts import Polar
  4. data =[]
  5. for i in range(101):
  6. theta =i /100*360
  7. r =5*(1+math.sin(theta /180*math.pi))
  8. data.append([r, theta])
  9. hour =[i for i in range(1, 25)]
  10. polar =Polar("极坐标系示例", width=1200, height=600)
  11. polar.add("Love", data, angle_data=hour, boundary_gap=False,start_angle=0)
  12. polar.show_config()
  13. polar.render()

  1. #v0.5
  2. import math
  3. from pyecharts import Polar
  4. data =[]
  5. for i in range(361):
  6. t =i /180*math.pi
  7. r =math.sin(2*t) *math.cos(2*t)
  8. data.append([r, i])
  9. polar =Polar("极坐标系示例", width=1200, height=600)
  10. polar.add("Flower", data, start_angle=0, symbol=None, axis_range=[0, None])
  11. polar.show_config()
  12. polar.render()

  1. #v0.5
  2. import math
  3. from pyecharts import Polar
  4. data =[]
  5. for i in range(361):
  6. t =i /180*math.pi
  7. r =math.sin(2*t) *math.cos(2*t)
  8. data.append([r, i])
  9. polar =Polar("极坐标系示例", width=1200, height=600)
  10. polar.add("Color-Flower", data, start_angle=0, symbol=None, axis_range=[0, None], area_color="#f71f24", area_opacity=0.6)
  11. polar.show_config()
  12. polar.render()

  1. #v0.5
  2. import math
  3. from pyecharts import Polar
  4. data =[]
  5. for i in range(5):
  6. for j in range(101):
  7. theta =j /100*360
  8. alpha =i *360+theta
  9. r =math.pow(math.e, 0.003*alpha)
  10. data.append([r, theta])
  11. polar =Polar("极坐标系示例")
  12. polar.add("", data, symbol_size=0, symbol='circle', start_angle=-25, is_radiusaxis_show=False, area_color="#f3c5b3", area_opacity=0.5, is_angleaxis_show=False)
  13. polar.show_config()
  14. polar.render()

折线/面积图

  1. #v0.5
  2. from pyecharts import Line
  3. attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
  4. v1 = [5, 20, 36, 10, 10, 100]
  5. v2 = [55, 60, 16, 20, 15, 80]
  6. line = Line("折线图示例")
  7. line.add("商家A", attr, v1, mark_point=["average"])
  8. line.add("商家B", attr, v2, is_smooth=True, mark_line=["max", "average"])
  9. line.show_config()
  10. line.render()

  1. #v0.5
  2. from pyecharts import Line
  3. attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
  4. v1 = [5, 20, 36, 10, 10, 100]
  5. line = Line("折线图-阶梯图示例")
  6. line.add("商家A", attr, v1, is_step=True, is_label_show=True)
  7. line.show_config()
  8. line.render()

  1. #v0.5
  2. from pyecharts import Line
  3. attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
  4. v1 = [5, 20, 36, 10, 10, 100]
  5. v2 = [55, 60, 16, 20, 15, 80]
  6. line = Line("折线图-面积图示例")
  7. line.add("商家A", attr, v1, is_fill=True, line_opacity=0.2, area_opacity=0.4, symbol=None)
  8. line.add("商家B", attr, v2, is_fill=True, area_color='#000', area_opacity=0.3, is_smooth=True)
  9. line.show_config()
  10. line.render()

  1. #v1
  2. import pyecharts.options as opts
  3. from pyecharts.charts import Line, Page
  4. from pyecharts.commons.utils import JsCode
  5. from pyecharts.faker import Collector, Faker
  6. C = Collector()
  7. @C.funcs
  8. def line_base() -> Line:
  9. c = (
  10. Line()
  11. .add_xaxis(Faker.choose())
  12. .add_yaxis("商家A", Faker.values())
  13. .add_yaxis("商家B", Faker.values())
  14. .set_global_opts(title_opts=opts.TitleOpts(title="Line-基本示例"))
  15. )
  16. return c
  17. @C.funcs
  18. def line_connect_null() -> Line:
  19. y = Faker.values()
  20. y[3], y[5] = None, None
  21. c = (
  22. Line()
  23. .add_xaxis(Faker.choose())
  24. .add_yaxis("商家A", y, is_connect_nones=True)
  25. .set_global_opts(title_opts=opts.TitleOpts(title="Line-连接空数据"))
  26. )
  27. return c
  28. @C.funcs
  29. def line_smooth() -> Line:
  30. c = (
  31. Line()
  32. .add_xaxis(Faker.choose())
  33. .add_yaxis("商家A", Faker.values(), is_smooth=True)
  34. .add_yaxis("商家B", Faker.values(), is_smooth=True)
  35. .set_global_opts(title_opts=opts.TitleOpts(title="Line-smooth"))
  36. )
  37. return c
  38. @C.funcs
  39. def line_areastyle() -> Line:
  40. c = (
  41. Line()
  42. .add_xaxis(Faker.choose())
  43. .add_yaxis(
  44. "商家A", Faker.values(), areastyle_opts=opts.AreaStyleOpts(opacity=0.5)
  45. )
  46. .add_yaxis(
  47. "商家B", Faker.values(), areastyle_opts=opts.AreaStyleOpts(opacity=0.5)
  48. )
  49. .set_global_opts(title_opts=opts.TitleOpts(title="Line-面积图"))
  50. )
  51. return c
  52. @C.funcs
  53. def line_areastyle_boundary_gap() -> Line:
  54. c = (
  55. Line()
  56. .add_xaxis(Faker.choose())
  57. .add_yaxis("商家A", Faker.values(), is_smooth=True)
  58. .add_yaxis("商家B", Faker.values(), is_smooth=True)
  59. .set_series_opts(
  60. areastyle_opts=opts.AreaStyleOpts(opacity=0.5),
  61. label_opts=opts.LabelOpts(is_show=False),
  62. )
  63. .set_global_opts(
  64. title_opts=opts.TitleOpts(title="Line-面积图(紧贴 Y 轴)"),
  65. xaxis_opts=opts.AxisOpts(
  66. axistick_opts=opts.AxisTickOpts(is_align_with_label=True),
  67. is_scale=False,
  68. boundary_gap=False,
  69. ),
  70. )
  71. )
  72. return c
  73. @C.funcs
  74. def line_yaxis_log() -> Line:
  75. c = (
  76. Line()
  77. .add_xaxis(xaxis_data=["一", "二", "三", "四", "五", "六", "七", "八", "九"])
  78. .add_yaxis(
  79. "2 的指数",
  80. y_axis=[1, 2, 4, 8, 16, 32, 64, 128, 256],
  81. linestyle_opts=opts.LineStyleOpts(width=2),
  82. )
  83. .add_yaxis(
  84. "3 的指数",
  85. y_axis=[1, 3, 9, 27, 81, 247, 741, 2223, 6669],
  86. linestyle_opts=opts.LineStyleOpts(width=2),
  87. )
  88. .set_global_opts(
  89. title_opts=opts.TitleOpts(title="Line-对数轴示例"),
  90. xaxis_opts=opts.AxisOpts(name="x"),
  91. yaxis_opts=opts.AxisOpts(
  92. type_="log",
  93. name="y",
  94. splitline_opts=opts.SplitLineOpts(is_show=True),
  95. is_scale=True,
  96. ),
  97. )
  98. )
  99. return c
  100. @C.funcs
  101. def line_markpoint_custom() -> Line:
  102. x, y = Faker.choose(), Faker.values()
  103. c = (
  104. Line()
  105. .add_xaxis(x)
  106. .add_yaxis(
  107. "商家A",
  108. y,
  109. markpoint_opts=opts.MarkPointOpts(
  110. data=[opts.MarkPointItem(name="自定义标记点", coord=[x[2], y[2]], value=y[2])]
  111. ),
  112. )
  113. .set_global_opts(title_opts=opts.TitleOpts(title="Line-MarkPoint(自定义)"))
  114. )
  115. return c
  116. @C.funcs
  117. def line_markpoint() -> Line:
  118. c = (
  119. Line()
  120. .add_xaxis(Faker.choose())
  121. .add_yaxis(
  122. "商家A",
  123. Faker.values(),
  124. markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(type_="min")]),
  125. )
  126. .add_yaxis(
  127. "商家B",
  128. Faker.values(),
  129. markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(type_="max")]),
  130. )
  131. .set_global_opts(title_opts=opts.TitleOpts(title="Line-MarkPoint"))
  132. )
  133. return c
  134. @C.funcs
  135. def line_markline() -> Line:
  136. c = (
  137. Line()
  138. .add_xaxis(Faker.choose())
  139. .add_yaxis(
  140. "商家A",
  141. Faker.values(),
  142. markline_opts=opts.MarkLineOpts(data=[opts.MarkLineItem(type_="average")]),
  143. )
  144. .add_yaxis(
  145. "商家B",
  146. Faker.values(),
  147. markline_opts=opts.MarkLineOpts(data=[opts.MarkLineItem(type_="average")]),
  148. )
  149. .set_global_opts(title_opts=opts.TitleOpts(title="Line-MarkLine"))
  150. )
  151. return c
  152. @C.funcs
  153. def line_step() -> Line:
  154. c = (
  155. Line()
  156. .add_xaxis(Faker.choose())
  157. .add_yaxis("商家A", Faker.values(), is_step=True)
  158. .set_global_opts(title_opts=opts.TitleOpts(title="Line-阶梯图"))
  159. )
  160. return c
  161. @C.funcs
  162. def line_itemstyle() -> Line:
  163. c = (
  164. Line()
  165. .add_xaxis(xaxis_data=Faker.choose())
  166. .add_yaxis(
  167. "商家A",
  168. Faker.values(),
  169. symbol="triangle",
  170. symbol_size=20,
  171. linestyle_opts=opts.LineStyleOpts(color="green", width=4, type_="dashed"),
  172. itemstyle_opts=opts.ItemStyleOpts(
  173. border_width=3, border_color="yellow", color="blue"
  174. ),
  175. )
  176. .set_global_opts(title_opts=opts.TitleOpts(title="Line-ItemStyle"))
  177. )
  178. return c
  179. @C.funcs
  180. def line_color_with_js_func() -> Line:
  181. x_data = ["14", "15", "16", "17", "18", "19", "20", "21", "22", "23"]
  182. y_data = [393, 438, 485, 631, 689, 824, 987, 1000, 1100, 1200]
  183. background_color_js = (
  184. "new echarts.graphic.LinearGradient(0, 0, 0, 1, "
  185. "[{offset: 0, color: '#c86589'}, {offset: 1, color: '#06a7ff'}], false)"
  186. )
  187. area_color_js = (
  188. "new echarts.graphic.LinearGradient(0, 0, 0, 1, "
  189. "[{offset: 0, color: '#eb64fb'}, {offset: 1, color: '#3fbbff0d'}], false)"
  190. )
  191. c = (
  192. Line(init_opts=opts.InitOpts(bg_color=JsCode(background_color_js)))
  193. .add_xaxis(xaxis_data=x_data)
  194. .add_yaxis(
  195. series_name="注册总量",
  196. y_axis=y_data,
  197. is_smooth=True,
  198. is_symbol_show=True,
  199. symbol="circle",
  200. symbol_size=6,
  201. linestyle_opts=opts.LineStyleOpts(color="#fff"),
  202. label_opts=opts.LabelOpts(is_show=True, position="top", color="white"),
  203. itemstyle_opts=opts.ItemStyleOpts(
  204. color="red", border_color="#fff", border_width=3
  205. ),
  206. tooltip_opts=opts.TooltipOpts(is_show=False),
  207. areastyle_opts=opts.AreaStyleOpts(color=JsCode(area_color_js), opacity=1),
  208. )
  209. .set_global_opts(
  210. title_opts=opts.TitleOpts(
  211. title="OCTOBER 2015",
  212. pos_bottom="5%",
  213. pos_left="center",
  214. title_textstyle_opts=opts.TextStyleOpts(color="#fff", font_size=16),
  215. ),
  216. xaxis_opts=opts.AxisOpts(
  217. type_="category",
  218. boundary_gap=False,
  219. axislabel_opts=opts.LabelOpts(margin=30, color="#ffffff63"),
  220. axisline_opts=opts.AxisLineOpts(is_show=False),
  221. axistick_opts=opts.AxisTickOpts(
  222. is_show=True,
  223. length=25,
  224. linestyle_opts=opts.LineStyleOpts(color="#ffffff1f"),
  225. ),
  226. splitline_opts=opts.SplitLineOpts(
  227. is_show=True, linestyle_opts=opts.LineStyleOpts(color="#ffffff1f")
  228. ),
  229. ),
  230. yaxis_opts=opts.AxisOpts(
  231. type_="value",
  232. position="right",
  233. axislabel_opts=opts.LabelOpts(margin=20, color="#ffffff63"),
  234. axisline_opts=opts.AxisLineOpts(
  235. linestyle_opts=opts.LineStyleOpts(width=2, color="#fff")
  236. ),
  237. axistick_opts=opts.AxisTickOpts(
  238. is_show=True,
  239. length=15,
  240. linestyle_opts=opts.LineStyleOpts(color="#ffffff1f"),
  241. ),
  242. splitline_opts=opts.SplitLineOpts(
  243. is_show=True, linestyle_opts=opts.LineStyleOpts(color="#ffffff1f")
  244. ),
  245. ),
  246. legend_opts=opts.LegendOpts(is_show=False),
  247. )
  248. )
  249. return c
  250. Page().add(*[fn() for fn, _ in C.charts]).render()

Parallel(平行坐标系)

  1. #v0.5
  2. from pyecharts import Parallel
  3. c_schema = [
  4. {"dim": 0, "name": "data"},
  5. {"dim": 1, "name": "AQI"},
  6. {"dim": 2, "name": "PM2.5"},
  7. {"dim": 3, "name": "PM10"},
  8. {"dim": 4, "name": "CO"},
  9. {"dim": 5, "name": "NO2"},
  10. {"dim": 6, "name": "CO2"},
  11. {"dim": 7, "name": "等级",
  12. "type": "category", "data": ['优', '良', '轻度污染', '中度污染', '重度污染', '严重污染']}
  13. ]
  14. data = [
  15. [1, 91, 45, 125, 0.82, 34, 23, "良"],
  16. [2, 65, 27, 78, 0.86, 45, 29, "良"],
  17. [3, 83, 60, 84, 1.09, 73, 27, "良"],
  18. [4, 109, 81, 121, 1.28, 68, 51, "轻度污染"],
  19. [5, 106, 77, 114, 1.07, 55, 51, "轻度污染"],
  20. [6, 109, 81, 121, 1.28, 68, 51, "轻度污染"],
  21. [7, 106, 77, 114, 1.07, 55, 51, "轻度污染"],
  22. [8, 89, 65, 78, 0.86, 51, 26, "良"],
  23. [9, 53, 33, 47, 0.64, 50, 17, "良"],
  24. [10, 80, 55, 80, 1.01, 75, 24, "良"],
  25. [11, 117, 81, 124, 1.03, 45, 24, "轻度污染"],
  26. [12, 99, 71, 142, 1.1, 62, 42, "良"],
  27. [13, 95, 69, 130, 1.28, 74, 50, "良"],
  28. [14, 116, 87, 131, 1.47, 84, 40, "轻度污染"]
  29. ]
  30. parallel = Parallel("平行坐标系-用户自定义指示器")
  31. parallel.config(c_schema=c_schema)
  32. parallel.add("parallel", data)
  33. parallel.show_config()
  34. parallel.render()

3d折线图

  1. import math
  2. from pyecharts import options as opts
  3. from pyecharts.charts import Line3D, Page
  4. from pyecharts.faker import Collector, Faker
  5. C = Collector()
  6. @C.funcs
  7. def line3d_base() -> Line3D:
  8. data = []
  9. for t in range(0, 25000):
  10. _t = t / 1000
  11. x = (1 + 0.25 * math.cos(75 * _t)) * math.cos(_t)
  12. y = (1 + 0.25 * math.cos(75 * _t)) * math.sin(_t)
  13. z = _t + 2.0 * math.sin(75 * _t)
  14. data.append([x, y, z])
  15. c = (
  16. Line3D()
  17. .add(
  18. "",
  19. data,
  20. xaxis3d_opts=opts.Axis3DOpts(Faker.clock, type_="value"),
  21. yaxis3d_opts=opts.Axis3DOpts(Faker.week_en, type_="value"),
  22. grid3d_opts=opts.Grid3DOpts(width=100, height=100, depth=100),
  23. )
  24. .set_global_opts(
  25. visualmap_opts=opts.VisualMapOpts(
  26. max_=30, min_=0, range_color=Faker.visual_color
  27. ),
  28. title_opts=opts.TitleOpts(title="Line3D-基本示例"),
  29. )
  30. )
  31. return c
  32. @C.funcs
  33. def line3d_auto_rotate() -> Line3D:
  34. data = []
  35. for t in range(0, 25000):
  36. _t = t / 1000
  37. x = (1 + 0.25 * math.cos(75 * _t)) * math.cos(_t)
  38. y = (1 + 0.25 * math.cos(75 * _t)) * math.sin(_t)
  39. z = _t + 2.0 * math.sin(75 * _t)
  40. data.append([x, y, z])
  41. c = (
  42. Line3D()
  43. .add(
  44. "",
  45. data,
  46. xaxis3d_opts=opts.Axis3DOpts(Faker.clock, type_="value"),
  47. yaxis3d_opts=opts.Axis3DOpts(Faker.week_en, type_="value"),
  48. grid3d_opts=opts.Grid3DOpts(
  49. width=100, depth=100, rotate_speed=150, is_rotate=True
  50. ),
  51. )
  52. .set_global_opts(
  53. visualmap_opts=opts.VisualMapOpts(
  54. max_=30, min_=0, range_color=Faker.visual_color
  55. ),
  56. title_opts=opts.TitleOpts(title="Line3D-旋转的弹簧"),
  57. )
  58. )
  59. return c
  60. Page().add(*[fn() for fn, _ in C.charts]).render()

surface3D

  1. #v1
  2. import math
  3. from pyecharts import options as opts
  4. from pyecharts.charts import Page, Surface3D
  5. from pyecharts.faker import Collector, Faker
  6. C = Collector()
  7. @C.funcs
  8. def surface3d_base() -> Surface3D:
  9. def surface3d_data():
  10. for t0 in range(-60, 60, 1):
  11. y = t0 / 60
  12. for t1 in range(-60, 60, 1):
  13. x = t1 / 60
  14. if math.fabs(x) < 0.1 and math.fabs(y) < 0.1:
  15. z = "-"
  16. else:
  17. z = math.sin(x * math.pi) * math.sin(y * math.pi)
  18. yield [x, y, z]
  19. c = (
  20. Surface3D()
  21. .add(
  22. "",
  23. list(surface3d_data()),
  24. xaxis3d_opts=opts.Axis3DOpts(type_="value"),
  25. yaxis3d_opts=opts.Axis3DOpts(type_="value"),
  26. grid3d_opts=opts.Grid3DOpts(width=100, height=100, depth=100),
  27. )
  28. .set_global_opts(
  29. title_opts=opts.TitleOpts(title="Surface3D-基本示例"),
  30. visualmap_opts=opts.VisualMapOpts(
  31. max_=3, min_=-3, range_color=Faker.visual_color
  32. ),
  33. )
  34. )
  35. return c
  36. @C.funcs
  37. def surface3D_flower() -> Surface3D:
  38. def surface3d_data():
  39. for t0 in range(-30, 30, 1):
  40. y = t0 / 10
  41. for t1 in range(-30, 30, 1):
  42. x = t1 / 10
  43. z = math.sin(x * x + y * y) * x / 3.14
  44. yield [x, y, z]
  45. c = (
  46. Surface3D()
  47. .add(
  48. "",
  49. list(surface3d_data()),
  50. xaxis3d_opts=opts.Axis3DOpts(type_="value"),
  51. yaxis3d_opts=opts.Axis3DOpts(type_="value"),
  52. grid3d_opts=opts.Grid3DOpts(width=100, height=100, depth=100),
  53. )
  54. .set_global_opts(
  55. title_opts=opts.TitleOpts(title="Surface3D-曲面波图"),
  56. visualmap_opts=opts.VisualMapOpts(
  57. max_=1, min_=-1, range_color=Faker.visual_color
  58. ),
  59. )
  60. )
  61. return c
  62. Page().add(*[fn() for fn, _ in C.charts]).render()

河流图

  1. #v1
  2. from pyecharts import options as opts
  3. from pyecharts.charts import Page, ThemeRiver
  4. from pyecharts.faker import Collector
  5. C = Collector()
  6. @C.funcs
  7. def themeriver_example() -> ThemeRiver:
  8. data = [
  9. ["2015/11/08", 10, "DQ"],
  10. ["2015/11/09", 15, "DQ"],
  11. ["2015/11/10", 35, "DQ"],
  12. ["2015/11/14", 7, "DQ"],
  13. ["2015/11/15", 2, "DQ"],
  14. ["2015/11/16", 17, "DQ"],
  15. ["2015/11/17", 33, "DQ"],
  16. ["2015/11/18", 40, "DQ"],
  17. ["2015/11/19", 32, "DQ"],
  18. ["2015/11/20", 26, "DQ"],
  19. ["2015/11/08", 35, "TY"],
  20. ["2015/11/09", 36, "TY"],
  21. ["2015/11/10", 37, "TY"],
  22. ["2015/11/11", 22, "TY"],
  23. ["2015/11/12", 24, "TY"],
  24. ["2015/11/13", 26, "TY"],
  25. ["2015/11/14", 34, "TY"],
  26. ["2015/11/15", 21, "TY"],
  27. ["2015/11/16", 18, "TY"],
  28. ["2015/11/17", 45, "TY"],
  29. ["2015/11/18", 32, "TY"],
  30. ["2015/11/19", 35, "TY"],
  31. ["2015/11/20", 30, "TY"],
  32. ["2015/11/08", 21, "SS"],
  33. ["2015/11/09", 25, "SS"],
  34. ["2015/11/10", 27, "SS"],
  35. ["2015/11/11", 23, "SS"],
  36. ["2015/11/12", 24, "SS"],
  37. ["2015/11/13", 21, "SS"],
  38. ["2015/11/14", 35, "SS"],
  39. ["2015/11/15", 39, "SS"],
  40. ["2015/11/16", 40, "SS"],
  41. ["2015/11/17", 36, "SS"],
  42. ["2015/11/18", 33, "SS"],
  43. ["2015/11/19", 43, "SS"],
  44. ["2015/11/20", 40, "SS"],
  45. ["2015/11/14", 7, "QG"],
  46. ["2015/11/15", 2, "QG"],
  47. ["2015/11/16", 17, "QG"],
  48. ["2015/11/17", 33, "QG"],
  49. ["2015/11/18", 40, "QG"],
  50. ["2015/11/19", 32, "QG"],
  51. ["2015/11/20", 26, "QG"],
  52. ["2015/11/21", 35, "QG"],
  53. ["2015/11/22", 40, "QG"],
  54. ["2015/11/23", 32, "QG"],
  55. ["2015/11/24", 26, "QG"],
  56. ["2015/11/25", 22, "QG"],
  57. ["2015/11/08", 10, "SY"],
  58. ["2015/11/09", 15, "SY"],
  59. ["2015/11/10", 35, "SY"],
  60. ["2015/11/11", 38, "SY"],
  61. ["2015/11/12", 22, "SY"],
  62. ["2015/11/13", 16, "SY"],
  63. ["2015/11/14", 7, "SY"],
  64. ["2015/11/15", 2, "SY"],
  65. ["2015/11/16", 17, "SY"],
  66. ["2015/11/17", 33, "SY"],
  67. ["2015/11/18", 40, "SY"],
  68. ["2015/11/19", 32, "SY"],
  69. ["2015/11/20", 26, "SY"],
  70. ["2015/11/21", 35, "SY"],
  71. ["2015/11/22", 4, "SY"],
  72. ["2015/11/23", 32, "SY"],
  73. ["2015/11/24", 26, "SY"],
  74. ["2015/11/25", 22, "SY"],
  75. ["2015/11/08", 10, "DD"],
  76. ["2015/11/09", 15, "DD"],
  77. ["2015/11/10", 35, "DD"],
  78. ["2015/11/11", 38, "DD"],
  79. ["2015/11/12", 22, "DD"],
  80. ["2015/11/13", 16, "DD"],
  81. ["2015/11/14", 7, "DD"],
  82. ["2015/11/15", 2, "DD"],
  83. ["2015/11/16", 17, "DD"],
  84. ["2015/11/17", 33, "DD"],
  85. ["2015/11/18", 4, "DD"],
  86. ["2015/11/19", 32, "DD"],
  87. ["2015/11/20", 26, "DD"],
  88. ]
  89. c = (
  90. ThemeRiver()
  91. .add(
  92. ["DQ", "TY", "SS", "QG", "SY", "DD"],
  93. data,
  94. singleaxis_opts=opts.SingleAxisOpts(type_="time", pos_bottom="10%"),
  95. )
  96. .set_global_opts(title_opts=opts.TitleOpts(title="ThemeRiver-基本示例"))
  97. )
  98. return c
  99. Page().add(*[fn() for fn, _ in C.charts]).render()

树形图

  1. #v1
  2. import json
  3. import os
  4. from pyecharts import options as opts
  5. from pyecharts.charts import Page, Tree
  6. from pyecharts.faker import Collector
  7. C = Collector()
  8. @C.funcs
  9. def tree_base() -> Tree:
  10. data = [
  11. {
  12. "children": [
  13. {"name": "B"},
  14. {
  15. "children": [
  16. {"children": [{"name": "I"}], "name": "E"},
  17. {"name": "F"},
  18. ],
  19. "name": "C",
  20. },
  21. {
  22. "children": [
  23. {"children": [{"name": "J"}, {"name": "K"}], "name": "G"},
  24. {"name": "H"},
  25. ],
  26. "name": "D",
  27. },
  28. ],
  29. "name": "A",
  30. }
  31. ]
  32. c = (
  33. Tree()
  34. .add("", data)
  35. .set_global_opts(title_opts=opts.TitleOpts(title="Tree-基本示例"))
  36. )
  37. return c
  38. @C.funcs
  39. def tree_lr() -> Tree:
  40. with open(os.path.join("fixtures", "flare.json"), "r", encoding="utf-8") as f:
  41. j = json.load(f)
  42. c = (
  43. Tree()
  44. .add("", [j], collapse_interval=2)
  45. .set_global_opts(title_opts=opts.TitleOpts(title="Tree-左右方向"))
  46. )
  47. return c
  48. @C.funcs
  49. def tree_rl() -> Tree:
  50. with open(os.path.join("fixtures", "flare.json"), "r", encoding="utf-8") as f:
  51. j = json.load(f)
  52. c = (
  53. Tree()
  54. .add("", [j], collapse_interval=2, orient="RL")
  55. .set_global_opts(title_opts=opts.TitleOpts(title="Tree-右左方向"))
  56. )
  57. return c
  58. @C.funcs
  59. def tree_tb() -> Tree:
  60. with open(os.path.join("fixtures", "flare.json"), "r", encoding="utf-8") as f:
  61. j = json.load(f)
  62. c = (
  63. Tree()
  64. .add(
  65. "",
  66. [j],
  67. collapse_interval=2,
  68. orient="TB",
  69. label_opts=opts.LabelOpts(
  70. position="top",
  71. horizontal_align="right",
  72. vertical_align="middle",
  73. rotate=-90,
  74. ),
  75. )
  76. .set_global_opts(title_opts=opts.TitleOpts(title="Tree-上下方向"))
  77. )
  78. return c
  79. @C.funcs
  80. def tree_bt() -> Tree:
  81. with open(os.path.join("fixtures", "flare.json"), "r", encoding="utf-8") as f:
  82. j = json.load(f)
  83. c = (
  84. Tree()
  85. .add(
  86. "",
  87. [j],
  88. collapse_interval=2,
  89. orient="BT",
  90. label_opts=opts.LabelOpts(
  91. position="top",
  92. horizontal_align="right",
  93. vertical_align="middle",
  94. rotate=-90,
  95. ),
  96. )
  97. .set_global_opts(title_opts=opts.TitleOpts(title="Tree-下上方向"))
  98. )
  99. return c
  100. @C.funcs
  101. def tree_layout() -> Tree:
  102. with open(os.path.join("fixtures", "flare.json"), "r", encoding="utf-8") as f:
  103. j = json.load(f)
  104. c = (
  105. Tree()
  106. .add("", [j], collapse_interval=2, layout="radial")
  107. .set_global_opts(title_opts=opts.TitleOpts(title="Tree-Layout"))
  108. )
  109. return c
  110. Page().add(*[fn() for fn, _ in C.charts]).render()

TreeMap

  1. #v1
  2. import json
  3. import os
  4. from pyecharts import options as opts
  5. from pyecharts.charts import Page, TreeMap
  6. from pyecharts.faker import Collector
  7. C = Collector()
  8. @C.funcs
  9. def treemap_base() -> TreeMap:
  10. data = [
  11. {"value": 40, "name": "我是A"},
  12. {
  13. "value": 180,
  14. "name": "我是B",
  15. "children": [
  16. {
  17. "value": 76,
  18. "name": "我是B.children",
  19. "children": [
  20. {"value": 12, "name": "我是B.children.a"},
  21. {"value": 28, "name": "我是B.children.b"},
  22. {"value": 20, "name": "我是B.children.c"},
  23. {"value": 16, "name": "我是B.children.d"},
  24. ],
  25. }
  26. ],
  27. },
  28. ]
  29. c = (
  30. TreeMap()
  31. .add("演示数据", data)
  32. .set_global_opts(title_opts=opts.TitleOpts(title="TreeMap-基本示例"))
  33. )
  34. return c
  35. @C.funcs
  36. def treemap_official():
  37. with open(os.path.join("fixtures", "treemap.json"), "r", encoding="utf-8") as f:
  38. data = json.load(f)
  39. c = (
  40. TreeMap()
  41. .add("演示数据", data)
  42. .set_global_opts(title_opts=opts.TitleOpts(title="TreeMap-官方示例"))
  43. )
  44. return c
  45. Page().add(*[fn() for fn, _ in C.charts]).render()

条形图

  1. #v0.5
  2. from pyecharts import Bar
  3. bar = Bar('设置堆叠效果')
  4. attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
  5. v1 = [5, 20, 36, 10, 75, 90]
  6. v2 = [10, 25, 8, 60, 20, 80]
  7. bar.add('商家A', attr, v1, is_stack=True)
  8. bar.add("商家B", attr, v2, is_stack=True)
  9. bar.render()

  1. #v0.5
  2. from pyecharts import Bar
  3. #configure(output_image=True)
  4. bar = Bar('各个城市的人口','虚构的',background_color = 'white',title_text_size = 25,subtitle_text_size = 15)
  5. attr = ['惠州','东莞','广州','深圳','佛山','江门','珠海']
  6. v1 = [23,45,68,58,32,28,36]
  7. v2 = [12,22,34,29,16,14,18]
  8. bar.add('举例数字1',attr,v1,is_label_show = True,mark_point = ['min','max'],
  9. mark_point_symbol = 'diamond',xaxis_rotate = 30,xaxis_label_textsize = 15,yaxis_label_textsize = 15)
  10. bar.add('举例数字2',attr,v2,is_label_show = True,mark_point = ['min','max'],
  11. mark_point_symbol = 'triangle',xaxis_rotate = 30,xaxis_label_textsize = 15,yaxis_label_textsize = 15)
  12. bar.render()

  1. #v1
  2. from pyecharts.commons.utils import JsCode # 导入js代码库,可以调用一些js方法
  3. from pyecharts.faker import Faker
  4. from pyecharts import options as opts
  5. from pyecharts.charts import Bar, Grid # 导入网格库
  6. from pyecharts.globals import ThemeType # 导入主题库
  7. # 例2 渐变圆柱
  8. bar2=(
  9. Bar()
  10. .add_xaxis(Faker.choose())
  11. .add_yaxis("数据1",Faker.values(),category_gap="60%")
  12. # category_gap是同一系列的柱间距离,默认为类目间距的 20%,可设固定值
  13. .set_series_opts(itemstyle_opts={# set_series_opts设置系列配置
  14. "normal":{ # normal代表一般、正常情况
  15. # LinearGradient 设置线性渐变,offset为0是柱子0%处颜色,为1是100%处颜色
  16. "color": JsCode("""new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
  17. offset: 0,
  18. color: 'rgba(0, 233, 245, 1)'
  19. }, {
  20. offset: 1,
  21. color: 'rgba(0, 45, 187, 1)'
  22. }], false)"""),
  23. "barBorderRadius": [30, 30, 30, 30],# 设置柱子4个角为30变成圆柱
  24. "shadowColor": 'red',# 阴影颜色
  25. }})
  26. .set_global_opts(title_opts=opts.TitleOpts(title="Bar-渐变圆柱"))
  27. )
  28. bar2.render()

注意是v1版本

  1. #v1
  2. from pyecharts.commons.utils import JsCode # 导入js代码库,可以调用一些js方法
  3. from pyecharts.faker import Faker
  4. from pyecharts import options as opts
  5. from pyecharts.charts import Bar, Grid # 导入网格库
  6. from pyecharts.globals import ThemeType # 导入主题库
  7. # 例4 背景图基本示例
  8. bar4=(
  9. Bar(
  10. init_opts=opts.InitOpts(
  11. bg_color={
  12. "type":"pattern",
  13. "image":JsCode("img"),
  14. "repeat":"no-repeat",
  15. }# bg_color是设置背景颜色,这里image是设置图片背景,repeat设置no-repeat不重复
  16. )
  17. )
  18. .add_xaxis(Faker.choose())
  19. .add_yaxis("数据1",Faker.values())
  20. .add_yaxis("数据2",Faker.values())
  21. .set_global_opts(
  22. title_opts=opts.TitleOpts(
  23. title="Bar-背景图基本示例",
  24. subtitle="副标题",
  25. title_textstyle_opts=opts.TextStyleOpts(color='red'),
  26. )
  27. )
  28. )
  29. # 这里添加图片src连接,也可以换其他图片的网址
  30. bar4.add_js_funcs(
  31. '''
  32. var img=new Image();img.src='https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1571897969384&di=e417611ecd20f69eee6c549ef44e357e&imgtype=0&src=http%3A%2F%2Fimg8.zol.com.cn%2Fbbs%2Fupload%2F18863%2F18862812.JPG';
  33. '''
  34. )
  35. bar4.render()

3D柱状图

blog.csdn.net/HHG20171226/article/details/103113923

  1. #v0.5
  2. from pyecharts import Bar3D
  3. bar3d = Bar3D("3D 柱状图示例", width=1200, height=600)
  4. x_axis = [
  5. "12a", "1a", "2a", "3a", "4a", "5a", "6a", "7a", "8a", "9a", "10a", "11a",
  6. "12p", "1p", "2p", "3p", "4p", "5p", "6p", "7p", "8p", "9p", "10p", "11p"
  7. ]
  8. y_axis = [
  9. "Saturday", "Friday", "Thursday", "Wednesday", "Tuesday", "Monday", "Sunday"
  10. ]
  11. data = [
  12. [0, 0, 5], [0, 1, 1], [0, 2, 0], [0, 3, 0], [0, 4, 0], [0, 5, 0],
  13. [0, 6, 0], [0, 7, 0], [0, 8, 0], [0, 9, 0], [0, 10, 0], [0, 11, 2],
  14. [0, 12, 4], [0, 13, 1], [0, 14, 1], [0, 15, 3], [0, 16, 4], [0, 17, 6],
  15. [0, 18, 4], [0, 19, 4], [0, 20, 3], [0, 21, 3], [0, 22, 2], [0, 23, 5],
  16. [1, 0, 7], [1, 1, 0], [1, 2, 0], [1, 3, 0], [1, 4, 0], [1, 5, 0],
  17. [1, 6, 0], [1, 7, 0], [1, 8, 0], [1, 9, 0], [1, 10, 5], [1, 11, 2],
  18. [1, 12, 2], [1, 13, 6], [1, 14, 9], [1, 15, 11], [1, 16, 6], [1, 17, 7],
  19. [1, 18, 8], [1, 19, 12], [1, 20, 5], [1, 21, 5], [1, 22, 7], [1, 23, 2],
  20. [2, 0, 1], [2, 1, 1], [2, 2, 0], [2, 3, 0], [2, 4, 0], [2, 5, 0],
  21. [2, 6, 0], [2, 7, 0], [2, 8, 0], [2, 9, 0], [2, 10, 3], [2, 11, 2],
  22. [2, 12, 1], [2, 13, 9], [2, 14, 8], [2, 15, 10], [2, 16, 6], [2, 17, 5],
  23. [2, 18, 5], [2, 19, 5], [2, 20, 7], [2, 21, 4], [2, 22, 2], [2, 23, 4],
  24. [3, 0, 7], [3, 1, 3], [3, 2, 0], [3, 3, 0], [3, 4, 0], [3, 5, 0],
  25. [3, 6, 0], [3, 7, 0], [3, 8, 1], [3, 9, 0], [3, 10, 5], [3, 11, 4],
  26. [3, 12, 7], [3, 13, 14], [3, 14, 13], [3, 15, 12], [3, 16, 9], [3, 17, 5],
  27. [3, 18, 5], [3, 19, 10], [3, 20, 6], [3, 21, 4], [3, 22, 4], [3, 23, 1],
  28. [4, 0, 1], [4, 1, 3], [4, 2, 0], [4, 3, 0], [4, 4, 0], [4, 5, 1],
  29. [4, 6, 0], [4, 7, 0], [4, 8, 0], [4, 9, 2], [4, 10, 4], [4, 11, 4],
  30. [4, 12, 2], [4, 13, 4], [4, 14, 4], [4, 15, 14], [4, 16, 12], [4, 17, 1],
  31. [4, 18, 8], [4, 19, 5], [4, 20, 3], [4, 21, 7], [4, 22, 3], [4, 23, 0],
  32. [5, 0, 2], [5, 1, 1], [5, 2, 0], [5, 3, 3], [5, 4, 0], [5, 5, 0],
  33. [5, 6, 0], [5, 7, 0], [5, 8, 2], [5, 9, 0], [5, 10, 4], [5, 11, 1],
  34. [5, 12, 5], [5, 13, 10], [5, 14, 5], [5, 15, 7], [5, 16, 11], [5, 17, 6],
  35. [5, 18, 0], [5, 19, 5], [5, 20, 3], [5, 21, 4], [5, 22, 2], [5, 23, 0],
  36. [6, 0, 1], [6, 1, 0], [6, 2, 0], [6, 3, 0], [6, 4, 0], [6, 5, 0],
  37. [6, 6, 0], [6, 7, 0], [6, 8, 0], [6, 9, 0], [6, 10, 1], [6, 11, 0],
  38. [6, 12, 2], [6, 13, 1], [6, 14, 3], [6, 15, 4], [6, 16, 0], [6, 17, 0],
  39. [6, 18, 0], [6, 19, 0], [6, 20, 1], [6, 21, 2], [6, 22, 2], [6, 23, 6]
  40. ]
  41. range_color = ['#313695', '#4575b4', '#74add1', '#abd9e9', '#e0f3f8', '#ffffbf',
  42. '#fee090', '#fdae61', '#f46d43', '#d73027', '#a50026']
  43. bar3d.add(
  44. "",
  45. x_axis,
  46. y_axis,
  47. [[d[1], d[0], d[2]] for d in data],
  48. is_visualmap=True,
  49. visual_range=[0, 20],
  50. visual_range_color=range_color,
  51. grid3d_width=200,
  52. grid3d_depth=80,
  53. )
  54. bar3d.render()

拖动鼠标还可以转动坐标轴

桑基图

  1. #v1
  2. import json
  3. import os
  4. from pyecharts import options as opts
  5. from pyecharts.charts import Page, Sankey
  6. from pyecharts.faker import Collector
  7. C = Collector()
  8. @C.funcs
  9. def sankey_base() -> Sankey:
  10. nodes = [
  11. {"name": "category1"},
  12. {"name": "category2"},
  13. {"name": "category3"},
  14. {"name": "category4"},
  15. {"name": "category5"},
  16. {"name": "category6"},
  17. ]
  18. links = [
  19. {"source": "category1", "target": "category2", "value": 10},
  20. {"source": "category2", "target": "category3", "value": 15},
  21. {"source": "category3", "target": "category4", "value": 20},
  22. {"source": "category5", "target": "category6", "value": 25},
  23. ]
  24. c = (
  25. Sankey()
  26. .add(
  27. "sankey",
  28. nodes,
  29. links,
  30. linestyle_opt=opts.LineStyleOpts(opacity=0.2, curve=0.5, color="source"),
  31. label_opts=opts.LabelOpts(position="right"),
  32. )
  33. .set_global_opts(title_opts=opts.TitleOpts(title="Sankey-基本示例"))
  34. )
  35. return c
  36. @C.funcs
  37. def sankey_offical() -> Sankey:
  38. with open(os.path.join("fixtures", "energy.json"), "r", encoding="utf-8") as f:
  39. j = json.load(f)
  40. c = (
  41. Sankey()
  42. .add(
  43. "sankey",
  44. nodes=j["nodes"],
  45. links=j["links"],
  46. linestyle_opt=opts.LineStyleOpts(opacity=0.2, curve=0.5, color="source"),
  47. label_opts=opts.LabelOpts(position="right"),
  48. )
  49. .set_global_opts(title_opts=opts.TitleOpts(title="Sankey-官方示例"))
  50. )
  51. return c
  52. @C.funcs
  53. def sankey_vertical() -> Sankey:
  54. colors = [
  55. "#67001f",
  56. "#b2182b",
  57. "#d6604d",
  58. "#f4a582",
  59. "#fddbc7",
  60. "#d1e5f0",
  61. "#92c5de",
  62. "#4393c3",
  63. "#2166ac",
  64. "#053061",
  65. ]
  66. nodes = [
  67. {"name": "a"},
  68. {"name": "b"},
  69. {"name": "a1"},
  70. {"name": "b1"},
  71. {"name": "c"},
  72. {"name": "e"},
  73. ]
  74. links = [
  75. {"source": "a", "target": "a1", "value": 5},
  76. {"source": "e", "target": "b", "value": 3},
  77. {"source": "a", "target": "b1", "value": 3},
  78. {"source": "b1", "target": "a1", "value": 1},
  79. {"source": "b1", "target": "c", "value": 2},
  80. {"source": "b", "target": "c", "value": 1},
  81. ]
  82. c = (
  83. Sankey()
  84. .set_colors(colors)
  85. .add(
  86. "sankey",
  87. nodes=nodes,
  88. links=links,
  89. pos_bottom="10%",
  90. focus_node_adjacency="allEdges",
  91. orient="vertical",
  92. linestyle_opt=opts.LineStyleOpts(opacity=0.2, curve=0.5, color="source"),
  93. label_opts=opts.LabelOpts(position="top"),
  94. )
  95. .set_global_opts(
  96. title_opts=opts.TitleOpts(title="Sankey-Vertical"),
  97. tooltip_opts=opts.TooltipOpts(trigger="item", trigger_on="mousemove"),
  98. )
  99. )
  100. return c
  101. @C.funcs
  102. def sankey_with_level_setting() -> Sankey:
  103. with open(os.path.join("fixtures", "product.json"), "r", encoding="utf-8") as f:
  104. j = json.load(f)
  105. c = (
  106. Sankey()
  107. .add(
  108. "sankey",
  109. nodes=j["nodes"],
  110. links=j["links"],
  111. pos_top="10%",
  112. focus_node_adjacency=True,
  113. levels=[
  114. opts.SankeyLevelsOpts(
  115. depth=0,
  116. itemstyle_opts=opts.ItemStyleOpts(color="#fbb4ae"),
  117. linestyle_opts=opts.LineStyleOpts(color="source", opacity=0.6),
  118. ),
  119. opts.SankeyLevelsOpts(
  120. depth=1,
  121. itemstyle_opts=opts.ItemStyleOpts(color="#b3cde3"),
  122. linestyle_opts=opts.LineStyleOpts(color="source", opacity=0.6),
  123. ),
  124. opts.SankeyLevelsOpts(
  125. depth=2,
  126. itemstyle_opts=opts.ItemStyleOpts(color="#ccebc5"),
  127. linestyle_opts=opts.LineStyleOpts(color="source", opacity=0.6),
  128. ),
  129. opts.SankeyLevelsOpts(
  130. depth=3,
  131. itemstyle_opts=opts.ItemStyleOpts(color="#decbe4"),
  132. linestyle_opts=opts.LineStyleOpts(color="source", opacity=0.6),
  133. ),
  134. ],
  135. linestyle_opt=opts.LineStyleOpts(curve=0.5),
  136. )
  137. .set_global_opts(
  138. title_opts=opts.TitleOpts(title="Sankey-Level Settings"),
  139. tooltip_opts=opts.TooltipOpts(trigger="item", trigger_on="mousemove"),
  140. )
  141. )
  142. return c
  143. Page().add(*[fn() for fn, _ in C.charts]).render()

3D散点图

  1. #v1
  2. import random
  3. from pyecharts import options as opts
  4. from pyecharts.charts import Page, Scatter3D
  5. from pyecharts.faker import Collector, Faker
  6. C = Collector()
  7. @C.funcs
  8. def scatter3d_base() -> Scatter3D:
  9. data = [
  10. [random.randint(0, 100), random.randint(0, 100), random.randint(0, 100)]
  11. for _ in range(80)
  12. ]
  13. c = (
  14. Scatter3D()
  15. .add("", data)
  16. .set_global_opts(
  17. title_opts=opts.TitleOpts("Scatter3D-基本示例"),
  18. visualmap_opts=opts.VisualMapOpts(range_color=Faker.visual_color),
  19. )
  20. )
  21. return c
  22. @C.funcs
  23. def scatter3d_muti_visualmap_channel():
  24. data = [
  25. [random.randint(0, 100), random.randint(0, 100), random.randint(0, 100)]
  26. for _ in range(80)
  27. ]
  28. c = (
  29. Scatter3D()
  30. .add("", data)
  31. .set_global_opts(
  32. title_opts=opts.TitleOpts("Scatter3D-多视觉映射通道"),
  33. visualmap_opts=[
  34. opts.VisualMapOpts(range_color=Faker.visual_color),
  35. opts.VisualMapOpts(type_="size", range_size=[10, 50], pos_top="20%"),
  36. ],
  37. )
  38. )
  39. return c
  40. Page().add(*[fn() for fn, _ in C.charts]).render()

旭日图

  1. #v1
  2. import json
  3. import os
  4. from pyecharts import options as opts
  5. from pyecharts.charts import Page, Sunburst
  6. from pyecharts.faker import Collector
  7. C = Collector()
  8. @C.funcs
  9. def sunburst_base() -> Sunburst:
  10. data = [
  11. opts.SunburstItem(
  12. name="Grandpa",
  13. children=[
  14. opts.SunburstItem(
  15. name="Uncle Leo",
  16. value=15,
  17. children=[
  18. opts.SunburstItem(name="Cousin Jack", value=2),
  19. opts.SunburstItem(
  20. name="Cousin Mary",
  21. value=5,
  22. children=[opts.SunburstItem(name="Jackson", value=2)],
  23. ),
  24. opts.SunburstItem(name="Cousin Ben", value=4),
  25. ],
  26. ),
  27. opts.SunburstItem(
  28. name="Father",
  29. value=10,
  30. children=[
  31. opts.SunburstItem(name="Me", value=5),
  32. opts.SunburstItem(name="Brother Peter", value=1),
  33. ],
  34. ),
  35. ],
  36. ),
  37. opts.SunburstItem(
  38. name="Nancy",
  39. children=[
  40. opts.SunburstItem(
  41. name="Uncle Nike",
  42. children=[
  43. opts.SunburstItem(name="Cousin Betty", value=1),
  44. opts.SunburstItem(name="Cousin Jenny", value=2),
  45. ],
  46. )
  47. ],
  48. ),
  49. ]
  50. c = (
  51. Sunburst(init_opts=opts.InitOpts(width="1000px", height="600px"))
  52. .add(series_name="", data_pair=data, radius=[0, "90%"])
  53. .set_global_opts(title_opts=opts.TitleOpts(title="Sunburst-基本示例"))
  54. .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}"))
  55. )
  56. return c
  57. @C.funcs
  58. def sunburst_official() -> Sunburst:
  59. with open(os.path.join("fixtures", "drink.json"), "r", encoding="utf-8") as f:
  60. j = json.load(f)
  61. c = (
  62. Sunburst(init_opts=opts.InitOpts(width="1000px", height="600px"))
  63. .add(
  64. "",
  65. data_pair=j,
  66. highlight_policy="ancestor",
  67. radius=[0, "95%"],
  68. sort_="null",
  69. levels=[
  70. {},
  71. {
  72. "r0": "15%",
  73. "r": "35%",
  74. "itemStyle": {"borderWidth": 2},
  75. "label": {"rotate": "tangential"},
  76. },
  77. {"r0": "35%", "r": "70%", "label": {"align": "right"}},
  78. {
  79. "r0": "70%",
  80. "r": "72%",
  81. "label": {"position": "outside", "padding": 3, "silent": False},
  82. "itemStyle": {"borderWidth": 3},
  83. },
  84. ],
  85. )
  86. .set_global_opts(title_opts=opts.TitleOpts(title="Sunburst-官方示例"))
  87. .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}"))
  88. )
  89. return c
  90. Page().add(*[fn() for fn, _ in C.charts]).render()

Geo(地理坐标系)

  1. #v0.5
  2. from pyecharts import Geo
  3. data = [
  4. ("海门", 90),("鄂尔多斯", 150),("招远", 12),("舟山", 122),("齐齐哈尔", 14),("盐城", 15),
  5. ("赤峰", 16),("青岛", 18),("乳山", 180),("金昌", 19),("泉州", 21),("莱西", 21),
  6. ("日照", 21),("胶南", 220),("南通", 23),("拉萨", 100),("云浮", 24),("梅州", 25)]
  7. geo = Geo("全国主要城市空气质量", "data from pm2.5", title_color="#fff", title_pos="center",
  8. width=1200, height=600, background_color='#404a59')
  9. attr, value = geo.cast(data)
  10. geo.add("", attr, value, visual_range=[0, 200], visual_text_color="#fff", symbol_size=15, is_visualmap=True)
  11. geo.show_config()
  12. geo.render()

左下角可以上下拖动

  1. #v0.5
  2. from pyecharts import Geo
  3. data = [("海门", 9), ("鄂尔多斯", 12), ("招远", 12), ("舟山", 12), ("齐齐哈尔", 14), ("盐城", 15)]
  4. geo = Geo("全国主要城市空气质量", "data from pm2.5", title_color="#fff", title_pos="center",
  5. width=1200, height=600, background_color='#404a59')
  6. attr, value = geo.cast(data)
  7. geo.add("", attr, value, type="effectScatter", is_random=True, effect_scale=5)
  8. geo.show_config()
  9. geo.render()

  1. #v1
  2. from pyecharts import options as opts
  3. from pyecharts.charts import Geo, Page
  4. from pyecharts.faker import Collector, Faker
  5. from pyecharts.globals import ChartType, SymbolType
  6. C = Collector()
  7. @C.funcs
  8. def geo_base() -> Geo:
  9. c = (
  10. Geo()
  11. .add_schema(maptype="china")
  12. .add("geo", [list(z) for z in zip(Faker.provinces, Faker.values())])
  13. .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
  14. .set_global_opts(
  15. visualmap_opts=opts.VisualMapOpts(),
  16. title_opts=opts.TitleOpts(title="Geo-基本示例"),
  17. )
  18. )
  19. return c
  20. @C.funcs
  21. def geo_visualmap_piecewise() -> Geo:
  22. c = (
  23. Geo()
  24. .add_schema(maptype="china")
  25. .add("geo", [list(z) for z in zip(Faker.provinces, Faker.values())])
  26. .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
  27. .set_global_opts(
  28. visualmap_opts=opts.VisualMapOpts(is_piecewise=True),
  29. title_opts=opts.TitleOpts(title="Geo-VisualMap(分段型)"),
  30. )
  31. )
  32. return c
  33. @C.funcs
  34. def geo_effectscatter() -> Geo:
  35. c = (
  36. Geo()
  37. .add_schema(maptype="china")
  38. .add(
  39. "geo",
  40. [list(z) for z in zip(Faker.provinces, Faker.values())],
  41. type_=ChartType.EFFECT_SCATTER,
  42. )
  43. .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
  44. .set_global_opts(title_opts=opts.TitleOpts(title="Geo-EffectScatter"))
  45. )
  46. return c
  47. @C.funcs
  48. def geo_heatmap() -> Geo:
  49. c = (
  50. Geo()
  51. .add_schema(maptype="china")
  52. .add(
  53. "geo",
  54. [list(z) for z in zip(Faker.provinces, Faker.values())],
  55. type_=ChartType.HEATMAP,
  56. )
  57. .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
  58. .set_global_opts(
  59. visualmap_opts=opts.VisualMapOpts(),
  60. title_opts=opts.TitleOpts(title="Geo-HeatMap"),
  61. )
  62. )
  63. return c
  64. @C.funcs
  65. def geo_guangdong() -> Geo:
  66. c = (
  67. Geo()
  68. .add_schema(maptype="广东")
  69. .add(
  70. "geo",
  71. [list(z) for z in zip(Faker.guangdong_city, Faker.values())],
  72. type_=ChartType.HEATMAP,
  73. )
  74. .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
  75. .set_global_opts(
  76. visualmap_opts=opts.VisualMapOpts(),
  77. title_opts=opts.TitleOpts(title="Geo-广东地图"),
  78. )
  79. )
  80. return c
  81. @C.funcs
  82. def geo_lines() -> Geo:
  83. c = (
  84. Geo()
  85. .add_schema(maptype="china")
  86. .add(
  87. "",
  88. [("广州", 55), ("北京", 66), ("杭州", 77), ("重庆", 88)],
  89. type_=ChartType.EFFECT_SCATTER,
  90. color="white",
  91. )
  92. .add(
  93. "geo",
  94. [("广州", "上海"), ("广州", "北京"), ("广州", "杭州"), ("广州", "重庆")],
  95. type_=ChartType.LINES,
  96. effect_opts=opts.EffectOpts(
  97. symbol=SymbolType.ARROW, symbol_size=6, color="blue"
  98. ),
  99. linestyle_opts=opts.LineStyleOpts(curve=0.2),
  100. )
  101. .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
  102. .set_global_opts(title_opts=opts.TitleOpts(title="Geo-Lines"))
  103. )
  104. return c
  105. @C.funcs
  106. def geo_lines_background() -> Geo:
  107. c = (
  108. Geo()
  109. .add_schema(
  110. maptype="china",
  111. itemstyle_opts=opts.ItemStyleOpts(color="#323c48", border_color="#111"),
  112. )
  113. .add(
  114. "",
  115. [("广州", 55), ("北京", 66), ("杭州", 77), ("重庆", 88)],
  116. type_=ChartType.EFFECT_SCATTER,
  117. color="white",
  118. )
  119. .add(
  120. "geo",
  121. [("广州", "上海"), ("广州", "北京"), ("广州", "杭州"), ("广州", "重庆")],
  122. type_=ChartType.LINES,
  123. effect_opts=opts.EffectOpts(
  124. symbol=SymbolType.ARROW, symbol_size=6, color="blue"
  125. ),
  126. linestyle_opts=opts.LineStyleOpts(curve=0.2),
  127. )
  128. .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
  129. .set_global_opts(title_opts=opts.TitleOpts(title="Geo-Lines-background"))
  130. )
  131. return c
  132. Page().add(*[fn() for fn, _ in C.charts]).render()

  1. #v1
  2. import json
  3. import os
  4. from pyecharts import options as opts
  5. from pyecharts.charts import BMap, Page
  6. from pyecharts.commons.utils import JsCode
  7. from pyecharts.faker import Collector, Faker
  8. from pyecharts.globals import BMapType, ChartType
  9. C = Collector()
  10. BAIDU_MAP_AK = os.environ.get("BAIDU_MAP_AK", "FAKE_AK")
  11. @C.funcs
  12. def bmap_base() -> BMap:
  13. c = (
  14. BMap()
  15. .add_schema(baidu_ak=BAIDU_MAP_AK, center=[120.13066322374, 30.240018034923])
  16. .add(
  17. "bmap",
  18. [list(z) for z in zip(Faker.provinces, Faker.values())],
  19. label_opts=opts.LabelOpts(formatter="{b}"),
  20. )
  21. .set_global_opts(title_opts=opts.TitleOpts(title="BMap-基本示例"))
  22. )
  23. return c
  24. @C.funcs
  25. def bmap_heatmap() -> BMap:
  26. c = (
  27. BMap()
  28. .add_schema(baidu_ak=BAIDU_MAP_AK, center=[120.13066322374, 30.240018034923])
  29. .add(
  30. "bmap",
  31. [list(z) for z in zip(Faker.provinces, Faker.values())],
  32. type_="heatmap",
  33. label_opts=opts.LabelOpts(formatter="{b}"),
  34. )
  35. .set_global_opts(
  36. title_opts=opts.TitleOpts(title="BMap-热力图"),
  37. visualmap_opts=opts.VisualMapOpts(),
  38. )
  39. )
  40. return c
  41. @C.funcs
  42. def bmap_lines() -> BMap:
  43. with open(
  44. os.path.join("fixtures", "hangzhou-tracks.json"), "r", encoding="utf-8"
  45. ) as f:
  46. j = json.load(f)
  47. c = (
  48. BMap()
  49. .add_schema(
  50. baidu_ak=BAIDU_MAP_AK,
  51. center=[120.13066322374, 30.240018034923],
  52. zoom=14,
  53. is_roam=True,
  54. map_style={
  55. "styleJson": [
  56. {
  57. "featureType": "water",
  58. "elementType": "all",
  59. "stylers": {"color": "#d1d1d1"},
  60. },
  61. {
  62. "featureType": "land",
  63. "elementType": "all",
  64. "stylers": {"color": "#f3f3f3"},
  65. },
  66. {
  67. "featureType": "railway",
  68. "elementType": "all",
  69. "stylers": {"visibility": "off"},
  70. },
  71. {
  72. "featureType": "highway",
  73. "elementType": "all",
  74. "stylers": {"color": "#fdfdfd"},
  75. },
  76. {
  77. "featureType": "highway",
  78. "elementType": "labels",
  79. "stylers": {"visibility": "off"},
  80. },
  81. {
  82. "featureType": "arterial",
  83. "elementType": "geometry",
  84. "stylers": {"color": "#fefefe"},
  85. },
  86. {
  87. "featureType": "arterial",
  88. "elementType": "geometry.fill",
  89. "stylers": {"color": "#fefefe"},
  90. },
  91. {
  92. "featureType": "poi",
  93. "elementType": "all",
  94. "stylers": {"visibility": "off"},
  95. },
  96. {
  97. "featureType": "green",
  98. "elementType": "all",
  99. "stylers": {"visibility": "off"},
  100. },
  101. {
  102. "featureType": "subway",
  103. "elementType": "all",
  104. "stylers": {"visibility": "off"},
  105. },
  106. {
  107. "featureType": "manmade",
  108. "elementType": "all",
  109. "stylers": {"color": "#d1d1d1"},
  110. },
  111. {
  112. "featureType": "local",
  113. "elementType": "all",
  114. "stylers": {"color": "#d1d1d1"},
  115. },
  116. {
  117. "featureType": "arterial",
  118. "elementType": "labels",
  119. "stylers": {"visibility": "off"},
  120. },
  121. {
  122. "featureType": "boundary",
  123. "elementType": "all",
  124. "stylers": {"color": "#fefefe"},
  125. },
  126. {
  127. "featureType": "building",
  128. "elementType": "all",
  129. "stylers": {"color": "#d1d1d1"},
  130. },
  131. {
  132. "featureType": "label",
  133. "elementType": "labels.text.fill",
  134. "stylers": {"color": "#999999"},
  135. },
  136. ]
  137. },
  138. )
  139. .add(
  140. "",
  141. type_="lines",
  142. data_pair=j,
  143. is_polyline=True,
  144. is_large=True,
  145. linestyle_opts=opts.LineStyleOpts(color="purple", opacity=0.6, width=1),
  146. )
  147. .add_control_panel(
  148. maptype_control_opts=opts.BMapTypeControlOpts(
  149. type_=BMapType.MAPTYPE_CONTROL_DROPDOWN
  150. ),
  151. scale_control_opts=opts.BMapScaleControlOpts(),
  152. overview_map_opts=opts.BMapOverviewMapControlOpts(is_open=True),
  153. )
  154. .set_global_opts(title_opts=opts.TitleOpts(title="BMap-杭州热门步行路线"))
  155. )
  156. return c
  157. @C.funcs
  158. def bmap_custom() -> BMap:
  159. with open(
  160. os.path.join("fixtures", "bmap-custom-data.json"), "r", encoding="utf-8"
  161. ) as f:
  162. j = json.load(f)
  163. color_list = ["#070093", "#1c3fbf", "#1482e5", "#70b4eb", "#b4e0f3", "#ffffff"]
  164. c = (
  165. BMap()
  166. .add_schema(
  167. baidu_ak=BAIDU_MAP_AK,
  168. center=[116.46, 39.92],
  169. zoom=11.8,
  170. is_roam=True,
  171. map_style={
  172. "styleJson": [
  173. {
  174. "featureType": "water",
  175. "elementType": "all",
  176. "stylers": {"color": "#d1d1d1"},
  177. },
  178. {
  179. "featureType": "land",
  180. "elementType": "all",
  181. "stylers": {"color": "#f3f3f3"},
  182. },
  183. {
  184. "featureType": "railway",
  185. "elementType": "all",
  186. "stylers": {"visibility": "off"},
  187. },
  188. {
  189. "featureType": "highway",
  190. "elementType": "all",
  191. "stylers": {"color": "#999999"},
  192. },
  193. {
  194. "featureType": "highway",
  195. "elementType": "labels",
  196. "stylers": {"visibility": "off"},
  197. },
  198. {
  199. "featureType": "arterial",
  200. "elementType": "geometry",
  201. "stylers": {"color": "#fefefe"},
  202. },
  203. {
  204. "featureType": "arterial",
  205. "elementType": "geometry.fill",
  206. "stylers": {"color": "#fefefe"},
  207. },
  208. {
  209. "featureType": "poi",
  210. "elementType": "all",
  211. "stylers": {"visibility": "off"},
  212. },
  213. {
  214. "featureType": "green",
  215. "elementType": "all",
  216. "stylers": {"visibility": "off"},
  217. },
  218. {
  219. "featureType": "subway",
  220. "elementType": "all",
  221. "stylers": {"visibility": "off"},
  222. },
  223. {
  224. "featureType": "manmade",
  225. "elementType": "all",
  226. "stylers": {"color": "#d1d1d1"},
  227. },
  228. {
  229. "featureType": "local",
  230. "elementType": "all",
  231. "stylers": {"color": "#d1d1d1"},
  232. },
  233. {
  234. "featureType": "arterial",
  235. "elementType": "labels",
  236. "stylers": {"visibility": "off"},
  237. },
  238. {
  239. "featureType": "boundary",
  240. "elementType": "all",
  241. "stylers": {"color": "#fefefe"},
  242. },
  243. {
  244. "featureType": "building",
  245. "elementType": "all",
  246. "stylers": {"color": "#d1d1d1"},
  247. },
  248. {
  249. "featureType": "label",
  250. "elementType": "labels.text.fill",
  251. "stylers": {"color": "rgba(0,0,0,0)"},
  252. },
  253. ]
  254. },
  255. )
  256. .add_js_funcs(
  257. """
  258. var lngExtent = [39.5, 40.6];
  259. var latExtent = [115.9, 116.8];
  260. var cellCount = [50, 50];
  261. var cellSizeCoord = [
  262. (lngExtent[1] - lngExtent[0]) / cellCount[0],
  263. (latExtent[1] - latExtent[0]) / cellCount[1]
  264. ];
  265. var gapSize = 0;
  266. function renderItem(params, api) {
  267. var lngIndex = api.value(0);
  268. var latIndex = api.value(1);
  269. var pointLeftTop = getCoord(params, api, lngIndex, latIndex);
  270. var pointRightBottom = getCoord(params, api, lngIndex + 1, latIndex + 1);
  271. return {
  272. type: 'rect',
  273. shape: {
  274. x: pointLeftTop[0],
  275. y: pointLeftTop[1],
  276. width: pointRightBottom[0] - pointLeftTop[0],
  277. height: pointRightBottom[1] - pointLeftTop[1]
  278. },
  279. style: api.style({
  280. stroke: 'rgba(0,0,0,0.1)'
  281. }),
  282. styleEmphasis: api.styleEmphasis()
  283. };
  284. }
  285. function getCoord(params, api, lngIndex, latIndex) {
  286. var coords = params.context.coords || (params.context.coords = []);
  287. var key = lngIndex + '-' + latIndex;
  288. return coords[key] || (coords[key] = api.coord([
  289. +(latExtent[0] + lngIndex * cellSizeCoord[0]).toFixed(6),
  290. +(lngExtent[0] + latIndex * cellSizeCoord[1]).toFixed(6)
  291. ]));
  292. }
  293. """
  294. )
  295. .add(
  296. series_name="",
  297. data_pair=j["data"],
  298. type_=ChartType.CUSTOM,
  299. render_item=JsCode("renderItem"),
  300. itemstyle_opts=opts.ItemStyleOpts(color="yellow"),
  301. encode={"tooltip": 2},
  302. )
  303. .set_global_opts(
  304. title_opts=opts.TitleOpts(title="BMap-Custom 图"),
  305. tooltip_opts=opts.TooltipOpts(is_show=True, formatter=None),
  306. visualmap_opts=opts.VisualMapOpts(
  307. is_piecewise=True,
  308. pos_top="10",
  309. pos_left="10",
  310. is_inverse=True,
  311. pieces=[
  312. {"value": i, "color": color_list[i]} for i in range(len(color_list))
  313. ],
  314. dimension=2,
  315. border_color="#ccc",
  316. border_width=2,
  317. background_color="#eee",
  318. range_opacity=0.7,
  319. ),
  320. graphic_opts=[
  321. opts.GraphicGroup(
  322. graphic_item=opts.GraphicItem(
  323. rotation=JsCode("Math.PI / 4"),
  324. bounding="raw",
  325. right=110,
  326. bottom=110,
  327. z=100,
  328. ),
  329. children=[
  330. opts.GraphicRect(
  331. graphic_item=opts.GraphicItem(
  332. left="center", top="center", z=100
  333. ),
  334. graphic_shape_opts=opts.GraphicShapeOpts(
  335. width=400, height=50
  336. ),
  337. graphic_basicstyle_opts=opts.GraphicBasicStyleOpts(
  338. fill="rgba(0,0,0,0.3)"
  339. ),
  340. ),
  341. opts.GraphicText(
  342. graphic_item=opts.GraphicItem(
  343. left="center", top="center", z=100
  344. ),
  345. graphic_textstyle_opts=opts.GraphicTextStyleOpts(
  346. text="Made by pyecharts",
  347. font="bold 26px Microsoft YaHei",
  348. graphic_basicstyle_opts=opts.GraphicBasicStyleOpts(
  349. fill="#fff"
  350. ),
  351. ),
  352. ),
  353. ],
  354. )
  355. ],
  356. )
  357. )
  358. return c
  359. Page().add(*[fn() for fn, _ in C.charts]).render()

地图热力图

  1. #v0.5
  2. from pyecharts import Map
  3. value = [155, 10, 66, 78, 33, 80, 190, 53, 49.6]
  4. attr = ["福建", "山东", "北京", "上海", "甘肃", "新疆", "河南", "广西", "西藏"]
  5. map = Map("Map 结合 VisualMap 示例", width=1200, height=600)
  6. map.add("", attr, value, maptype='china', is_visualmap=True, visual_text_color='#000')
  7. map.show_config()
  8. map.render()

左下角可以上下拖动

  1. #v0.5
  2. from pyecharts import Geo,Map
  3. value = [95.1, 23.2, 43.3, 66.4, 88.5]
  4. attr= ["China", "Canada", "Brazil", "Russia", "United States"]
  5. map = Map("世界地图示例", width=1200, height=600)
  6. map.add("", attr, value, maptype="world", is_visualmap=True, visual_text_color='#000')
  7. map.render()

广东地图

  1. #v0.5
  2. from pyecharts import Map
  3. value = [20, 190, 253, 77, 65]
  4. attr = ['汕头市', '汕尾市', '揭阳市', '阳江市', '肇庆市']
  5. map = Map("广东地图示例", width=1200, height=600)
  6. map.add("", attr, value, maptype='广东', is_visualmap=True, visual_text_color='#000')
  7. map.show_config()
  8. map.render()

  1. #v1
  2. import pyecharts.options as opts
  3. from pyecharts.charts import MapGlobe
  4. from pyecharts.faker import POPULATION
  5. def map_globe_base():
  6. data = [x for _, x in POPULATION[1:]]
  7. low, high = min(data), max(data)
  8. mg = (
  9. MapGlobe()
  10. .add_schema()
  11. .add(
  12. maptype="world",
  13. series_name="World Population",
  14. data_pair=POPULATION[1:],
  15. is_map_symbol_show=False,
  16. label_opts=opts.LabelOpts(is_show=False),
  17. )
  18. .set_global_opts(
  19. visualmap_opts=opts.VisualMapOpts(
  20. min_=low,
  21. max_=high,
  22. range_text=["max", "min"],
  23. is_calculable=True,
  24. range_color=["lightskyblue", "yellow", "orangered"],
  25. )
  26. )
  27. )
  28. mg.render()
  29. map_globe_base()

能旋转

  1. #v1
  2. from pyecharts import options as opts
  3. from pyecharts.charts import Map3D, Page
  4. from pyecharts.faker import Collector
  5. from pyecharts.globals import ChartType
  6. from pyecharts.commons.utils import JsCode
  7. C = Collector()
  8. @C.funcs
  9. def map3d_china_base() -> Map3D:
  10. c = (
  11. Map3D()
  12. .add_schema(
  13. itemstyle_opts=opts.ItemStyleOpts(
  14. color="rgb(5,101,123)",
  15. opacity=1,
  16. border_width=0.8,
  17. border_color="rgb(62,215,213)",
  18. ),
  19. map3d_label=opts.Map3DLabelOpts(
  20. is_show=True,
  21. text_style=opts.TextStyleOpts(
  22. color="#fff", font_size=16, background_color="rgba(0,0,0,0)"
  23. ),
  24. ),
  25. emphasis_label_opts=opts.LabelOpts(is_show=True),
  26. light_opts=opts.Map3DLightOpts(
  27. main_color="#fff",
  28. main_intensity=1.2,
  29. is_main_shadow=False,
  30. main_alpha=55,
  31. main_beta=10,
  32. ambient_intensity=0.3,
  33. ),
  34. )
  35. .add(series_name="", data_pair="", maptype=ChartType.MAP3D)
  36. .set_global_opts(
  37. title_opts=opts.TitleOpts(title="全国行政区划地图-Base"),
  38. visualmap_opts=opts.VisualMapOpts(is_show=False),
  39. tooltip_opts=opts.TooltipOpts(is_show=True),
  40. )
  41. )
  42. return c
  43. @C.funcs
  44. def map3d_with_bar3d() -> Map3D:
  45. example_data = [
  46. ("黑龙江", [127.9688, 45.368, 100]),
  47. ("内蒙古", [110.3467, 41.4899, 300]),
  48. ("吉林", [125.8154, 44.2584, 300]),
  49. ("辽宁", [123.1238, 42.1216, 300]),
  50. ("河北", [114.4995, 38.1006, 300]),
  51. ("天津", [117.4219, 39.4189, 300]),
  52. ("山西", [112.3352, 37.9413, 300]),
  53. ("陕西", [109.1162, 34.2004, 300]),
  54. ("甘肃", [103.5901, 36.3043, 300]),
  55. ("宁夏", [106.3586, 38.1775, 300]),
  56. ("青海", [101.4038, 36.8207, 300]),
  57. ("新疆", [87.9236, 43.5883, 300]),
  58. ("西藏", [91.11, 29.97, 300]),
  59. ("四川", [103.9526, 30.7617, 300]),
  60. ("重庆", [108.384366, 30.439702, 300]),
  61. ("山东", [117.1582, 36.8701, 300]),
  62. ("河南", [113.4668, 34.6234, 300]),
  63. ("江苏", [118.8062, 31.9208, 300]),
  64. ("安徽", [117.29, 32.0581, 300]),
  65. ("湖北", [114.3896, 30.6628, 300]),
  66. ("浙江", [119.5313, 29.8773, 300]),
  67. ("福建", [119.4543, 25.9222, 300]),
  68. ("江西", [116.0046, 28.6633, 300]),
  69. ("湖南", [113.0823, 28.2568, 300]),
  70. ("贵州", [106.6992, 26.7682, 300]),
  71. ("广西", [108.479, 23.1152, 300]),
  72. ("海南", [110.3893, 19.8516, 300]),
  73. ("上海", [121.4648, 31.2891, 1300]),
  74. ]
  75. c = (
  76. Map3D()
  77. .add_schema(
  78. itemstyle_opts=opts.ItemStyleOpts(
  79. color="rgb(5,101,123)",
  80. opacity=1,
  81. border_width=0.8,
  82. border_color="rgb(62,215,213)",
  83. ),
  84. map3d_label=opts.Map3DLabelOpts(
  85. is_show=False,
  86. formatter=JsCode(
  87. "function(data){return data.name + " " + data.value[2];}"
  88. ),
  89. ),
  90. emphasis_label_opts=opts.LabelOpts(
  91. is_show=False,
  92. color="#fff",
  93. font_size=10,
  94. background_color="rgba(0,23,11,0)",
  95. ),
  96. light_opts=opts.Map3DLightOpts(
  97. main_color="#fff",
  98. main_intensity=1.2,
  99. main_shadow_quality="high",
  100. is_main_shadow=False,
  101. main_beta=10,
  102. ambient_intensity=0.3,
  103. ),
  104. )
  105. .add(
  106. series_name="bar3D",
  107. data_pair=example_data,
  108. type_=ChartType.BAR3D,
  109. bar_size=1,
  110. shading="lambert",
  111. label_opts=opts.LabelOpts(
  112. is_show=False,
  113. formatter=JsCode(
  114. "function(data){return data.name + ' ' + data.value[2];}"
  115. ),
  116. ),
  117. )
  118. .set_global_opts(title_opts=opts.TitleOpts(title="Map3D-Bar3D"))
  119. )
  120. return c
  121. @C.funcs
  122. def map3d_with_lines3d() -> Map3D:
  123. example_data = [
  124. [[119.107078, 36.70925, 1000], [116.587245, 35.415393, 1000]],
  125. [[117.000923, 36.675807], [120.355173, 36.082982]],
  126. [[118.047648, 36.814939], [118.66471, 37.434564]],
  127. [[121.391382, 37.539297], [119.107078, 36.70925]],
  128. [[116.587245, 35.415393], [122.116394, 37.509691]],
  129. [[119.461208, 35.428588], [118.326443, 35.065282]],
  130. [[116.307428, 37.453968], [115.469381, 35.246531]],
  131. ]
  132. c = (
  133. Map3D()
  134. .add_schema(
  135. maptype="山东",
  136. itemstyle_opts=opts.ItemStyleOpts(
  137. color="rgb(5,101,123)",
  138. opacity=1,
  139. border_width=0.8,
  140. border_color="rgb(62,215,213)",
  141. ),
  142. light_opts=opts.Map3DLightOpts(
  143. main_color="#fff",
  144. main_intensity=1.2,
  145. is_main_shadow=False,
  146. main_alpha=55,
  147. main_beta=10,
  148. ambient_intensity=0.3,
  149. ),
  150. view_control_opts=opts.Map3DViewControlOpts(center=[-10, 0, 10]),
  151. post_effect_opts=opts.Map3DPostEffectOpts(is_enable=False),
  152. )
  153. .add(
  154. series_name="",
  155. data_pair=example_data,
  156. type_=ChartType.LINES3D,
  157. effect=opts.Lines3DEffectOpts(
  158. is_show=True,
  159. period=4,
  160. trail_width=3,
  161. trail_length=0.5,
  162. trail_color="#f00",
  163. trail_opacity=1,
  164. ),
  165. linestyle_opts=opts.LineStyleOpts(is_show=False, color="#fff", opacity=0),
  166. )
  167. .set_global_opts(title_opts=opts.TitleOpts(title="Map3D-Lines3D"))
  168. )
  169. return c
  170. @C.funcs
  171. def map3d_with_scatter3d() -> Map3D:
  172. example_data = [
  173. ("黑龙江", [127.9688, 45.368, 100]),
  174. ("内蒙古", [110.3467, 41.4899, 100]),
  175. ("吉林", [125.8154, 44.2584, 100]),
  176. ("辽宁", [123.1238, 42.1216, 100]),
  177. ("河北", [114.4995, 38.1006, 100]),
  178. ("天津", [117.4219, 39.4189, 100]),
  179. ("山西", [112.3352, 37.9413, 100]),
  180. ("陕西", [109.1162, 34.2004, 100]),
  181. ("甘肃", [103.5901, 36.3043, 100]),
  182. ("宁夏", [106.3586, 38.1775, 100]),
  183. ("青海", [101.4038, 36.8207, 100]),
  184. ("新疆", [87.9236, 43.5883, 100]),
  185. ("西藏", [91.11, 29.97, 100]),
  186. ("四川", [103.9526, 30.7617, 100]),
  187. ("重庆", [108.384366, 30.439702, 100]),
  188. ("山东", [117.1582, 36.8701, 100]),
  189. ("河南", [113.4668, 34.6234, 100]),
  190. ("江苏", [118.8062, 31.9208, 100]),
  191. ("安徽", [117.29, 32.0581, 100]),
  192. ("湖北", [114.3896, 30.6628, 100]),
  193. ("浙江", [119.5313, 29.8773, 100]),
  194. ("福建", [119.4543, 25.9222, 100]),
  195. ("江西", [116.0046, 28.6633, 100]),
  196. ("湖南", [113.0823, 28.2568, 100]),
  197. ("贵州", [106.6992, 26.7682, 100]),
  198. ("广西", [108.479, 23.1152, 100]),
  199. ("海南", [110.3893, 19.8516, 100]),
  200. ("上海", [121.4648, 31.2891, 100]),
  201. ]
  202. c = (
  203. Map3D()
  204. .add_schema(
  205. itemstyle_opts=opts.ItemStyleOpts(
  206. color="rgb(5,101,123)",
  207. opacity=1,
  208. border_width=0.8,
  209. border_color="rgb(62,215,213)",
  210. ),
  211. map3d_label=opts.Map3DLabelOpts(
  212. is_show=False,
  213. formatter=JsCode(
  214. "function(data){return data.name + " " + data.value[2];}"
  215. ),
  216. ),
  217. emphasis_label_opts=opts.LabelOpts(
  218. is_show=False,
  219. color="#fff",
  220. font_size=10,
  221. background_color="rgba(0,23,11,0)",
  222. ),
  223. light_opts=opts.Map3DLightOpts(
  224. main_color="#fff",
  225. main_intensity=1.2,
  226. main_shadow_quality="high",
  227. is_main_shadow=False,
  228. main_beta=10,
  229. ambient_intensity=0.3,
  230. ),
  231. )
  232. .add(
  233. series_name="Scatter3D",
  234. data_pair=example_data,
  235. type_=ChartType.SCATTER3D,
  236. bar_size=1,
  237. shading="lambert",
  238. label_opts=opts.LabelOpts(
  239. is_show=False,
  240. formatter=JsCode(
  241. "function(data){return data.name + ' ' + data.value[2];}"
  242. ),
  243. ),
  244. )
  245. .set_global_opts(title_opts=opts.TitleOpts(title="Map3D-Scatter3D"))
  246. )
  247. return c
  248. Page().add(*[fn() for fn, _ in C.charts]).render()

自定义

  1. #v1
  2. import json
  3. import os
  4. from pyecharts import options as opts
  5. from pyecharts.charts import Page, PictorialBar
  6. from pyecharts.faker import Collector
  7. from pyecharts.globals import SymbolType
  8. C = Collector()
  9. location = ["山西", "四川", "西藏", "北京", "上海", "内蒙古", "云南", "黑龙江", "广东", "福建"]
  10. values = [13, 42, 67, 81, 86, 94, 166, 220, 249, 262]
  11. with open(os.path.join("fixtures", "symbol.json"), "r", encoding="utf-8") as f:
  12. symbols = json.load(f)
  13. @C.funcs
  14. def pictorialbar_base() -> PictorialBar:
  15. c = (
  16. PictorialBar()
  17. .add_xaxis(location)
  18. .add_yaxis(
  19. "",
  20. values,
  21. label_opts=opts.LabelOpts(is_show=False),
  22. symbol_size=18,
  23. symbol_repeat="fixed",
  24. symbol_offset=[0, 0],
  25. is_symbol_clip=True,
  26. symbol=SymbolType.ROUND_RECT,
  27. )
  28. .reversal_axis()
  29. .set_global_opts(
  30. title_opts=opts.TitleOpts(title="PictorialBar-各省份人口数量(虚假数据)"),
  31. xaxis_opts=opts.AxisOpts(is_show=False),
  32. yaxis_opts=opts.AxisOpts(
  33. axistick_opts=opts.AxisTickOpts(is_show=False),
  34. axisline_opts=opts.AxisLineOpts(
  35. linestyle_opts=opts.LineStyleOpts(opacity=0)
  36. ),
  37. ),
  38. )
  39. )
  40. return c
  41. @C.funcs
  42. def pictorialbar_custom_symbol() -> PictorialBar:
  43. c = (
  44. PictorialBar()
  45. .add_xaxis(location)
  46. .add_yaxis(
  47. "",
  48. values,
  49. label_opts=opts.LabelOpts(is_show=False),
  50. symbol_size=22,
  51. symbol_repeat="fixed",
  52. symbol_offset=[0, -5],
  53. is_symbol_clip=True,
  54. symbol=symbols["boy"],
  55. )
  56. .reversal_axis()
  57. .set_global_opts(
  58. title_opts=opts.TitleOpts(title="PictorialBar-自定义 Symbol"),
  59. xaxis_opts=opts.AxisOpts(is_show=False),
  60. yaxis_opts=opts.AxisOpts(
  61. axistick_opts=opts.AxisTickOpts(is_show=False),
  62. axisline_opts=opts.AxisLineOpts(
  63. linestyle_opts=opts.LineStyleOpts(opacity=0)
  64. ),
  65. ),
  66. )
  67. )
  68. return c
  69. @C.funcs
  70. def pictorialbar_multi_custom_symbols() -> PictorialBar:
  71. c = (
  72. PictorialBar()
  73. .add_xaxis(["reindeer", "ship", "plane", "train", "car"])
  74. .add_yaxis(
  75. "2015",
  76. [
  77. {"value": 157, "symbol": symbols["reindeer"]},
  78. {"value": 21, "symbol": symbols["ship"]},
  79. {"value": 66, "symbol": symbols["plane"]},
  80. {"value": 78, "symbol": symbols["train"]},
  81. {"value": 123, "symbol": symbols["car"]},
  82. ],
  83. label_opts=opts.LabelOpts(is_show=False),
  84. symbol_size=22,
  85. symbol_repeat="fixed",
  86. symbol_offset=[0, 5],
  87. is_symbol_clip=True,
  88. )
  89. .add_yaxis(
  90. "2016",
  91. [
  92. {"value": 184, "symbol": symbols["reindeer"]},
  93. {"value": 29, "symbol": symbols["ship"]},
  94. {"value": 73, "symbol": symbols["plane"]},
  95. {"value": 91, "symbol": symbols["train"]},
  96. {"value": 95, "symbol": symbols["car"]},
  97. ],
  98. label_opts=opts.LabelOpts(is_show=False),
  99. symbol_size=22,
  100. symbol_repeat="fixed",
  101. symbol_offset=[0, -25],
  102. is_symbol_clip=True,
  103. )
  104. .reversal_axis()
  105. .set_global_opts(
  106. title_opts=opts.TitleOpts(title="PictorialBar-Vehicles in X City"),
  107. xaxis_opts=opts.AxisOpts(is_show=False),
  108. yaxis_opts=opts.AxisOpts(
  109. axistick_opts=opts.AxisTickOpts(is_show=False),
  110. axisline_opts=opts.AxisLineOpts(
  111. linestyle_opts=opts.LineStyleOpts(opacity=0)
  112. ),
  113. ),
  114. )
  115. )
  116. return c
  117. Page().add(*[fn() for fn, _ in C.charts]).render()

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

闽ICP备14008679号