最近想用pyecharts做一个简单的可联动的数据大屏。
目前已通过timeline+Grid结合的方式实现了Map图和Pie图的联动显示,但在往Grid()添加Bar()的时候老是报错。
报错信息如下:
通过多次尝试方向,当只导入Pie图和Bar图可以成功,但只导入Map图和Bar图就又是上面这种报错信息,似乎两者有点不对付。
作为猛xin,将目前屎一样代码防止下方,供各位帮忙解决。目的就是想将Bar加入到具有Timeline联动功能的Grid中,谢谢。
import os
import pandas as pd
from pyecharts import options as opts
from pyecharts.charts import Bar, Grid, Line, Liquid, Page, Pie,Timeline,Map,WordCloud
from pyecharts.commons.utils import JsCode
from pyecharts.components import Table
from pyecharts.faker import Faker
from pyecharts.globals import ThemeType
import jieba
from pyecharts import options as opts
from pyecharts.charts import Map, Timeline, Pie
from pyecharts.faker import Faker
os.chdir(r'C:\Users\XX\Desktop')
data_park = pd.DataFrame(pd.read_csv('公园情绪数据库2.csv'))
data_park_need = data_park[['公园名', '公园类','县级名','OID_','评论内', '经度', '纬度', '地理位', 'score', 'keywork', 'ID', '分词', '功能类','交通出', '设施设', '生态环', '空间要', '商业业', '历史文', '面积']]
parkclass_data = data_park_need['score'].groupby([data_park_need['公园类'],data_park_need['县级名']]).mean().reset_index()
parkclass_list = parkclass_data['公园类'].drop_duplicates().reset_index()
parkclass_list = parkclass_list['公园类']
#按类型进行区分
##按类型测算得分情况
parkmap_data = data_park_need['score'].groupby([data_park_need['县级名'],data_park_need['公园类']]).mean().reset_index()
parkmap_data['score'] = parkmap_data['score'].round(3)
##按类型测算排名情况
rank = data_park_need['score'].groupby([data_park_need['公园名'],data_park_need['公园类'],data_park_need['县级名']]).mean().reset_index()
rank_010 = rank.sort_values(by=['score'])
def make_Bar(T_df):
t = (
Bar()
.add_xaxis(list(T_df["公园名"]))
.add_yaxis('得分情况',list(T_df["score"]))
)
print('已完成倒数排行图')
return t
def make_Map(m_df):
data_park_qu = m_df['score'].groupby(m_df['县级名']).mean().reset_index()
c = (
Map(init_opts = opts.InitOpts(chart_id = 'Map',width = "280px",height='160px'))
.add("公园情绪",
[list(z) for z in zip(data_park_qu.县级名, data_park_qu.score)],
"XX",
layout_center = ['20%','30%'],
layout_size = '400',
is_roam = False,
label_opts = opts.LabelOpts(font_size = 10.5),
)
.set_global_opts(
title_opts = opts.TitleOpts(title="XX市中心城区公园",pos_left='center'),
visualmap_opts = opts.VisualMapOpts(min_ = 0.1, max_ = 1),
legend_opts = opts.LegendOpts(is_show = False)
)
)
print('已完成区市县级地图')
return c
def make_pie(b_df):
data_bark = b_df['score'].groupby(b_df['县级名']).mean().reset_index()
pie = (
Pie(init_opts = opts.InitOpts(chart_id = 'pie',width = "280px",height='160px'))
.add(
"中心城区不同类型公园情绪得分",
[list(z) for z in zip(b_df.县级名, b_df.score)],
center=["20%", "75%"],
is_still_show_zero_sum=False,
radius = "30%",
)
.set_global_opts(
legend_opts = opts.LegendOpts(is_show = False)
)
)
print('已完成饼图')
return pie
tl = Timeline(init_opts=opts.InitOpts(width="1388px", height="790px", theme=ThemeType.DARK))
for i in range(0,5):
parkmap = (
make_Map(parkmap_data[parkmap_data['公园类'] == str(parkclass_list[i])])
)
parkpie = (
make_pie(parkmap_data[parkmap_data['公园类'] == str(parkclass_list[i])])
)
parkrank =(
make_Bar(rank_010[rank_010['公园类'] == str(parkclass_list[i])])
)
grid = Grid()
grid.add(parkmap,grid_opts=opts.GridOpts())
grid.add(parkpie,grid_opts=opts.GridOpts())
grid.add(parkrank,grid_opts=opts.GridOpts())
tl.add(grid,"{}".format(str(parkclass_list[i])))
tl.render("timeline_pie.html")