# %%
import numpy as np
import pandas as pd
from sqlalchemy import create_engine
from pyecharts.charts import Bar
from pyecharts import options as opts
# %%
engine = create_engine('mysql+pymysql://root:12345678@localhost:3306/db?charset=utf8')
df = pd.read_sql('select * from db.house_data', con=engine)
# %%
# 过滤深圳周边城市的房源
df = df[df['区域'] != '深圳']
df = (df.groupby('区域')
.agg({'单价': np.mean})
.sort_values(by='单价', ascending=False)
.reset_index())
df['单价'] = df['单价'].astype(int)
# %%
# x_data = list(df['区域'])
# y_data = list(df['单价'])
x_data = df['区域'].tolist()
y_data = df['单价'].tolist()
# 再插入一个y轴数据,柱形
# y_data2 = list(df['单价']-10000)
# %%
bar = Bar()
bar.add_xaxis(x_data)
bar.add_yaxis('房价', y_data)
# 再插入一个y轴数据,柱形
# bar.add_yaxis('房价2', y_data2)
bar.set_global_opts(
title_opts=opts.TitleOpts(title='各区平均房价'),
datazoom_opts=opts.DataZoomOpts(is_show=True)
)
bar.render_notebook()
# %%
bar.render('./figure/柱形图.html')
# %%