本文数据集文末自取
本文基于所获取的东京奥运会相关数据,利用pandas和pyecharts制作静态可视化大屏

地图

数据大屏布局

将上面四个张图表修改为函数,使用Page进行组合图表
完整代码

  1. # https://www.233tw.com/python/59145
  2. import pandas as pd
  3. from pyecharts import options as opts
  4. from pyecharts.charts import *
  5. def map_world()-> Map:
  6. df = pd.read_excel("奖牌榜单数据集.xlsx")
  7. data = [[i,j] for i,j in zip(df['英文名称'], df['奖牌总数'])]
  8. charts = Map()
  9. charts.add("奖章总数", data, "world",is_map_symbol_show=False)
  10. charts.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
  11. charts.set_global_opts(title_opts=opts.TitleOpts(title="2020东京奥运会各国金牌分布图"),
  12. visualmap_opts=opts.VisualMapOpts(max_=120,is_piecewise=True,split_number=3))
  13. return charts
  14. def bar_medals()->Bar:
  15. df = pd.read_excel("奖牌榜单数据集.xlsx")
  16. df = df.sort_values(by="奖牌总数",axis=0,ascending=False)
  17. nation = df['名称'].values[:10][::-1].tolist()
  18. gold = df['金牌'].values[:10][::-1].tolist()
  19. silver = df['银牌'].values[:10][::-1].tolist()
  20. copper = df['铜牌'].values[:10][::-1].tolist()
  21. bar = Bar(init_opts=opts.InitOpts(width='1000px',height='600px'))
  22. bar.add_xaxis(nation)
  23. # stack值一样的系列会堆叠在一起
  24. bar.add_yaxis('金牌', gold, stack='stack1',category_gap="50%",color="#e5b751")
  25. bar.add_yaxis('银牌', silver, stack='stack1',category_gap="50%",color="#f1f0ed")
  26. bar.add_yaxis('铜牌', copper, stack='stack1',category_gap="50%",color="#fed71a")
  27. bar.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
  28. bar.set_global_opts(title_opts=opts.TitleOpts(title="东京奥运会奖牌榜TOP10"))
  29. bar.reversal_axis()
  30. return bar
  31. def pie_china()->Pie:
  32. df = pd.read_excel("参赛运动员数据集.xlsx")
  33. region_list = ["中国", "中国香港", "中国台北"]
  34. titles = []
  35. pie = Pie(
  36. init_opts=opts.InitOpts(
  37. theme='light',
  38. width='1000px',
  39. height='800px',
  40. )
  41. )
  42. for i,r in enumerate(region_list):
  43. sortdata = df[df["国家"]== r].groupby('项目名')['项目名'].count().sort_values(0, ascending=False)
  44. names = sortdata.index.tolist()
  45. values = sortdata.values.tolist()
  46. dataItem = [list(z) for z in zip(names, values)]
  47. pos_x = '{}%'.format(int(i / 3) * 33 + 18)
  48. pos_y = '{}%'.format(i % 3 * 28 + 33)
  49. titles.append(dict(text=r+' ',
  50. left=pos_x,
  51. top=pos_y,
  52. textAlign='center',
  53. textVerticalAlign='middle',
  54. textStyle=dict(color='#603d30',fontSize=12)))
  55. pie.add(r,dataItem,center=[pos_x, pos_y],radius=['8%', '12%'],label_opts=opts.LabelOpts(is_show=True,formatter='{b}:{d}%'))
  56. pie.set_global_opts(legend_opts=opts.LegendOpts(is_show=False),title_opts=titles)
  57. return pie
  58. def timeline()->Line:
  59. df = pd.read_excel("参赛运动员数据集.xlsx")
  60. y_data = []
  61. counter = 0
  62. position = ['left', 'right']
  63. for idx, row in df[(df['英文缩写']=='CHN') & (df['金牌类型']==1)].iterrows():
  64. msg = '{bbb|%s}\n{aaa|%s}\n{bbb|%s/%s}' % (row['获奖时间'][:10], row['运动员'], row['项目名'], row['子项目名称'])
  65. # 单个数据项配置
  66. l_item = opts.LineItem(
  67. name=10,
  68. value=counter,
  69. symbol='emptyCircle',
  70. symbol_size=10,
  71. label_opts=opts.LabelOpts(
  72. is_show=True,
  73. font_size=16,
  74. position=position[counter%2],
  75. formatter=msg,
  76. rich = {
  77. 'aaa': {
  78. 'fontSize': 18,
  79. 'color': 'red',
  80. 'fontWeight':'bold',
  81. 'align':position[(counter+1)%2],
  82. },
  83. 'bbb': {
  84. 'fontSize': 15,
  85. 'color': '#000',
  86. 'align':position[(counter+1)%2]}}
  87. )
  88. )
  89. y_data.append(l_item)
  90. counter+=1
  91. line = Line(
  92. init_opts=opts.InitOpts(
  93. theme='light',
  94. width='1000px',
  95. height='2000px',
  96. bg_color='white'
  97. )
  98. )
  99. line.add_xaxis(
  100. ['CHN']
  101. )
  102. line.add_yaxis(
  103. '',
  104. y_data,
  105. linestyle_opts={
  106. 'normal': {
  107. 'width': 4, # 设置线宽
  108. 'color':'red',
  109. 'shadowColor': 'rgba(155, 18, 184, .3)', # 阴影颜色
  110. 'shadowBlur': 10, # 阴影大小
  111. 'shadowOffsetY': 10, # Y轴方向阴影偏移
  112. 'shadowOffsetX': 10, # x轴方向阴影偏移
  113. }
  114. },
  115. itemstyle_opts={
  116. 'normal': {
  117. 'color':'red',
  118. 'shadowColor': 'rgba(155, 18, 184, .3)', # 阴影颜色
  119. 'shadowBlur': 10, # 阴影大小
  120. 'shadowOffsetY': 10, # Y轴方向阴影偏移
  121. 'shadowOffsetX': 10, # x轴方向阴影偏移
  122. }
  123. },
  124. tooltip_opts=opts.TooltipOpts(is_show=False)
  125. )
  126. line.set_global_opts(
  127. xaxis_opts=opts.AxisOpts(is_show=False, type_='category'),
  128. yaxis_opts=opts.AxisOpts(is_show=False, type_='value', max_=len(y_data)),
  129. title_opts=opts.TitleOpts(
  130. title="夺金时刻", pos_left='center', pos_top='2%',
  131. title_textstyle_opts=opts.TextStyleOpts(color='red', font_size=20)
  132. ),
  133. graphic_opts=[
  134. opts.GraphicGroup(
  135. graphic_item=opts.GraphicItem(id_='1',left="center", top="center", z=-1),
  136. children=[# tokyo
  137. opts.GraphicImage(graphic_item=opts.GraphicItem(id_="logo",
  138. left='center',
  139. z=-1),
  140. graphic_imagestyle_opts=opts.GraphicImageStyleOpts(
  141. image="https://olympics.com/tokyo-2020/en/d3images/emblem/olympics/emblem-tokyo2020.svg",
  142. width=800,
  143. height=1000,
  144. opacity=0.1,)
  145. )
  146. ]
  147. )
  148. ]
  149. )
  150. return line
  151. page = Page(layout=Page.DraggablePageLayout, page_title="2020东京奥运会数据可视化")
  152. # 在页面中添加图表
  153. page.add(
  154. timeline(),
  155. map_world(),
  156. bar_medals(),
  157. pie_china(),
  158. )
  159. page.load_javascript()

效果:

参考资料:
[1] 【2020东京奥运会】数据可视化~
[2] 手把手教你用 pandas 分析可视化东京奥运会数据
[3] Python制作可视化大屏(东京奥运会)