# Data visualization exercise (2)

Time：2022-8-4

### Task 2: multiple pie charts show the proportion of different levels of accommodation in each province

In order to better develop enterprise business and recommend agreed accommodation places that meet their positioning to enterprise customers, enterprise consumption platforms need to analyze the proportion of “three-star / comfortable”, “four-star / high-end” and “five-star / luxury” accommodation places and “other types” accommodation places in the top five provinces in the above title (except the above three categories, other types of accommodation places are classified as “other categories”). Please render with the specified legend according to the data in the specified table.

Detailed description:

Please count the proportion of different levels of accommodation in each province according to the relevant fields such as provinces and stars in the relevant data set in the database, and draw multiple pie charts.

1) According to the relevant fields of the table, make statistics on the proportion of different star rated accommodation places in the top 5 provinces, and print out the name of each province and the proportion of star rated accommodation places;

The print statement format is as follows:

==Province: a= proportion of comfortable accommodation:**===

==Province: a= proportion of high-end accommodation:**===

==Province: a= proportion of luxury accommodation:**===

==Province: b= proportion of comfortable accommodation:**===

==Province: b= proportion of high-end accommodation:**===

==Province: b= proportion of luxury accommodation:**===

……

2) Using the flask framework, combined with echarts, draw multiple pie charts in the composite chart. The proportion of accommodation places at all stars in each province is presented with a pie chart, and the screenshot of the visualization results is saved.

1)ralateLevels.py

``import datetimeimport numpy as npimport pandas as pdfrom sqlalchemy import create_ enginemysql = create_ engine('mysql+mysql connector://root:123456 @127.0.0.1:3306/hotel')df_ orderNum = pd.read_ CSV ('e:\\python job \keshihua\\enterprisecustomize\task2\csv22-06-22\u 1655885919.934801df\u data.csv') SQL ='select Province, star from platform_ rate'df_ platformRate = pd.read_ sql(sql, mysql)df_ starNum = pd.DataFrame({'starNum': []})topFiveProvince = []for i in range(5):    topFiveProvince. append(df_orderNum['province'][i])def change_ Name (x): if x = = 'three star': return 'comfort' elif x = = 'four star': return 'premium' elif x = = 'five star': return 'luxury' elif x = = 'comfort': return 'comfort' elif x = = 'premium': return 'premium' elif x = = 'luxury': return 'luxury' else: return 'other categories'df_ platformRate['star'] = df_ platformRate['star']. apply(change_name)df_ topFiveProvince = pd.DataFrame({'province': [], 'star': []})for i in topFiveProvince:    for j in df_ platformRate.groupby(d``

2）Echarts_pies.html

``Title        fucking window<br>    var myChart = echarts.init(document.getElementById("box"));< br>    var option;< Br > // get data from the backend <br> var province = [{% for item in province%}'{item}}', {% endfor%}]< br>    var star = [{% for item in star %}'{{ item }}', {% endfor %}];< br>    var starNum = [{% for item in starNum %}{{ item }}, {% endfor %}];< br>    var objects = {{ objects|safe }};< Br > < br > // configure graphic parameters <br> option = {< br > title: [< br > {< br > text: 'hotel grade proportion of the top five provinces and cities', <br> top:'25%', <br> left:'35%'<br>}, <br> {< br > subtext: '{{province[0]}}', <br> left:'15%', <br> top:'75%', <br> textalign:'center'<br >}, <br>        {<br>          subtext: '{{province[1]}}',<br>          left: '30%',<br>          top: '75%',<br>          textAlign: 'center'<br>        },<br>        {<br>          subtext: '{{province[2]}}',<br>          left: '45%',<br>          top: '75%',<br>          textAlign: 'cent``
``from flask import Flask, render_ Templateimport pandas as pdimport os# get data names_ last_ csv = os. Listdir (R "e:\python job \keshihua\enterprisechustomize\task2\csv") \print (name\u last\u CSV) name_ last_ csv_ 01 = '\\' + Names_ last_ csv[-1]Name_ last_ csv_ 02 = r'e:\python job \keshihua\enterprisecustomize\task2\csv'+ name_ last_ csv_ 01print(Name_last_csv_02)data = pd.read_ CSV (name_last_csv_02) \ Columns# extract data province = data['province'] tolist()starNum = data['starNum']. tolist()star = data['star']. tolist()item = []objects = []for i in range(len(province)):    item. append(star[i])    item. append(starNum[i])    item. append(province[i])    objects. Append (item) item = [] \create a flask instance app = flask (\u name\u)# Create view @app route("/")def echarts_ 01 (): # data transfer to the front end # return render_ template("Echarts_pies.html", province=province, starNum=starNum, star=star)    return render_ Template ("echarts_pies.html", province=province, starnum=starnum, star=star, objects=objects) \__ name__ == '__ main__':``

## Dry principle of design principle

Dry principle Dry principle, which is described in English as don’t repeat yourself. Don’t repeat yourself. It can also be understood as: don’t write duplicate code. We explain the dry principle from three kinds of code repetition: implementation logic repetition, function semantic repetition and code execution repetition. Realize logical repetition For example, there are two […]