Python中画柱形图分析数据(分析各国红酒的好酒占比数据)

最近有网友求助一个分析红酒数据画柱形图的问题,之前没画过柱形图,所以特意去研究了一下。

分享一下画柱形图的方法,以下是代码:

# -*- 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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值