数据可视化练习(1)
MySQL数据库中的相关数据集包含了城市、省份、评分、评论数等多项基础信息字段。请使用Flask框架,结合Echarts完成下列题目。
数据库账号:root 密码:123456 库名:hotel
自行创建代码工程路径为:C:\s_hotel
任务一:条形图呈现住宿场所订单量
住宿场所订单量是反映住宿场所入住数量的重要指标之一,某省订单数量一定程度上可以反应出该省住宿场所入住情况,为了更好地分析全国各省住宿场所订单量,请根据指定表中数据统计出全国各省住宿场所订单量的情况,并以指定图例进行呈现。
详细描述:
请以数据库中相关表格为数据源,分析并统计各省订单数量,绘制条形图。
具体任务要求:
1)提取表格相关字段,分别统计各省的订单数量,在控制台按照“订单数量”降序排列,打印输出各省份的订单数量;
打印语句格式如下:
==省份:*** =订单数量:**个==
……
2)使用Flask框架,结合Echarts绘制“订单数量”的条形图,标题为“各省订单数量”(字体颜色:红色,加粗),纵坐标为省份名称,横坐标为订单数量,并将可视化结果复制粘贴至对应报告中。
1.路径:orderNum.py
import pandas as pd
import numpy as np
from sqlalchemy import create_engine
from pandas.core.frame import DataFrame
mysql = create_engine('mysql+mysqlconnector://root:123456@127.0.0.1:3306/hotel')
sql = 'select * from platform'
df = pd.read_sql(sql, mysql)
df['order_num'] = df['order_num'].astype(float)
df = df.groupby(df['Province'])
df_province = df['Province']
province = []
for i in df_province:
province.append(i[0])
# print(province)
num_order = []
for i in df['order_num'].sum():
num_order.append(i)
#print(num_order)
data = [province, num_order]
df_data = DataFrame({"Province": province, "num_order": num_order})
# print(df_data)
df_data = df_data.sort_values(by='num_order', ascending=False)
df_data = df_data.reset_index(drop=True)
print(df_data)
for i in range(len(df_data)):
print('==省份:' + df_data['Province'][i] + ' =订单数量:' + str(df_data['num_order'][i]) + '个==')
# filepath = "E:\\Python\\keshihua\\enterpriseCustomize\\task1\\csv\\df_data.csv"
# df_data.to_csv(filepath, index=False, header=False)
2.
(1) Echarts_01.html
<!DOCTYPE html>
<html lang="en" style="height:100%">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="../static/echarts.min.js"></script>
</head>
<body style="height:100%;width:100%">
<!-- 创建div容器用于存放图形 -->
<div id="box" style="width:900px;height: 600px">fucking window</div>
<!-- 引入echarts资源 -->
<script>
var myChart = echarts.init(document.getElementById("box"));
// 从后端获取数据
var province = [{% for item in province %}'{{ item }}',{% endfor %}];
var order_num = [{% for item in order_num %}{{ item }}, {% endfor %}];
// 配置图形参数
option = {
title: {
text: '各省订单数量',
subtext: '',
left: 'center',
textStyle:{
color:'#FF0000',
fontStyle:'normal',
fontWeight:'bolder',
fontFamily:'sans-serif',
fontSize:25
}
},
xAxis: {
type: 'category',
data: province
},
yAxis: {
type: 'value'
},
series: [
{
data: order_num,
type: 'bar',
showBackground: true,
backgroundStyle: {
color: 'rgba(180, 180, 180, 0.2)'
}
}
]
};
// 将配置的参数传递给echarts对象
option && myChart.setOption(option);
</script>
</body>
</html>
from flask import Flask, render_template
import pandas as pd
import os
# 获取数据
Names_last_csv = os.listdir(r"E:\Python作业\keshihua\enterpriseCustomize\task1\csv")
# print(Name_last_csv)
Name_last_csv_01 = '\\' + Names_last_csv[-1]
Name_last_csv_02 = r'E:\Python作业\keshihua\enterpriseCustomize\task1\csv' + Name_last_csv_01
print(Name_last_csv_02)
data = pd.read_csv(Name_last_csv_02)
# 获取数据的列
columns = data.columns
# 提取数据
province = data['province'].tolist()
order_num = data['order_num'].tolist()
# 创建flask实例
app = Flask(__name__)
# 创建视图
@app.route("/")
def echarts_01():
# 数据传输到前端
return render_template("Echarts_01.html", province=province, order_num=order_num)
#
# 运行程序
if __name__ == '__main__':
app.run(debug=True)
(3)目录
![](https://img2022.cnblogs.com/blog/2409071/202206/2409071-20220622164957244-665357727.png)