Python 数据可视化神器—Pyecharts

前言Echarts 是百度开源的一款数据可视化 JS 工具,数据可视化类型十分丰富,但是得通过导入 js 库在 JAVA Web 项目上运行 。
作为工作中常用 Python 的选手,不能不知道这款数据可视化插件的强大 。那么,能否在 Python 中也能用到 Echarts 的功能呢?寻找中惊喜地发现了 pyecharts,只需在python中安装该模块即可使用 。
 
安装常用的pip安装包一键安装pyecharts
【Python 数据可视化神器—Pyecharts】# pyecharts安装命令:python -m pip install pyechartsPython + pyecharts具体应用结合工作中的项目数据,我选择了 test 项目需求中 hotel_code_new 为 CNSZVS_002,CWSWS_003 对应2019年12个月指标为 RNs 的数据做可视化展示与分析 。
1.Hive数据库查询sqlhive_sql内容如下
# sql中所使用的部分语法为hive sql中常规的语法,与MySQL有所不同,请注意 。select rrrd1.hotel_code_new as hotel_code_new,dda.natural_date as natural_date,nvl(rrrd.room_nights, 0) as room_nights from ( select distinct substr(natural_dt,1,7) as natural_datefrom dws.dws_test_date_calendarwhere dt_year='2019')ddaleft join(select 'CNSZVS_002' hotel_code_newUNION all select'CWSWS_003' hotel_code_new)rrrd1left join(selecthotel_code_new,substr(stay_date,1,7) as stay_date,sum(number_of_room_nights) as room_nightsfrom dwm.dwm_test_resvs_rom_daily_dfwhere dt='2021-10-24'and hotel_code_new in(CNSZVS_002', 'CWSWS_003')and resv_status in('CHECKEDSSSIN','CHECKEDSSSOUT')and substr(stay_date,0,4) = '2019'group by hotel_code_new,substr(stay_date,1,7))rrrdon dda.natural_date = rrrd.stay_dateand rrrd1.hotel_code_new=rrrd.hotel_code_neworder by rrrd.hotel_code_new;2.Python代码实现—柱状图from impala.dbapi import connectimport warnings#数据仓库数据获取准备def hive_connect(sql):warnings.filterwarnings('ignore')config_hive_beta = {'host': '10.7.0.12',#hive的host地址'port': 10000,#hive的端口号'user': 'hive',#hive的username'password': 'hive',#hive的password'database': 'tmp',#hive中需要查询的数据库名'auth_mechanism': 'PLAIN' #hive的hive-site.xml配置文件中获取}conn = connect(**config_hive_beta)cursor = conn.cursor()cursor.execute(sql)hive_all_data = cursor.fetchall()return hive_all_data# all_data = hive_connect(hive_sql)# 通过调用hive_connect方法获取到的数据库查询结果数据如all_data列表所示all_data = [('CNSZVS_002', '2019-01', 0), ('CNSZVS_002', '2019-02', 0), ('CNSZVS_002', '2019-03', 0),('CNSZVS_002', '2019-04', 0), ('CNSZVS_002', '2019-05', 0), ('CNSZVS_002', '2019-06', 2353),('CNSZVS_002', '2019-07', 2939), ('CNSZVS_002', '2019-08', 5148), ('CNSZVS_002', '2019-09', 3850),('CNSZVS_002', '2019-10', 4973), ('CNSZVS_002', '2019-11', 5467), ('CNSZVS_002', '2019-12', 4742),('CWSWS_003', '2019-01', 5914), ('CWSWS_003', '2019-02', 4434), ('CWSWS_003', '2019-03', 6003),('CWSWS_003', '2019-04', 6611), ('CWSWS_003', '2019-05', 6586), ('CWSWS_003', '2019-06', 5840),('CWSWS_003', '2019-07', 6624), ('CWSWS_003', '2019-08', 7001), ('CWSWS_003', '2019-09', 5792),('CWSWS_003', '2019-10', 6898), ('CWSWS_003', '2019-11', 6944), ('CWSWS_003', '2019-12', 5404)]# 从pyecharts模块导入柱状图-Barfrom pyecharts import Bar# 设置横轴行名,这里使用12个月份的英文简称columns = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]# 分别新建2个空list用于存储每个月份对应的RNs的值CNSZVS_002 = []CWSWS_003 = []for i in all_data:if i[0] == 'CNSZVS_002':CNSZVS_002.Append(i[2])elif i[0] == 'CWSWS_003':CWSWS_003.append(i[2])else:pass# 设置柱状图的主标题与副标题bar = Bar("柱状图", "Test需求—2019年的RNs")# 添加柱状图的数据及配置项-求平均值、最大值、最小值bar.add("CNSZVS_002", columns, CNSZVS_002, mark_line=["average"], mark_point=["max", "min"])bar.add("CWSWS_003", columns, CWSWS_003, mark_line=["average"], mark_point=["max", "min"])# 在本py文件同级目录下生成名为render.html的本地文件(默认为.html文件)bar.render()# 也可设置成指定的路径用于保存html文件#bar.render(r"D:bar_render.html")柱状效果图展示

Python 数据可视化神器—Pyecharts


推荐阅读