我有一个带有一些数据的csv文件,我想使用python代码绘制这些数据的图形,并且我想将绘图保存在同一个csv文件中。
我就是这样插入数据的
import csv
list=[5,6,7,5,8]
csv_file='path/csv_file.csv'
outfile = open(csv_file,'w')
out = csv.writer(outfile)
list.insert(0,'header1')
out.writerows([list])
outfile.close()
我想绘制此数据并将图插入csv文件
1条答案
按热度按时间2nbm6dog1#
如果你想把数据保存到excel文件并插入图像,你应该看看
openpyxl
模块,它允许你编写和编辑excel文件。你可以用
matplotlib
来生成你的图像,假设你有一个变量来保存你的数据(这里我用示例值填充了它),注意你不应该把你的变量叫做list
,因为这是Python中创建列表的内置函数的名字,你会覆盖它,所以我在这里把它叫做l
。现在可以使用matplotlib绘制数据。
然后,您可以使用
io.BytesIO
设置一个buffer对象并写入该对象,而不是将图形保存到图像文件中,从而有效地将图像保存在内存中。现在,使用openpyxl创建一个excel文件,并填写值和图像。