我有一个简单的文本文件,其中包含科学数据,每行上有一个数据项。我想简单地将其绘制为y轴的顺序项目。我称之为:list_y
在Python脚本中,我创建了一个列表,该列表是x轴的简单序号列表。我称之为:list_x
当我使用matplotlib调用进行绘图时,与我读过的数十个示例相同,我得到了意想不到的结果。Matplotlab似乎没有给出y轴从y值数据项中从最小到最大的y轴。它似乎绘制了数字对 - 而不是我所说的“图形”。
我阅读了各种示例,并使用我正在使用的确切代码观看了Youtube演示,这些示例都给出了我所期望的结果。
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 10 12:18:57 2020
@author: usera
"""
from matplotlib import pyplot as plt
# data in data01a.txt is: 0.1 0.22 0.33 0.5 0.6 0.66 1 .9
data_han = open('data01a.txt', 'r')
list_y = data_han.readlines()
data_han.close()
y_size = len(list_y)
ii = 0
list_x = []
#follow while stmt creates a numeric list for x-axis
while ii < y_size:
list_x.append(ii)
ii += 1
plt.xlabel('Sequence')
plt.ylabel('Value')
plt.legend()
plt.grid('True')
#following 3 lines of code commented out
#since it causes strange results
#axes = plt.gca()
#axes.set_xlim(0,10)
#axes.set_ylim(0,1)
#following commented out code does not give correct x axis
#and no line is plotted at all
#plt.axis(0,10,0,1)
#following commented out code does not give correct y axis
#plt.xlim(0,10)
#plt.ylim(0,1)
plt.plot(list_x, list_y, label='Test Plot')
plt.savefig('test01a.png')
plt.show()
撒科打诨
相关分类