自用版
数据格式如下:
运行效果如下:
import pandas as pd
from pyecharts.charts import Map, TreeMap, Timeline, Page, WordCloud
from pyecharts import options as opts
from pyecharts.commons.utils import JsCode
from pyecharts.globals import ThemeType
import jieba
import jieba.analyse
import codecs
import math
#存成csv格式
data_path =r"lyjd.csv"
df = pd.read_csv(data_path)
####数据预处理
#按照·进行分隔
df0 = df['省/市/区'].str.split('·', expand=True)
#存入对应的df中
df0.columns=['省', '市', '区','无']
df['省'] = df0['省']
df['市'] = df0['市']
df['区'] = df0['区']
#对不同分类依据进行计数
dfCitySale = df.groupby(['市'])['销量'].sum().reset_index(name='总销量')
dfParkSale = df.groupby(['名称'])['销量'].sum().reset_index(name='景区总销量')
dfParkSale = dfParkSale.sort_values(by='景区总销量', ascending=False)
## 1、 全国销量Top20的热门景点
print(dfParkSale.head(20));
## 2、 全国各省市4A-5A景区数量;(景点分布情况)(可选)
#利用Map进行绘制
def c1() ->Map:
#dfCityCount记录各省市4A-5A的景区数量
dfCityCount = df[df['星级'].isin(['4A','5A'])].groupby(['市'])['名称'].count().reset_index(name='景区总数');
data_city_count = list(zip(dfCityCount['市'],dfCityCount['景区总数']))
#开始绘图
china_city2 = (
Map()
.add(
"景区总数",
data_city_count,
"china-cities",
label_opts=opts.LabelOpts(is_show=False),
)
.set_global_opts(
title_opts=opts.TitleOpts(title="lxy全国各省市4A-5A景区数量"),
visualmap_opts=opts.VisualMapOpts(
min_=1,
max_=20,
is_piecewise=True
),
)
#.render("全国各省市4A-5A景区数量.html")
)
return china_city2
## 3、 全国各省市假期出行数据在地图上的分布;(出游分析及建议)(必做)
def c2() ->Map:
data_city_sale = list(zip(dfCitySale['市'],dfCitySale['总销量']))
china_city3 = (
Map()
.add(
"景区销量",
data_city_sale,
"china-cities",
label_opts=opts.LabelOpts(is_show=False),
)
.set_global_opts(
title_opts=opts.TitleOpts(title="lxy全国各省市假期出行数据在地图上的分布"),
visualmap_opts=opts.VisualMapOpts(
min_=1,
max_=5000,
is_piecewise=True
),
)
#.render("全国各省市假期出行数据在地图上的分布.html")
)
return china_city3
def c3() ->WordCloud :
##4、全国各景点简介文本统计词云图;(景点主要特色)(必做)
rows = pd.read_csv(data_path)
counts = {} # 通过键值对的形式存储词语及其出现的次数
for index, row in rows.iterrows():
content = row['简介']
if pd.isna(content):#如果简介是空值就跳过
continue
#分词
test_list = jieba.lcut(content, cut_all=True)
for word in test_list:
if len(word) == 1: # 单个词语不计算在内
continue
else:
counts[word] = counts.get(word, 0) + 1 # 遍历所有词语,每出现一次其对应的值加 1
items = list(counts.items()) # 将键值对转换成列表
items.sort(key=lambda x: x[1], reverse=True) # 根据词语出现的次数进行从大到小排序
# for i in items:
# word, count = i
# print("{0:<5}{1:>5}".format(word, count))
c = (
WordCloud()
.add("", items, word_size_range=[20, 100])
.set_global_opts(title_opts=opts.TitleOpts(title="lxy景区简介词云图"))
#.render("词云图.html")
)
return c
###利用Page函数将三张图绘制在一张页面上
def creatPage():
page=Page(layout=Page.DraggablePageLayout)
page.add(
c1(),
c2(),
c3()
)
page.render("数据可视化.html")
if __name__ == "__main__":
creatPage()