matplotlib可视化

from pymongo import MongoClient
import matplotlib.pyplot as plt
from pylab import *
mpl.rcParams['font.sans-serif'] = ['SimHei']                        #添加汉字/ #用来正常显示中文标签
mpl.rcParams['axes.unicode_minus'] = False                          #用来正常显示负号

def get_data():
    client = MongoClient('localhost', 27017)    # 链接数据库
    db = client['Taoguba']
    name = 'List'
    for i in range(5):
        db_name = name + str(i + 1) + str("_emotion")
        db_emotion = db[db_name]
        news = db_emotion.find()
        number = []
        for a in news:
            if a["Emotion"] > 0.6:
                number.append(1)
            else:
                number.append(0)
        numb = [0, 1]
        emo_num = []
        for i in numb:
            a = number.count(i)
            emo_num.append(a)
            print("  %i  一共出现了%a次!" % (i, a))
        if number.count(0) <= number.count(1):
            print("文档偏积极型!")
        else:
            print("文档偏消极型!")
        print("_______________________")
        emo_pic(db_name, emo_num)

def emo_pic(name, data):
    labels = '消极', '积极'
    fracs = data
    colors = ['yellow', 'red']
    explode = (0, 0.08)     # 偏移量
    plt.subplot(aspect=1)
    plt.pie(fracs, explode=explode, labels=labels, colors=colors, autopct='%.0f%%', shadow=True, radius=1)
    #                                   %.0f%% 整数, %1.1f%% 一位小数
    plt.legend()
    plt.axis('equal')
    savefig("C:/Users/web/ %s.png" % name)
    plt.show()


def main():
    get_data()

if __name__ == '__main__':
    main()

猜你喜欢

转载自blog.csdn.net/luzaofa/article/details/79712328