将txt文件作为数据源 绘制 pyplot 曲线
程序员文章站
2022-06-05 20:14:16
...
import numpy
import matplotlib.pyplot as plt
# Load data from a text file data.txt
data = numpy.loadtxt("data.txt")
# # 6行,三列
print(data)
# print(type(data)) <class 'numpy.ndarray'> nd: n, d:dimensionality
# 三行,六列
print(data.T)
'''
只要维度大于1, 实现就会调转
Same as self.transpose(), except that self is returned if
self.ndim < 2.
Examples
--------
>>> x = np.array([[1.,2.],[3.,4.]])
>>> x
array([[ 1., 2.],
[ 3., 4.]])
>>> x.T
array([[ 1., 3.],
[ 2., 4.]])
>>> x = np.array([1.,2.,3.,4.])
>>> x
array([ 1., 2., 3., 4.])
>>> x.T
array([ 1., 2., 3., 4.])
'''
print('----------data.T----------')
print(data.T)
for row in data.T:
x = data.T[0]
y = row
print('--------xy-----------')
print(x)
print(y)
plt.plot(x, y)
plt.show()
output:
----------data.T----------
[[ 0. 1. 2. 4. 5. 6.]
[ 0. 1. 2. 12. 33. 36.]
[ 6. 5. 4. 3. 2. 1.]]
--------xy-----------
[0. 1. 2. 4. 5. 6.]
[0. 1. 2. 4. 5. 6.]
--------xy-----------
[0. 1. 2. 4. 5. 6.]
[ 0. 1. 2. 12. 33. 36.]
--------xy-----------
[0. 1. 2. 4. 5. 6.]
[6. 5. 4. 3. 2. 1.]
上一篇: 学Python了还不会异常处理吗?这一篇让你彻底掌握它
下一篇: 判断一颗树是不是完全二叉树