我有一个包含一些数据的CSV文件,我需要创建一个折线图,显示CSV文件中每个日期和时间范围的时间线上的临时变化以及每条记录的临时值。
from matplotlib import pyplot, dates
from csv import reader
from dateutil import parser
with open("C:\\Users\\Username\\Desktop\\temdata.csv","r") as f:
data = list(reader(f))
temp = [i[3] for i in data]
time = [parser.parse(i[1]) for i in data]
pyplot.title('Temperature changes over Time')
pyplot.xlabel('Time/hours')
pyplot.ylabel('Temperature')
pyplot.plot(time, temp)
pyplot.show()
我得到了这个可怕的缩放图,它没有将数据放在透视图中,它看起来更有意义,而不仅仅是在温度确实增加的时候有一个巨大的峰值。
我能够在文件中放置一个虚拟记录,让它认为温度一度达到300,使其看起来更好。
是否有任何方法对于初学者来说很简单,并且使用Python绘制数据以便在不创建虚拟数据的情况下以不同方式缩放数据?
是否有更强大的功能,用于绘制和绘制Python以显示一段时间内的温度并不太难?
答案 0 :(得分:2)
我想到了两个可能的简单想法:
使用对数y轴显示数据。将plot()
条目替换为semilogy()
:
pyplot.semilogy(time, temp)
将温度限制在上限,例如120:
temp = [min(120, int(i[3])) for i in data]
注意,我会避免将time
用作变量,因为这也是Python module
名称。
所以你的更新可能会是这样的:
from matplotlib import pyplot, dates
from csv import reader
from dateutil import parser
with open("input.csv","r") as f:
data = list(reader(f))
temp = [min(120, int(i[3])) for i in data]
time = [parser.parse(i[1]) for i in data]
pyplot.title('Temperature changes over Time')
pyplot.xlabel('Time/hours')
pyplot.ylabel('Temperature')
pyplot.plot(time, temp)
pyplot.show()