From 3ede54c75dc3062472f7627a01dbd4db1ebc689f Mon Sep 17 00:00:00 2001 From: pygrv8n62 <2510412192@qq.com> Date: Wed, 9 Oct 2024 10:55:43 +0800 Subject: [PATCH] ADD file via upload --- 图表.py | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 图表.py diff --git a/图表.py b/图表.py new file mode 100644 index 0000000..dac807d --- /dev/null +++ b/图表.py @@ -0,0 +1,74 @@ +import pandas as pd +from pyecharts.charts import Line,Pie,Bar,WordCloud +from pyecharts import options as opts + +# 读取Excel文件 +df = pd.read_excel('携程餐厅数据.xlsx', skiprows=[1]) # 跳过第一行无效数据 + +cuisine_column = '菜系' +score_column = '评分' + +# 计算各菜系的评分平均分 +cuisine_scores = df.groupby(cuisine_column)[score_column].mean().reset_index() +cuisine_scores.columns = ['菜系', '平均分'] + +# 使用Pyecharts绘制折线图 +line = Line() +line.add_xaxis(cuisine_scores['菜系'].tolist()) +line.add_yaxis("平均分", cuisine_scores['平均分'].tolist()) + +# 设置图表标题、工具提示等 +line.set_global_opts( + title_opts=opts.TitleOpts(title="各菜系评分平均分折线图"), + tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="cross"), + xaxis_opts=opts.AxisOpts(type_="category", boundary_gap=False), + yaxis_opts=opts.AxisOpts(type_="value") +) + +# 渲染图表到HTML文件 +line.render('cuisine_scores_percent.html') + +# 计算各菜系的消费次数之和(即评论总数之和) +cuisine_counts = df.groupby('菜系')['评论总数'].sum().reset_index() +cuisine_counts.columns = ['菜系', '消费次数'] + +# 使用Pyecharts绘制饼图 +pie = ( + Pie() + .add( + "", + [list(z) for z in zip(cuisine_counts['菜系'], cuisine_counts['消费次数'])], + radius=["40%", "75%"], # 设置饼图的内外半径 + label_opts=opts.LabelOpts(formatter="{b}: {c} ({d}%)"), # 标签格式,包括菜系名称、消费次数和百分比 + ) + .set_global_opts(title_opts=opts.TitleOpts(title="各菜系消费次数占比")) + # 不需要在这里再设置 label_opts,因为它已经在 add 方法中设置过了 +) + +# 渲染图表到HTML文件 +pie.render('cuisine_consumption_pie_chart.html') + +# 计算每个菜系的人均消费总和和平均消费(假设每个菜系有多条记录) +cuisine_sums = df.groupby('菜系')['人均消费'].sum() +cuisine_counts = df.groupby('菜系')['人均消费'].count() +cuisine_averages = cuisine_sums / cuisine_counts + +# 转换为DataFrame以便使用 +cuisine_df = cuisine_averages.reset_index() +cuisine_df.columns = ['菜系', '平均消费'] + +# 绘制柱状图 +bar = Bar() +bar.add_xaxis(cuisine_df['菜系'].tolist()) +bar.add_yaxis('平均消费', cuisine_df['平均消费'].tolist()) + +# 设置全局配置项 +bar.set_global_opts( + title_opts=opts.TitleOpts(title='各菜系人均消费占比柱状图'), + xaxis_opts=opts.AxisOpts(name='菜系'), + yaxis_opts=opts.AxisOpts(name='平均消费'), + toolbox_opts=opts.ToolboxOpts() +) + +# 渲染图表到HTML文件(可以在浏览器中打开查看) +bar.render('cuisine_average_consumption.html') \ No newline at end of file