【折线图】python简单绘制折线图

# coding: utf-8
import sys
import re
import csv
import numpy as np
import random
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
import pylab as pl

if sys.version_info[0] > 2:
    is_py3 = True
else:
    reload(sys)
    sys.setdefaultencoding("utf-8")
    is_py3 = False

def native_word(word, encoding='utf-8'):
    """如果在python2下面使用python3训练的模型,可考虑调用此函数转化一下字符编码"""
    if not is_py3:
        return word.encode(encoding)
    else:
        return word

def native_content(content):
    if not is_py3:
        return content.decode('utf-8')
    else:
        return content
    
def open_file(filename, mode='r'):
    """
    常用文件操作,可在python2和python3间切换.
    mode: 'r' or 'w' for read or write
    """
    if is_py3:
        return open(filename, mode, encoding='utf-8', errors='ignore')
    else:
        return open(filename, mode)
def read_csvfile(filename):
    with open(filename) as csvfile:
        readCSV = csv.reader(csvfile,delimiter=',')  # 读取训练数据
        data = []
        label = []
        emo = []
        for row in readCSV:
            if row[0] == '序号':
                continue
            data.append(row[2])
            label.append(row[1])
            emo.append(int(row[3]))
        return data, label, emo

if __name__ == '__main__':
    # dataframe = pd.DataFrame({'confidence': con, 'featureNum': fea, 'F值': F})
    # dataframe.to_csv('sem.csv', index=True, sep=',',encoding='utf-8')
    data, label, emo = read_csvfile('emotion.csv')
    sta = []
    for i in range(len(label)):
        if label[i] not in sta:
            sta.append(label[i])
    print(len(sta))
    num = [[0, 0, 0, 0, 0, 0, 0] for i in range(len(sta))]
    nn = [1 ,2, 3, 4, 5, 6, 7]
    print(sta)
    for i in range(len(label)):
        for j in range(len(sta)):
            if label[i] == sta[j]:
                num[j][emo[i] - 1] += 1
    print(num)
    # plot1 = pl.plot(fea, F, label='me')  # use pylab to plot x and y
    pl.title("比喻")
    pl.xlabel("emotion")
    pl.ylabel("num")
    # pl.show()
    # dataframe = pd.DataFrame({'confidence': con, 'featureNum': fea, 'F值': F})
    # dataframe.to_csv('sem_svm.csv', index=True, sep=',', encoding='utf-8')
    plot = pl.plot(nn, num[0], label='svm')  # use pylab to plot x and y
    # pl.legend()  # make legend
    pl.show()
    with open('emo.txt', 'w', encoding='utf-8') as f:
        for i in range(len(num)):
            f.write(sta[i])
            f.write('\t')
            for j in range(len(num[i])):
                f.write(str(num[i][j]))
                f.write('\t')
            f.write('\n')
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值