1. 程式人生 > >python 微信群男女比例分析,區域分析,柱狀圖顯示

python 微信群男女比例分析,區域分析,柱狀圖顯示

本文使用python中的itchat模組獲取微信資訊,並用matlab進行資料顯示,這兩個模組沒有的可直接在控制檯 pip install ** 進行安裝,話不多說,先上效果圖

一、男女比例分析

 

二、各個地區的男女數量-柱狀圖

 

三、各個地區的男女數量-橫向柱狀圖(地區標籤比較清晰)

下來一 一上程式碼,做這個主要還是看資料怎麼分析,以及運用下圖表的顯示方式,matlab挺強大的,支援一下

一,餅狀圖程式碼

import itchat, time
import matplotlib.pyplot as plot

def chatProportion():
    itchat.auto_login(True)
    male = female = other = 0
    rName = "群名稱,自己填"
    roomSum = 0
    chatRooms = itchat.search_chatrooms(name=rName)
    if chatRooms is None:
        print("no this:" + rName)
    else:
        chatRoom = itchat.update_chatroom(chatRooms[0]['UserName'], detailedMember=True)
        index = 0
        mem_list = chatRoom['MemberList']
        roomSum = len(mem_list)
        for friend in mem_list:
            dis = friend['DisplayName']
            nick = friend['NickName']
            sex = friend['Sex']
            if sex == 1:
                male += 1
            elif sex == 2:
                female += 1
            else:
                other += 1
            index += 1
            print(index,dis,nick,sex)
    labels = ['男:'+str(male),'女'+str(female),'其他'+str(other)]
    sizes = [male, female, other]
    colors = ['green','red','gray']
    # 幾個分量向外偏移
    explode = (0.1,0.1,0)
    plot.pie(sizes,explode,labels,colors,'%2.0f%%')
    plot.axis('equal')
    plot.legend(loc='upper left', bbox_to_anchor=(-0.1, 1))
    plot.rcParams['font.sans-serif'] = ['Microsoft YaHei']
    plot.rcParams['axes.unicode_minus'] = False
    plot.title("群名:"+str(rName)+"[總人數:"+str(roomSum)+"]\n"+str("(男女比例分析-流年master出品)"))
    plot.grid()
    plot.show()

if __name__ == "__main__":
    chatProportion()

 

二、柱狀圖程式碼

import itchat, time
import matplotlib.pyplot as plt

def getInfo():
    # 測試資料
    # info = []
    # city_list = ["西安","大理","西安","city","西安","西安","","大理"]
    # sex_list = ["男","女","男"]
    # for city in city_list:
        
    #     for sex in sex_list:
    #         per_info = {
    #             'city':None,
    #             'sex':None
    #         }
    #         if city == "":
    #             city = "其他"
            
    #         per_info['city'] = city
    #         per_info['sex'] = sex
    #         info.append(per_info)
    
    # return info

    data_info = []
    itchat.auto_login(True)
    rName = "群名稱,自己填"
    chatRooms = itchat.search_chatrooms(name=rName)
    if chatRooms is None:
        print("no this:" + rName)
    else:
        chatRoom = itchat.update_chatroom(chatRooms[0]['UserName'], detailedMember=True)
        mem_list = chatRoom['MemberList']
        for friend in mem_list:
            sex = friend['Sex']
            city = friend['City']

            per_info = {
                'city':None,
                'sex':None
            }
            if city == "":
                city = "其他"
            per_info['city'] = city
            per_info['sex'] = sex
            data_info.append(per_info)
    return data_info

def cityData():
    info = getInfo()
    city_list = []
    for ereryOne in info:
        city_list.append(ereryOne['city'])
    
    # 歸一去重
    single_list = set(city_list)
    men_arr = []
    women_arr = []
    for single in single_list:
        men_count = 0
        women_count = 0
        for everyOne in info:
            if everyOne['city'] == single:
                if everyOne['sex'] == 1 or everyOne['sex'] == "男":
                    men_count += 1
                else:
                    women_count += 1
        men_arr.append(men_count)
        women_arr.append(women_count)
    
    x_dir = list(range(len(single_list)))
    ax = plt.subplot()
    wid = 0.4
    tick_loc = []
    for i in range(len(single_list)):
        tick_loc.append(i + wid/2)

    # 1 豎向柱狀圖
    # label1 = plt.bar(x_dir, men_arr, width=wid, fc='gray')
    # for rect in label1:
    #     height = rect.get_height()
    #     # .-0.1 : 反向移動0.1
    #     plt.text(rect.get_x()+rect.get_width()/2.-0.1, 1.03*height, "%s" % float(height))

    # # 第二個柱狀圖向右偏移一點
    # for i in range(len(x_dir)):
    #     x_dir[i] += wid
    # label2 = plt.bar(x_dir, women_arr, width=wid, fc='r')
    # for rect in label2:
    #     height = rect.get_height()
    #     plt.text(rect.get_x()+rect.get_width()/2.-0.1, 1.03*height, "%s" % float(height))

    # # 橫向標籤位置
    # ax.set_xticks(tick_loc)
    # # 橫向標籤名稱
    # ax.set_xticklabels(single_list)
    # plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
    # plt.title("男女地區豎向柱狀圖")
    # plt.show()

    # 2 橫向柱狀圖
    label1 = ax.barh(x_dir, men_arr, height=wid, fc='gray')
    for rect in label1:
        w = rect.get_width()
        ax.text(w, rect.get_y()+rect.get_height()/2, '%d'%int(w), ha='left',va='center')
    
    # 第二個柱狀圖向右偏移一點
    for i in range(len(x_dir)):
        x_dir[i] += wid
    label2 = ax.barh(x_dir, women_arr, height=wid, fc='r')
    for rect in label2:
        w = rect.get_width()
        ax.text(w, rect.get_y()+rect.get_height()/2, '%d'%int(w), ha='left',va='center')

    # 縱向標籤位置
    ax.set_yticks(tick_loc)
    # 縱向標籤名稱
    ax.set_yticklabels(single_list)
    plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
    plt.title("男女地區橫向柱狀圖")
    plt.show()

if __name__ == "__main__":
    cityData()