Python中畫柱形圖分析資料(分析各國紅酒的好酒佔比資料)
阿新 • • 發佈:2019-01-07
最近有網友求助一個分析紅酒資料畫柱形圖的問題,之前沒畫過柱形圖,所以特意去研究了一下。
分享一下畫柱形圖的方法,以下是程式碼:
# -*- coding:utf8 -*- import matplotlib.pyplot as plt from numpy import * import random import csv def ParseWineData():#解析資料 countryList = [] pointList = [] with open('winemag-data_first150k.csv') as csvfile: csv_reader = csv.reader(csvfile) # 使用csv.reader讀取csvfile中的檔案 for row in csv_reader: # 將csv 檔案中的資料儲存到birth_data中 countryList.append(row[1]) pointList.append(row[4]) countryList.remove(countryList[0]) pointList.remove(pointList[0]) csvfile.close() # fr = open('winemag-data_first150k.csv')#普通方法讀取資料 # allLinesArr = fr.readlines() # for line in allLinesArr: # line = line.strip() # lineList = line.split(',') # countryList.append(lineList[1]) return countryList, pointList def CreateVocabList(dataSet):#建立資料集的並集 vocabList = [] for i in range(len(dataSet)): if dataSet[i] in vocabList: continue else: if len(dataSet[i]) < 20 and dataSet[i] != '': vocabList.append(dataSet[i]) return vocabList def CountCountryNum(countryList, countryVocabList, pointList):#計算各國家好的紅酒佔比 countryNumList = [0 for _ in range(len(countryVocabList))]#國家出現總次數 pointNinetyFiveList = [0 for _ in range(len(countryVocabList))]#紅酒高於95分次數 precentList = [0 for _ in range(len(countryVocabList))]#95分紅酒在當前國家佔比 for i in range(len(countryList)): for j in range(len(countryVocabList)): if countryList[i] == countryVocabList[j]: countryNumList[j] = countryNumList[j] + 1#計算國家出現次數 if (int(pointList[i])) >= 95: pointNinetyFiveList[j] = pointNinetyFiveList[j] + 1#計算高分紅酒次數 for k in range(len(precentList)): precentList[k] = pointNinetyFiveList[k] *100.0/ countryNumList[k]#計算百分比 return precentList def DrawBarChart(nameList, precentList):#畫柱形圖 randomColorList = [] for i in range(len(nameList)): rNum = str(hex(random.randint(0, 255))) if str(hex(random.randint(0, 255)))==4 else str(hex(random.randint(0, 255)))+"0" gNum = str(hex(random.randint(0, 255))) if str(hex(random.randint(0, 255)))==4 else str(hex(random.randint(0, 255)))+"0" bNum = str(hex(random.randint(0, 255))) if str(hex(random.randint(0, 255)))==4 else str(hex(random.randint(0, 255)))+"0" randomColor = str('#'+rNum[2:4]+gNum[2:4]+bNum[2:4])#建立隨機顏色 randomColorList.append(randomColor) plt.title("95-Point Wine In Countrys") rects = plt.bar(range(len(precentList)), precentList, 1, color = randomColorList[:]) # X軸標題 indexList = [i for i in range(len(precentList))] plt.xticks(indexList, nameList, rotation = 90) plt.xlabel("Countrys") # X軸標籤 plt.ylim(ymax=5.0, ymin=0.0) plt.ylabel("95-Point Wine Precent(%)") # Y軸標籤 for rect in rects: height = rect.get_height() height = float("%.2f" %height) plt.text(rect.get_x() + rect.get_width()*0.6, height+0.15, str(height)+"%" , ha='center', va='bottom', rotation = 90) plt.show() countryList, pointList = ParseWineData() countryVocabList = CreateVocabList(countryList) precentList = CountCountryNum(countryList, countryVocabList, pointList) print countryVocabList DrawBarChart(countryVocabList, precentList)
程式碼很短,只有79行,可以看出python程式碼的特有的簡潔和便捷的功能,還有matplotlib圖形庫的強大之處。
執行以後,畫出柱形圖: