返回小栈
关于pyecharts可视化动态效果和美化布局中国经济、人口等数据
bq_wang2020-02-21 09:17:00

pyecharts提供了一些多坐标轴,图表组合功能,动画播放功能;

在本文中第一段代码是通过geo展现三千多个地级市、县级市的热力图,第二段代码是播放从1950年-2010年各省直辖市自治区GDP的发展情况;第三段代码是以表格方式展现六大行政区的一二三产业数据;第四段代码通过grid来调整标题和图例的布局,使报表更加美观;第五段代码是把4个报表在一个页面中展现出来。


代码示例

i

  1. import cx_Oracle

  2. import pandas as pd

  3. import os

  4. import numpy as np

  5. import random

  6. from collections import OrderedDict

  7. import json


  8. from pyecharts import options as opts

  9. from pyecharts.faker import Faker

  10. from pyecharts.commons.utils import JsCode

  11. from pyecharts.globals import SymbolType

  12. from pyecharts.globals import ThemeType


  13. from pyecharts.charts import Funnel,Gauge,Graph,Liquid,Parallel,Pie,Polar,Radar,Sankey,Sunburst,ThemeRiver,WordCloud,Gauge, Page,Timeline,Map,Geo

  14. from pyecharts import options as opts

  15. from pyecharts.charts import Page, ThemeRiver

  16. from pyecharts.globals import ChartType, SymbolType


  17. import json


  18. username = 'metadata'

  19. password = 'Wbq197711'

  20. connstr ='127.0.0.1:1521/ORCL'

  21. conn = cx_Oracle.connect(username,password,connstr) #连接数据库

  22. sql='''

  23. select b.areaname,b.echartprovincename,a.gdp

  24. from chinaeconomy a,proviceinfo b

  25. where a.provincename=b.provincename

  26. and a.reportyear=2010

  27. order by 1


  28. with open(os.path.join("", "city_coordinates.json"), "r", encoding="utf-8") as f:

  29. j = json.load(f)

  30. # {

  31. # "阿城": [

  32. # 126.58,

  33. # 45.32

  34. # ],

  35. # "阿克苏": [

  36. # 80.19,

  37. # 41.09

  38. # ],

  39. # 生成全国城市的随机数组

  40. provincename=['广东','江苏','山东','浙江','河南','河北','辽宁','四川','上海','湖南','重庆',

  41. '湖北','福建','北京','安徽','陕西','广西','江西','天津','山西','吉林','云南',

  42. '新疆','贵州','甘肃','海南','宁夏','青海','西藏','内蒙古','黑龙江']

  43. citydatas=[[city,random.randrange(1,1000)] for city in j if city not in provincename]


  44. def geo_heatmap() -> Geo:

  45. c = (

  46. Geo()

  47. .add_schema(maptype="china")

  48. .add(

  49. "geo",

  50. citydatas,

  51. type_=ChartType.HEATMAP,

  52. )

  53. .set_series_opts(label_opts=opts.LabelOpts(is_show=False))

  54. .set_global_opts(

  55. visualmap_opts=opts.VisualMapOpts(max_=2000),

  56. title_opts=opts.TitleOpts(title="Geo-HeatMap"),

  57. )

  58. )

  59. return c

  60. c=geo_heatmap()

  61. c.render('all_contry_city.html')

  62. #

  63. # print(citydatas)

  64. # def map_visualmap() -> Map:

  65. # c = (

  66. # Map()

  67. # .add("商家A",citydatas, "china")

  68. # .set_global_opts(

  69. # title_opts=opts.TitleOpts(title="Map-VisualMap(连续型)"),

  70. # visualmap_opts=opts.VisualMapOpts(max_=1000),

  71. # )

  72. # )

  73. # return c

  74. # c=map_visualmap()

  75. # c.render('all_contry_city_map.html')


  76. sql='''

  77. select b.echartprovincename,a.reportyear,

  78. a.gdp as population

  79. from chinaeconomy a,proviceinfo b

  80. where a.provincename=b.provincename

  81. and a.reportyear>=1952

  82. order by 2

  83. '''

  84. df = pd.read_sql(sql,conn) #read_sql读取数据

  85. xdata=df['ECHARTPROVINCENAME'].tolist()

  86. population=df['POPULATION'].round(2).tolist() #四舍五入,再转list

  87. reportyear=df['REPORTYEAR'].tolist()


  88. minpopulation=min(population)

  89. maxpopulation=max(population)

  90. alldata=[list(z) for z in zip(reportyear,xdata, population)]


  91. provicepiece=[{"min": 50000, "label": ">=50000"},

  92. {"min": 20000, "max": 49999, "label": "20000 - 19999"},

  93. {"min": 10000, "max": 19999, "label": "10000 - 19999"},

  94. {"min": 5000, "max": 9999, "label": "5000 - 9999"},

  95. {"min": 2000, "max": 4999, "label": "2000 - 4999"},

  96. {"min": 1000, "max": 1999, "label": "1000 - 1999"},

  97. {"min": 500,"max": 999, "label": "500 - 999"},

  98. {"min": 200, "max": 499, "label": "200 - 499"},

  99. {"min": 100, "max": 199, "label": "100 - 199"},

  100. {"min": 50, "max": 99, "label": "50 - 99"},

  101. {"min": 20, "max": 49, "label": "20 - 49"},

  102. {"min": 10, "max": 19, "label": "10 - 19"},

  103. {"min": 5, "max": 9, "label": "5 - 9"},

  104. {"min": 1,"max": 4, "label": "1 - 4"},

  105. {"max": , "label": "空缺"}

  106. ]

  107. BAIDU_MAP_AK='ZMtqRARPIk09nUpWnNKSmMPBVpwTvsci'

  108. def timeline_bmap() -> Timeline:

  109. tl = Timeline()

  110. tl.add_schema(pos_left="50%", pos_right="10px", pos_bottom="15px")

  111. for i in range(1952, 2010):

  112. bmap = (

  113. # BMap()

  114. # .add_schema(baidu_ak=BAIDU_MAP_AK, center=[120.13066322374, 30.240018034923])

  115. # .add(

  116. # "bmap",

  117. # [[curdata[1], curdata[2]] for curdata in alldata if curdata[] == i],

  118. # )

  119. # .set_global_opts(

  120. # title_opts=opts.TitleOpts(title="Timeline-BMap-热力图-{}年".format(i)),

  121. # visualmap_opts=opts.VisualMapOpts(max_=maxpopulation,is_piecewise=True,pieces=provicepiece,

  122. # pos_bottom="center", pos_right="10px"

  123. # ),

  124. # tooltip_opts=opts.TooltipOpts(formatter=None),

  125. # )

  126. # )

  127. Map()

  128. .add("商家A", [[curdata[1], curdata[2]] for curdata in alldata if curdata[] == i], "china")

  129. .set_global_opts(

  130. title_opts=opts.TitleOpts(title="Map-VisualMap(分段型)"),

  131. visualmap_opts=opts.VisualMapOpts(max_=maxpopulation, is_piecewise=True,pieces=provicepiece),

  132. )

  133. )

  134. tl.add(bmap, "{}年".format(i))

  135. return tl

  136. c=timeline_bmap()

  137. c.render('timeline_bmap.html')


  138. from pyecharts.components import Table

  139. from pyecharts.options import ComponentTitleOpts


  140. sql='''

  141. select b.areaname,

  142. sum(a.importexportvalue) as importexportvalue,sum(a.exportvalue)/10000 as exportvalue,sum(a.importexportvalue-a.exportvalue)/10000 as importvalue

  143. from chinaeconomy a,proviceinfo b

  144. where a.provincename=b.provincename

  145. and a.reportyear=2010

  146. group by b.areaname

  147. order by 2

  148. '''

  149. df = pd.read_sql(sql,conn) #read_sql读取数据

  150. headers=['行政区域','出口总额','进口总额','进出口总额']

  151. xdata=df['AREANAME'].tolist()

  152. exportvalue=df['EXPORTVALUE'].round(2).tolist() #四舍五入,再转list

  153. importvalue=df['IMPORTVALUE'].round(2).tolist() #四舍五入,再转list

  154. importexportvalue=df['IMPORTEXPORTVALUE'].round(2).tolist() #四舍五入,再转list

  155. datas=list(zip(xdata,exportvalue,importvalue,importexportvalue))

  156. print(datas)

  157. def table_base() -> Table:

  158. table = Table()


  159. #headers = ["City name", "Area", "Population", "Annual Rainfall"]

  160. rows = datas

  161. table.add(headers, rows).set_global_opts(

  162. title_opts=ComponentTitleOpts(title="Table-我是主标题", subtitle="我是副标题支持换行哦")

  163. )

  164. return table

  165. c=table_base()

  166. c.render('table_base.html')


  167. from pyecharts.faker import Faker

  168. from pyecharts import options as opts

  169. from pyecharts.charts import Bar, Grid, Line,Scatter


  170. # 主要通过grid来调整报表标题

  171. def grid_base() -> Grid:

  172. c = (

  173. Bar()

  174. .add_xaxis(Faker.choose())

  175. .add_yaxis("商家A", Faker.values())

  176. .add_yaxis("商家B", Faker.values())

  177. .set_global_opts(title_opts=opts.TitleOpts(title="Bar-基本示例", subtitle="我是副标题",

  178. pos_left="center", # 标题位置

  179. pos_top="10%",

  180. title_textstyle_opts=opts.TextStyleOpts(color="green", font_size=30),

  181. ),

  182. legend_opts = opts.LegendOpts(is_show=True, pos_right="10%", pos_top="10%", orient='vertical'))

  183. )


  184. grid=Grid()

  185. # 仅使用pos_top修改相对顶部的位置,标题和图表是分离的

  186. grid.add(c,grid_opts=opts.GridOpts(pos_top="30%"))

  187. return grid

  188. c=grid_base()

  189. c.render('grid_base.html')


  190. def line_base() -> Line:

  191. c = (

  192. Line()

  193. .add_xaxis(Faker.choose())

  194. .add_yaxis("商家A", Faker.values())

  195. .add_yaxis("商家B", Faker.values())

  196. .set_global_opts(title_opts=opts.TitleOpts(title="Line-基本示例"))

  197. )

  198. return c


  199. page = Page(layout=Page.SimplePageLayout)

  200. # 需要自行调整每个 chart height/width,显示效果在不同的显示器上可能不同

  201. page.add(grid_base(), line_base(), timeline_bmap(),table_base())

  202. page.render('DraggablePageLayout.html')


长按二维码关注“追梦IT人”


1
0