大家好,我是欧K。本期给大家盘点一下中国历届夏季奥运会奖牌获取情况,并用python树图动态展示,希望对你有所帮助。
先看看效果:
1. 导入模块
from pyecharts.charts import Tree from pyecharts import options as opts from pyecharts.commons.utils import JsCode from pyecharts.globals import ThemeType
2. 选择结构
2.1 历年奖牌数据
奖牌数据:
dic = { '2016年': ('China', 26, 18, 26), '2012年': ('China', 38, 27, 24), '2008年': ('China', 51, 21, 28), '2004年': ('China', 32, 18, 14), '2000年': ('China', 28, 16, 14), '1996年': ('China', 17, 22, 12), '1992年': ('China', 16, 22, 15), '1988年': ('China', 5, 11, 12), '1984年': ('China', 15, 8, 9)}
翻阅新中国夏季奥运会历史,最早可上溯到1984年。
2008年,奥运会在中国北京举行,无论是在参加人数、媒体报道、赛事组织、场馆建设等多个方面,北京奥运会都可以说是一届“真正的无与伦比”的奥运会。中国代表团共派出运动员639人,参加了全部28个大项的比赛,创下中国历届奥运参赛人数之最。各项目百花齐放,中国队在赛艇、射箭、帆船帆板、拳击项目中首次拿到金牌,实现历史性突破。最终,中国取得了51金21银28铜的优异成绩,成为奥运历史上首个登上金牌榜首的亚洲国家,创造了中国代表团参加奥运会以来的最好成绩。
2.2 设置颜色样式、节点线条样式
颜色样式:
color_js = """new echarts.graphic.LinearGradient(0, 0, 1, 0, [{offset: 0, color: '#696969'}, {offset: 1, color: '#ed1941'}], false)""" color_js0 = """new echarts.graphic.LinearGradient(0, 0, 1, 0, [{offset: 0, color: '#696969'}, {offset: 1, color: '#FFD700'}], false)""" color_js1 = """new echarts.graphic.LinearGradient(0, 0, 1, 0, [{offset: 0, color: '#696969'}, {offset: 1, color: '#C0C0C0'}], false)""" color_js2 = """new echarts.graphic.LinearGradient(0, 0, 1, 0, [{offset: 0, color: '#696969'}, {offset: 1, color: '#8B4513'}], false)""" color_js3 = """new echarts.graphic.LinearGradient(0, 0, 1, 0, [{offset: 0, color: '#FFFF00'}, {offset: 1, color: '#FF0000'}], false)""" color_js4 = """new echarts.graphic.LinearGradient(0, 0, 1, 0, [{offset: 0, color: '#FFF0F5'}, {offset: 1, color: '#1E90FF'}], false)""" color_function = """ function (params) { if (params.name == '金牌') { return '#FFD700'; } else if (params.name == '银牌') { return '#C0C0C0'; } return '#8B4513'; } """
节点线条样式:
width = 1.5 curveness = 0.9 itemStyle1={'color':JsCode(color_js0)} itemStyle2={'color':JsCode(color_js1)} itemStyle3={'color':JsCode(color_js2)} one_level_style = {'color':JsCode(color_js),'width': width, 'curveness': curveness, 'type': 'solid'} gold_style = {'color':JsCode(color_js0),'width': width, 'curveness': curveness, 'type': 'solid'} silver_style = {'color':JsCode(color_js1),'width': width, 'curveness': curveness, 'type': 'solid'} bronze_style = {'color':JsCode(color_js2),'width': width, 'curveness': curveness, 'type': 'solid'}
2.3 构造父子数据
代码:
data = [{ "name": "", 'symbolSize':'15', 'itemStyle':{ 'color': JsCode(color_js3), 'borderWidth':2, 'opacity':0.9 } , "children": [ { "name": "1984年", 'itemStyle':{'color':JsCode(color_js4)}, 'lineStyle':one_level_style, "children": [ {"name": "金牌", "value": 15,'itemStyle':itemStyle1,'lineStyle':gold_style}, {"name": "银牌", "value": 8,'itemStyle':itemStyle2,'lineStyle':silver_style}, {"name": "铜牌", "value": 9,'itemStyle':itemStyle3,'lineStyle':bronze_style} ] }, { "name": "1988年", 'itemStyle':{'color':JsCode(color_js4)}, 'lineStyle':one_level_style, "children": [ {"name": "金牌", "value": 5,'itemStyle': itemStyle1,'lineStyle':gold_style}, {"name": "银牌", "value": 11,'itemStyle':itemStyle2,'lineStyle':silver_style}, {"name": "铜牌", "value": 12,'itemStyle':itemStyle3,'lineStyle':bronze_style} ] }, { "name": "1992年", 'itemStyle':{'color':JsCode(color_js4)}, 'lineStyle':one_level_style, "children": [ {"name": "金牌", "value": 16,'itemStyle': itemStyle1,'lineStyle':gold_style}, {"name": "银牌", "value": 22,'itemStyle':itemStyle2,'lineStyle':silver_style}, {"name": "铜牌", "value": 15,'itemStyle':itemStyle3,'lineStyle':bronze_style} ] }, { "name": "1996年", 'itemStyle':{'color':JsCode(color_js4)}, 'lineStyle':one_level_style, "children": [ {"name": "金牌", "value": 17,'itemStyle': itemStyle1,'lineStyle':gold_style}, {"name": "银牌", "value": 22,'itemStyle':itemStyle2,'lineStyle':silver_style}, {"name": "铜牌", "value": 12,'itemStyle':itemStyle3,'lineStyle':bronze_style} ] }, { "name": "2000年", 'itemStyle':{'color':JsCode(color_js4)}, 'lineStyle':one_level_style, "children": [ {"name": "金牌", "value": 28,'itemStyle': itemStyle1,'lineStyle':gold_style}, {"name": "银牌", "value": 16,'itemStyle':itemStyle2,'lineStyle':silver_style}, {"name": "铜牌", "value": 14,'itemStyle':itemStyle3,'lineStyle':bronze_style} ] }, { "name": "2004年", 'itemStyle':{'color':JsCode(color_js4)}, 'lineStyle':one_level_style, "children": [ {"name": "金牌", "value": 32,'itemStyle': itemStyle1,'lineStyle':gold_style}, {"name": "银牌", "value": 18,'itemStyle':itemStyle2,'lineStyle':silver_style}, {"name": "铜牌", "value": 14,'itemStyle':itemStyle3,'lineStyle':bronze_style} ] }, { "name": "2008年", 'itemStyle':{'color':JsCode(color_js4)}, 'lineStyle':one_level_style, "children": [ {"name": "金牌", "value": 51,'itemStyle': itemStyle1,'lineStyle':gold_style}, {"name": "银牌", "value": 21,'itemStyle':itemStyle2,'lineStyle':silver_style}, {"name": "铜牌", "value": 28,'itemStyle':itemStyle3,'lineStyle':bronze_style} ] }, { "name": "2012年", 'itemStyle':{'color':JsCode(color_js4)}, 'lineStyle':one_level_style, "children": [ {"name": "金牌", "value": 38,'itemStyle': itemStyle1,'lineStyle':gold_style}, {"name": "银牌", "value": 27,'itemStyle':itemStyle2,'lineStyle':silver_style}, {"name": "铜牌", "value": 24,'itemStyle':itemStyle3,'lineStyle':bronze_style} ] }, { "name": "2016年", 'itemStyle':{'color':JsCode(color_js4)}, 'lineStyle':one_level_style, "children": [ {"name": "金牌", "value": 26,'itemStyle': itemStyle1,'lineStyle':gold_style}, {"name": "银牌", "value": 18,'itemStyle':itemStyle2,'lineStyle':silver_style}, {"name": "铜牌", "value": 26,'itemStyle':itemStyle3,'lineStyle':bronze_style} ] }, ] }]
3. 绘制树图
代码:
t = ( Tree(init_opts=opts.InitOpts(theme = ThemeType.CHALK,width="1000px", height="600px")) .add( "", data, pos_top="18%", pos_bottom="14%", symbol='circle', layout="radial", edge_fork_position="80%", symbol_size=15, label_opts=opts.LabelOpts(position="top",color='#00FFFF'), leaves_label_opts=opts.LabelOpts( position = 'top',distance = 12,vertical_align= 'left', font_size=12,color=JsCode(color_function),formatter='{b}: {c}') ) .set_global_opts( tooltip_opts=opts.TooltipOpts(trigger="item", trigger_on="mousemove"), title_opts=opts.TitleOpts(title="中国历届夏季奥运会奖牌数",pos_left='center',pos_top="5%")) ) t.render_notebook()
效果:
也可以根据需要调整排列格式:
完。
END
以上就是本期为大家整理的全部内容了,赶快练习起来吧,喜欢的朋友可以点赞、点在看也可以分享让更多人知道