|
| 1 | +# @Time : 2020/5/29 9:20 |
| 2 | +# @Author : Libuda |
| 3 | +# @FileName: analyze.py |
| 4 | +# @Software: PyCharm |
| 5 | +import numpy as np |
| 6 | +import pandas as pd |
| 7 | +import pyecharts.options as opts |
| 8 | +from pyecharts.charts import Line, Bar, Page, Pie |
| 9 | +from pyecharts.commons.utils import JsCode |
| 10 | + |
| 11 | +# 人口数量excel文件保存路径 |
| 12 | +POPULATION_EXCEL_PATH = 'Population of India (2020 and historical).xlsx' |
| 13 | + |
| 14 | +# 读取标准数据 |
| 15 | +DF_STANDARD = pd.read_excel(POPULATION_EXCEL_PATH) |
| 16 | +print(DF_STANDARD) |
| 17 | +# 自定义pyecharts图形背景颜色js |
| 18 | +background_color_js = ( |
| 19 | + "new echarts.graphic.LinearGradient(0, 0, 0, 1, " |
| 20 | + "[{offset: 0, color: '#c86589'}, {offset: 1, color: '#06a7ff'}], false)" |
| 21 | +) |
| 22 | +# 自定义pyecharts图像区域颜色js |
| 23 | +area_color_js = ( |
| 24 | + "new echarts.graphic.LinearGradient(0, 0, 0, 1, " |
| 25 | + "[{offset: 0, color: '#eb64fb'}, {offset: 1, color: '#3fbbff0d'}], false)" |
| 26 | +) |
| 27 | + |
| 28 | + |
| 29 | +def analysis_total(): |
| 30 | + """ |
| 31 | + 分析总人口 |
| 32 | + """ |
| 33 | + # 1、分析总人口,画人口曲线图 |
| 34 | + # 1.1 处理数据 |
| 35 | + x_data = DF_STANDARD['Year'][::-1] |
| 36 | + # 将人口单位转换为万 |
| 37 | + y_data = DF_STANDARD['Population'].map(lambda x: "%.2f" % (x / 10000))[::-1] |
| 38 | + # y_data = DF_STANDARD['Population'][::-1] |
| 39 | + # 1.2 自定义曲线图 |
| 40 | + line = ( |
| 41 | + Line(init_opts=opts.InitOpts(bg_color=JsCode(background_color_js))) |
| 42 | + .add_xaxis(xaxis_data=x_data) |
| 43 | + .add_yaxis( |
| 44 | + series_name="总人口", |
| 45 | + y_axis=y_data, |
| 46 | + is_smooth=True, |
| 47 | + is_symbol_show=True, |
| 48 | + symbol="circle", |
| 49 | + symbol_size=5, |
| 50 | + linestyle_opts=opts.LineStyleOpts(color="#fff"), |
| 51 | + label_opts=opts.LabelOpts(is_show=False, position="top", color="white"), |
| 52 | + itemstyle_opts=opts.ItemStyleOpts( |
| 53 | + color="red", border_color="#fff", border_width=1 |
| 54 | + ), |
| 55 | + tooltip_opts=opts.TooltipOpts(is_show=False), |
| 56 | + areastyle_opts=opts.AreaStyleOpts(color=JsCode(area_color_js), opacity=1), |
| 57 | + ) |
| 58 | + .set_global_opts( |
| 59 | + title_opts=opts.TitleOpts( |
| 60 | + title="印度人口变化(万人)", |
| 61 | + pos_bottom="5%", |
| 62 | + pos_left="center", |
| 63 | + title_textstyle_opts=opts.TextStyleOpts(color="#fff", font_size=16), |
| 64 | + ), |
| 65 | + # x轴相关的选项设置 |
| 66 | + xaxis_opts=opts.AxisOpts( |
| 67 | + type_="category", |
| 68 | + boundary_gap=False, |
| 69 | + axislabel_opts=opts.LabelOpts(margin=30, color="#ffffff63"), |
| 70 | + axisline_opts=opts.AxisLineOpts(is_show=False), |
| 71 | + axistick_opts=opts.AxisTickOpts( |
| 72 | + is_show=True, |
| 73 | + length=25, |
| 74 | + linestyle_opts=opts.LineStyleOpts(color="#ffffff1f"), |
| 75 | + ), |
| 76 | + splitline_opts=opts.SplitLineOpts( |
| 77 | + is_show=False, linestyle_opts=opts.LineStyleOpts(color="#ffffff1f") |
| 78 | + ), |
| 79 | + ), |
| 80 | + # y轴相关选项设置 |
| 81 | + yaxis_opts=opts.AxisOpts( |
| 82 | + type_="value", |
| 83 | + position="left", |
| 84 | + axislabel_opts=opts.LabelOpts(margin=20, color="#ffffff63"), |
| 85 | + axisline_opts=opts.AxisLineOpts( |
| 86 | + linestyle_opts=opts.LineStyleOpts(width=0, color="#ffffff1f") |
| 87 | + ), |
| 88 | + axistick_opts=opts.AxisTickOpts( |
| 89 | + is_show=True, |
| 90 | + length=15, |
| 91 | + linestyle_opts=opts.LineStyleOpts(color="#ffffff1f"), |
| 92 | + ), |
| 93 | + splitline_opts=opts.SplitLineOpts( |
| 94 | + is_show=False, linestyle_opts=opts.LineStyleOpts(color="#ffffff1f") |
| 95 | + ), |
| 96 | + ), |
| 97 | + # 图例配置项相关设置 |
| 98 | + legend_opts=opts.LegendOpts(is_show=False), |
| 99 | + ) |
| 100 | + ) |
| 101 | + # 3、渲染图像,将多个图像显示在一个html中 |
| 102 | + # DraggablePageLayout表示可拖拽 |
| 103 | + page = Page(layout=Page.SimplePageLayout) |
| 104 | + page.add(line) |
| 105 | + # page.add(bar) |
| 106 | + page.render('population_total.html') |
| 107 | + |
| 108 | + |
| 109 | +if __name__ == '__main__': |
| 110 | + analysis_total() |
0 commit comments