码迷,mamicode.com
首页 > 其他好文 > 详细

七. 从文件中加载数据

时间:2015-11-05 06:11:59      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:

从文件中加载数据有两种方法,一种是利用内置的csv模块,一种是利用第三方模块numpy

import matplotlib.pyplot as plt
import csv
import numpy as np

# csv 方法
x = []
y = []

with open(‘example.txt‘,‘r‘) as csvfile:
    plots = csv.reader(csvfile, delimiter=‘,‘)
    for row in plots:
        x.append(int(row[0]))
        y.append(int(row[1]))

#numpy 方法
x1, y1 = np.loadtxt(‘example1.txt‘, delimiter=‘,‘, unpack=True)

plt.plot(x,y, label=‘csv method‘)
plt.plot(x1,y1, label=‘numpy method‘)
plt.xlabel(‘x‘)
plt.ylabel(‘y‘)
plt.title(‘Graph\n‘)
plt.legend()
plt.show()

如下图

技术分享

七. 从文件中加载数据

标签:

原文地址:http://www.cnblogs.com/flagub/p/4937927.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!