在绘制图形时,y值没有按顺序排列
COLst3是一个没有按顺序排列的数字列表。示例COLst3列表:
['312', '313', '313', '312', '311', '313', '311', '311', '311', '310']X轴是时间,y轴是COLst3。创建的空列表是为了创建x个值点。
我需要帮助在一致的y间隔上绘制正确的值。
import time
import matplotlib.pyplot as plt
import numpy as np
def COfunction():
x=0
z=1
y=60 #change y according to the estimated number of CO values recorded
COLst = []
COLst3 = []
empty = []
while x < y:
open_file=open(r'C:\Users\MindStorm\Desktop\capture.txt','r')
file_lines=open_file.readlines()
file = file_lines[x].strip() # First Line
COLst = file.split()
COLst2 = COLst.pop(1)
COLst3.append(COLst2)
empty.append(z)
x += 6
z += 1
#plots using matplotlib library
plt.title('CO Displacement value Graph')
plt.xlabel('Time(seconds)')
plt.ylabel('Sensor values(volts)')
plt.plot(empty, COLst3)
plt.show()
#main functions
COfunction()代码成功运行,但我需要正确的y值间隔来绘制这两个列表。
Matplotlib版本: 2.2.3
发布于 2019-06-14 01:37:39
问题是你的值是字符串,这就是它们乱序的原因。分别使用int或float将它们转换为整型或浮点型。
COLst3 = ['312', '313', '313', '312', '311', '313', '311', '311', '311', '310']
COLst3 = list(map(int, COLst3)) # <--- Convert strings to integer
empty = range(len(COLst3))
plt.title('CO Displacement value Graph')
plt.xlabel('Time(seconds)')
plt.ylabel('Sensor values(volts)')
plt.plot(empty, COLst3)
plt.yticks(range(min(COLst3), max(COLst3)+1)) # <--- To show integer tick labels
plt.show()

https://stackoverflow.com/questions/56585476
复制相似问题