使用 matplotlib 库绘制列表(不按顺序)时如何更正 y 间隔?
How do you correct the y-intervals when graphing a list(not in order) using matplotlib library?
绘制时 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
Result
问题是您的值是字符串,这就是它们乱序的原因。分别使用 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()
绘制时 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
Result
问题是您的值是字符串,这就是它们乱序的原因。分别使用 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()