python读取csv文件数据平滑处理后可视化数据

python读取csv文件数据平滑处理后可视化数据

import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
from matplotlib.font_manager import FontProperties
import csv
 
import numpy as np
from matplotlib import pyplot as plt
from scipy.interpolate import make_interp_spline


# def smooth_xy(lx, ly):
#     """数据平滑处理

#     :param lx: x轴数据,数组
#     :param ly: y轴数据,数组
#     :return: 平滑后的x、y轴数据,数组 x_smooth, y_smooth
#     """
#     x = np.array(lx)
#     y = np.array(ly)
#     x_smooth = np.linspace(x.min(), x.max(), 100)
#     y_smooth = make_interp_spline(x, y)(x_smooth)
#     return x_smooth, y_smooth

'''平滑'''
def smooth_xy(scalars, weight):  # Weight between 0 and 1
    last = scalars[0]  # First value in the plot (first timestep)
    smoothed = list()
    for point in scalars:
        smoothed_val = last * weight + (1 - weight) * point  # Calculate smoothed value
        smoothed.append(smoothed_val)                        # Save it
        last = smoothed_val                                  # Anchor the last smoothed value

    return smoothed
    
'''读取csv文件'''
def readcsv(files):
    csvfile = open(files, 'r')
    plots = csv.reader(csvfile, delimiter=',')
    x = []
    y = []
    #读取csv文件中2,3列的数据,且转化为float类型
    for row in plots:
        y.append(float(row[2])) 
        x.append(float(row[1]))
    return x ,y
 
mpl.rcParams['font.family'] = 'sans-serif'
mpl.rcParams['font.sans-serif'] = 'NSimSun,Times New Roman'
 
plt.figure()
#读取文件
x2,y2=readcsv("xxx.csv")
y2_=smooth_xy(y2, 0.6)

plt.plot(x2, y2, color='gray', label='10%')
plt.plot(x2, y2_, color='red', label='10%')
 
plt.xticks(fontsize=16)
plt.yticks(fontsize=16)
#ylim和xlim的最大最小值根据csv文件的极大极小值决定 
plt.ylim(0,1.5)
plt.xlim(9, 50)

plt.xlabel('epoch',fontsize=20)
plt.ylabel('weight',fontsize=20)
plt.legend(fontsize=16)
plt.show()

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值