面向?qū)ο髷?shù)據(jù)分析案例(每日銷(xiāo)售額柱狀圖數(shù)據(jù)可視化)
使用工具: Pycharm、面向?qū)ο?、json模塊、pyecharts模塊等
實(shí)現(xiàn)步驟: 讀取數(shù)據(jù)–封裝數(shù)據(jù)對(duì)象–計(jì)算數(shù)據(jù)對(duì)象–pyechars繪圖
(項(xiàng)目數(shù)據(jù)見(jiàn)文章末參考內(nèi)容)
解析思路是通過(guò)定義三個(gè)Python文件,各文件功能如下:
- data_define.py:通過(guò)定義數(shù)據(jù)存儲(chǔ)類(lèi),用來(lái)存儲(chǔ)基本的數(shù)據(jù)類(lèi)以待使用
- file_define.py:通過(guò)定義文件處理類(lèi),抽象類(lèi)約束,具體類(lèi)用來(lái)實(shí)現(xiàn)文本文件、json文件的讀取
- main.py:主文件,實(shí)現(xiàn)具體的數(shù)據(jù)處理,乃至之后的可視化
data_define.py內(nèi)置代碼:
"""
數(shù)據(jù)定義的類(lèi)
"""
class Record:
def __init__(self, date, order_id, money, province):
self.date = date # 訂單日期
self.order_id = order_id # 訂單ID
self.money = money # 訂單金額
self.province = province # 銷(xiāo)售省份
def __str__(self):
return f"record對(duì)象內(nèi)的數(shù)據(jù)包括,訂單日期:{self.date};訂單ID:{self.order_id};訂單金額:{self.money};銷(xiāo)售省份:{self.province}"
file_define.py內(nèi)置代碼:
"""
和文件相關(guān)的類(lèi)定義
"""
from data_define import Record
import json
# 定義一個(gè)抽象類(lèi)用來(lái)做 頂層設(shè)計(jì),確定有哪些功能需要實(shí)現(xiàn)
class FileReader:
def read_data(self) -> list[Record]:
"""讀取文件的數(shù)據(jù),讀到的每一條數(shù)據(jù)都轉(zhuǎn)換為Record對(duì)象,將它們都封裝到list內(nèi)返回即可"""
pass
# 文本文件讀取的具體實(shí)現(xiàn)
class TextFileReader(FileReader):
def __init__(self, path):
self.path = path # 定義成員變量記錄文件的路徑
# 復(fù)寫(xiě)(實(shí)現(xiàn)抽象方法)父類(lèi)的方法
def read_data(self) -> list[Record]:
record_list: list[Record] = []
f = open(self.path, "r", encoding="UTF-8")
for line in f.readlines():
line = line.strip("\n")
data_list = line.split(",")
record = Record(data_list[0], data_list[1], int(data_list[2]), data_list[3])
record_list.append(record)
f.close()
return record_list
# json文件讀取的具體實(shí)現(xiàn)
class JsonFileReader(FileReader):
def __init__(self, path):
self.path = path # 定義成員變量記錄文件的路徑
# 復(fù)寫(xiě)(實(shí)現(xiàn)抽象方法)父類(lèi)的方法
def read_data(self) -> list[Record]:
record_list: list[Record] = []
f = open(self.path, "r", encoding="UTF-8")
for line in f.readlines():
data_dict = json.loads(line)
record = Record(data_dict["date"], data_dict["order_id"], int(data_dict["money"]), data_dict["province"])
record_list.append(record)
f.close()
return record_list
if __name__ == '__main__':
text_file_reader = TextFileReader("2011年1月銷(xiāo)售數(shù)據(jù).txt")
json_file_reader = JsonFileReader("2011年2月銷(xiāo)售數(shù)據(jù)JSON.txt")
list1 = text_file_reader.read_data()
list2 = json_file_reader.read_data()
# for item in list1:
# print(item)
for item in list2:
print(item)
main.py內(nèi)置代碼:
"""
面向?qū)ο?,?shù)據(jù)分析案例,主業(yè)務(wù)邏輯代碼
實(shí)現(xiàn)步驟:
1.設(shè)計(jì)一個(gè)類(lèi),可以完成數(shù)據(jù)的封裝
2.設(shè)計(jì)一個(gè)抽象類(lèi),定義文件讀取的相關(guān)功能,并使用子類(lèi)實(shí)現(xiàn)具體功能
3.讀取文件,生產(chǎn)數(shù)據(jù)對(duì)象
4.進(jìn)行數(shù)據(jù)需求的邏輯計(jì)算(計(jì)算每一天的銷(xiāo)售額)
5.通過(guò)Pyecharts進(jìn)行圖形繪制
"""
from file_define import FileReader, TextFileReader, JsonFileReader
from data_define import Record
from pyecharts.charts import Bar
from pyecharts.options import *
from pyecharts.globals import ThemeType
text_file_reader = TextFileReader("2011年1月銷(xiāo)售數(shù)據(jù).txt")
json_file_reader = JsonFileReader("2011年2月銷(xiāo)售數(shù)據(jù)JSON.txt")
jan_data: list[Record] = text_file_reader.read_data()
feb_data: list[Record] = json_file_reader.read_data()
# 將2個(gè)月份的數(shù)據(jù)合并為1個(gè)list來(lái)存儲(chǔ)
all_data: list[Record] = jan_data + feb_data
# 開(kāi)始進(jìn)行數(shù)據(jù)計(jì)算
# 最終存儲(chǔ)的格式為:{"2011-01-01": 1534, "2011-01-02": 300, "2011-01-03": 650}
data_dict = {}
for record in all_data:
if record.date in data_dict.keys():
# 當(dāng)前日期已經(jīng)有記錄了,所以和老記錄做累加即可
data_dict[record.date] += record.money
else:
data_dict[record.date] = record.money
# 可視化圖表開(kāi)發(fā)
bar = Bar(init_opts=InitOpts(theme=ThemeType.LIGHT))
bar.add_xaxis(list(data_dict.keys())) # 添加x軸的數(shù)據(jù)
bar.add_yaxis("銷(xiāo)售額", list(data_dict.values()), label_opts=LabelOpts(is_show=False)) # 添加y軸的數(shù)據(jù)
bar.set_global_opts(
title_opts=TitleOpts(title="每日銷(xiāo)售額")
)
bar.render("每日銷(xiāo)售額柱狀圖.html")
效果圖如下:
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-479889.html
參考內(nèi)容:
項(xiàng)目數(shù)據(jù)(https://download.csdn.net/download/qq_45833373/87895996)
python語(yǔ)法-面向?qū)ο螅?lèi)的基本使用)
python語(yǔ)法-數(shù)據(jù)可視化(全球GDP動(dòng)態(tài)柱狀圖開(kāi)發(fā))
黑馬程序員-python基礎(chǔ)文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-479889.html
到了這里,關(guān)于python語(yǔ)法-面向?qū)ο髷?shù)據(jù)分析案例(每日銷(xiāo)售額柱狀圖數(shù)據(jù)可視化)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!