python读取csv并生成图片

import csv
import cv2 as cv
import numpy as np


filePath = 'F:/zhengwangwork/test csv/11.csv'
# filePath = input("请输入处理文件存放的位置:")
def readcsv():
    # file = open("D:/Work/csv/31.csv")
    # filename=input("请输入要读取文件路径:")
    file = open("F:/csvFile/2.csv")
    # file = open(filename)
    reader = csv.reader(file)
    data = []
    data1 = []
    data2 = []
    data3 = []
    data4 = []
    data5 = []
    a = []
    # 读取文件数据列
    for row in reader:
        # column = eval(row[0])
        column = row[0]
        # min=min(column)
        column1 = row[1]
        column2 = row[2]
        column3 = row[3]
        column4 = row[4]
        column5 = row[5]

        data.append(column)
        data1.append(column1)
        data2.append(column2)
        data3.append(column3)
        data4.append(column4)
        data5.append(column5)
    # 求取第一列的最小值并相减输出
    q = min(data)
    data = [eval(i) - eval(q) for i in data]
    # print(data)
    # 将数据列重新写入csv文件中
    # filePath = 'F:/zhengwangwork/test csv/6.csv'
    rows = zip(data, data1, data2, data3, data4, data5)
    with open(filePath, "w", newline='') as f:
        writer = csv.writer(f)
        for row in rows:
            writer.writerow(row)

def drawpicture():
    # (233,1600,3)分别是宽、高、通道数
    canvas = np.zeros((233, 1600, 3), dtype=np.uint8) + 255
    file = open(filePath)  # 打开csv文件
    reader = csv.reader(file)  # 读取csv文件
    data = list(reader)  # 将csv数据转化为列表
    length_h = len(data)  # 得到数据行数
    lenght_l = len(data[0])  # 得到每行长度
    x = list()
    y = list()
    z = list()
    # 绘制散点图
    for i in range(0, length_h):  # 从第一行开始读取
        x.append(int(eval(data[i][0])))  # 将第一列数据从第一行读取到最后一行付给列表x
        y.append(eval(data[i][2]))  # 将第三列数据从第一行读取到最后一行付给列表y
        z.append(eval(data[i][1]))
    pts = zip(x, y)
    col = []
    dict = {"0":(0,255,255), "1": (0,0,255), "2": (255,192,255),"3": (255,127,255), "4": (255,64,255),
            "5": (127,255,0),
            "6": (127,0,255), "7": (64,0,255), "8": (0,255,0), "9": (0,127,0), "10": (255,192,0),
            "11": (255,127,0), "12": (255,64,0), "13": (127,55,255), "14": (127,0,0), "15": (64,0,0),
            "16": (0,0,127), "17": (255,0,255), "18":( 255,0,0)," 19": (127,240,0), "20": (127,240,255),
            "21": (255,15,255), "22": (255,15,0), "23": (0,0,120), "24": (255,255,255), "25": (255,255,0),
           " 26": (255,240,255), "27": (255,240,0)}
    # print(dict["0"])
    # # 通过循环将不同的通道值对应的颜色加到col数组中,其中data1就是db数组
    for i in range(0, len(z)):
        col.append(dict[str(z[i])])
    i = 0
    # 画出每个点,颜色随机
    for pt in pts:
        print(pt)
        cv.circle(canvas, pt, 2, col[i],-1)
        i = i + 1
    # 展示绘制的图形
    cv.namedWindow('Basic Graph', cv.WINDOW_NORMAL)
    cv.imshow('Basic Graph', canvas)
    # 保存绘制的图像
    cv.imwrite('Basic_Graph.png', canvas)
    cv.waitKey()

if __name__ == '__main__':
    readcsv()
    drawpicture()

你可能感兴趣的:(每日学习,python,numpy,开发语言)