简体   繁体   English

如何使用matplotlib将txt文件中的det unix时间值转换为日期值?

[英]How can I convert det unix time values from txt file to date values using matplotlib?

I basically want to convert the unix time values I have in the first row of my text file named 'EKTE9' to date. 我基本上想将文本文件“ EKTE9”的第一行中的Unix时间值转换为日期。 I think using the datetime library is the way to go, but I don't know how to implement that in my code. 我认为使用datetime库是可行的方法,但是我不知道如何在我的代码中实现它。 Some answers would be appreciated. 一些答案将不胜感激。

import matplotlib.pyplot as plt
import csv

x = []
y = []

with open('EKTE9.txt','r') as csvfile:
    plots = csv.reader(csvfile, delimiter=',')
    for row in plots:
        x.append(int(row[0]))
        y.append(float(row[3]))

plt.plot(x,y, label='Temperatur')
plt.xlabel('Tid')
plt.ylabel('Temperatur')
plt.title('Grafen viser temperatur under forsøket\n')
plt.legend()
plt.show()

Here are some values from the EKTE9.txt file: 以下是EKTE9.txt文件中的一些值:

1554058225,0.80,2.90,13.60,27.20
1554058525,0.30,0.80,9.60,26.70

The values goes on for about 200 lines 值持续约200行

You would first create a datetime object using datetime.fromtimestamp() . 您首先要使用datetime.fromtimestamp()创建一个datetime对象。 This can then be converted to a matplotlib number using date2num() . 然后可以使用date2num()将其转换为matplotlib数字。 Finally, you should use a DateFormatter() to help with displaying the x-axis. 最后,您应该使用DateFormatter()帮助显示x轴。

import matplotlib
import matplotlib.pyplot as plt
from datetime import datetime
import csv

x = []
y = []

with open('EKTE9.txt', 'r', newline='') as csvfile:
    plots = csv.reader(csvfile, delimiter=',')

    for row in plots:
        x.append(matplotlib.dates.date2num(datetime.fromtimestamp(int(row[0]))))
        y.append(float(row[3]))

hfmt = matplotlib.dates.DateFormatter('%d\n%H:%M')
plt.plot(x,y, label='Temperatur')
plt.gca().xaxis.set_major_formatter(hfmt)
plt.xlabel('Tid')
plt.ylabel('Temperatur')
plt.title('Grafen viser temperatur under forsøket\n')
plt.legend()
plt.show()

This would then show your two values as follows: 然后,将显示您的两个值,如下所示:

Matplotlib输出

That did the job. 做到了。 Thank you very much Martin Evans. 非常感谢Martin Evans。 Fina code: 国际泳联代码:

import matplotlib
import matplotlib.pyplot as plt
from datetime import datetime
import csv

x = []
y = []

with open('EKTE9.txt','r') as csvfile:
     plots = csv.reader(csvfile, delimiter=',')

     for row in plots:
        x.append(matplotlib.dates.date2num(datetime.fromtimestamp(int(row[0]))))
        y.append(float(row[3]))

hfmt = matplotlib.dates.DateFormatter('%d\n%H:%M')
plt.plot(x,y, label='Temperatur')
plt.gca().xaxis.set_major_formatter(hfmt)
plt.xlabel('Tid')
plt.ylabel('Temperatur')
plt.title('Grafen viser temperatur under forsøket\n')
plt.legend()
plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何使用 matplotlib 从 .txt 文件中绘制数据 - how can i plot data from .txt file using matplotlib 使用python,如何将windows上的txt文件转换为unix类型? - with python, how can i convert a txt file on windows to a unix type? 如何使用 matplotlib 使用 .txt 文件中保存的数据绘制数据? - How can I plot data using saved data from .txt file using matplotlib? 如何使用 matplotlib 从 .txt 文件中绘制数据? - How can you plot data from a .txt file using matplotlib? 如何通过读取.txt文件的每一行来将键值添加到字典中并更新值? - How can I add key values into a dictionary from reading in each line of a .txt file and update values? 如何获取一个包含值和标题表的 .txt 文件并将其转换为 numpy 2D 数组? - How can I take a .txt file full of a table of values and header and convert it to a numpy 2D array? 如何从 .txt 文件转换 RGB 值以在 Python 中显示图像 - How to convert RGB values from .txt file to display an image in Python 将值列表从txt文件转换为字典 - convert list of values from a txt file to dictionary 如何从.txt文件中减去我得到的两个值 - How can I subtract two values which I have got from a .txt file 我无法使用文件中的matplotlib值绘制图形 - I couldn't plot graph using matplotlib values from file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM