最近有网友求助一个分析红酒数据画柱形图的问题,之前没画过柱形图,所以特意去研究了一下。
分享一下画柱形图的方法,以下是代码:
# -*- 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])
# pointList.append(row[4])
# countryList.remove(countryList[0])
# pointList.remove(pointList[0])
# fr.close()
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图形库的强大之处。
运行以后,画出柱形图:

数据集winemag-data_first150k.csv来源:http://mail.sina.com.cn/filecenter/download.php?id=ft125bf57020522c59ecfb9c
892



被折叠的 条评论
为什么被折叠?



