python数据写入列表并导出折线图
捕風捉影 人气:2import csv datas = [['商品名称','周一销售','周二销售','周三销售','周四销售','周五销售','周六销售','周日销售'], ['洗衣机', '2', '1', '2', '3','2','4','5'], ['冰箱', '1', '2', '3', '2', '4', '5', '4'], ['空调', '3', '4', '3', '4', '3', '6', '5']] # '商品名称','周一销售','周二销售' ,'周三销售','周四销售','周五销售','周六销售','周日销售' #'洗衣机', '2', '1', '2', '3','2','4','5' #'冰箱', '1', '2', '3', '2', '4', '5', '4' #'空调', '3', '4', '3', '4', '3', '6', '5' #要改成你自己当前工程所在的路径 且创建对应的文件 #['1', '2', '3', '2', '4', '5', '4'], with open('D:/Users/admin/PycharmProjects/pythonProject/sale.csv', 'w', newline='') as csvfile: writer = csv.writer(csvfile) for row in datas: writer.writerow(row)
生成折线图:
import matplotlib.pyplot as plt import numpy as np import matplotlib.pyplot as plt plt.rcParams['font.sans-serif'] = ['SimHei'] # 显示中文标签 plt.rcParams['axes.unicode_minus'] = False # 这两行需要手动设置 x,y=np.loadtxt("D:/Users/admin/PycharmProjects/pythonProject/example_csv.txt", delimiter=",", unpack=True) plt.plot(x,y,label="空调销售数量") plt.xlabel("x") plt.ylabel("y") plt.title("销售折线图") plt.legend() plt.show()
加载全部内容