python 网络爬虫及可视化之实现(六)数据可视化实现

7 数据可视化实现

对爬取得到的图书信息进行数据分析
绘制直方图、 饼图、折线图、词云图

7.1引入要引用的包

这部分代码如果不单独成文件可以不需要

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib as mpl
import wordcloud
import imageio
7.2 win图形初始化

这部分代码根据情况可以全部注释掉

# 在windows中显示需要添加下两行 这部分代码根据情况可以全部注释掉
mpl.use('TKAgg')
plt.style.use('_mpl-gallery')
# 创建一个figure对象
fig = plt.figure()
# 设置左右上下的边距宽度x
fig.subplots_adjust(left=0.12, right=0.9, top=0.8, bottom=0.12)
7.3 导入分析的数据
# 从Excel文件读取数据并生成可视化图形
excel_path = r'畅销程序设计类图书.xlsx'
data_book = pd.read_excel(excel_path)
#删除书名为空的行
data_book=data_book.dropna(subset=['书名'])

# 准备数据
dict_category = {}
for category in data_book['分类']:
    dict_category[category] = dict_category.get(category, 0) + 1
list_category = list(dict_category.items())
list_category.sort(key=lambda x: x[1], reverse=True)
print(list_category)
list_ctype = []  # 分类
list_times = []  # 数量
for t in list_category:
    list_ctype.append(t[0])
    list_times.append(t[1])


# 4.1绘制直方图 电影年份出现次数
print(list_ctype,list_times)
DataVisualizer.make_Histogram(list_ctype,list_times,color=['g','y','m'],title='图书分类与出版数直方图',xlabel='图书分类',ylabel='出版数')

# 4.2绘制饼图
DataVisualizer.make_Pie(list_times,list_ctype,title='图书分类占比图')

# 准备折线图数据
list_plot_type = []
list_plot_times = []
list_category.sort(key=lambda x: int(x[1]))

for t in list_category:
    list_plot_type.append(t[0])
    list_plot_times.append(t[1])

# 4.3绘制折线图
DataVisualizer.make_Plot(list_plot_type,list_plot_times,title='图书分类与出版数折线图',xlabel='图书分类',ylabel='出版数')

#准备折线数据2
dict_pubyear = {}
for pubdate in data_book['出版时间']:
    #出版年份信息
    pubyear=pubdate[0:4]
    dict_pubyear[pubyear] = dict_pubyear.get(pubyear, 0) + 1
list_pubyear = list(dict_pubyear.items())
list_pubyear.sort(key=lambda x: x[0], reverse=False)
print(list_pubyear)

list_years = []  # 分类
list_bookcount = []  # 数量
for t in list_pubyear:
    list_years.append(t[0])
    list_bookcount.append(t[1])
DataVisualizer.make_Plot(list_years,list_bookcount,title='出版年与出版数折线图',xlabel='图书分类',ylabel='出版数')

# 准备词云数据
words = []
mv_type = data_book['书名']
words_temp = jieba.cut(mv_type.str.cat(sep=''))

# 读取停⽤词
with open("stopwords.txt","r",encoding="utf-8") as fp:
    stopwords = [s.rstrip() for s in fp.readlines()]

#去掉切分词语中的停⽤词
for w in words_temp:
    if w not in stopwords:
        words.append(w)

#去停⽤词之后的词频统计结果
frequency = dict(Counter(words))
print(frequency)
# 4.4绘制词云图
DataVisualizer.c_wordcloud(frequency)

7.4 绘制图形
绘制直方图

在这里插入图片描述

绘制饼图

在这里插入图片描述

绘制折线图

在这里插入图片描述

绘制词云图

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值