简体   繁体   English

Python:matplotlib线图x轴上的时间戳错误

[英]Python: Timestamp error on matplotlib line plot x-axis

I am trying to produce a line plot from a csv file with the data formatted: 我试图从具有格式的数据的csv文件中生成线图:

Time,Temp
05 Oct 4:35 pm,68
05 Oct 4:30 pm,68
05 Oct 4:20 pm,68

The code I used is: 我使用的代码是:

import matplotlib.pyplot as plt
import csv

x = []
y = []

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

plt.plot(x, y, label='Loaded from file')

plt.xlabel('Timestamp')
plt.ylabel('Temperature')
plt.title('Temperature by Timestamp')
plt.legend()
plt.show()

However it produces this error: 但是,它会产生此错误:

Traceback (most recent call last):
  File "visualizingdata.py", line 12, in <module>
    x.append(int(row[0]))
ValueError: invalid literal for int() with base 10: 'Time'

I believe this is due to the timestamp format but don't know how to convert it. 我相信这是由于时间戳格式引起的,但不知道如何转换。

Please help. 请帮忙。 Thank you. 谢谢。

Here is one solution with two problems fixed: 这是解决了两个问题的一种解决方案:

with open('time_temp.csv', 'r') as csvfile:
    plots = csv.reader(csvfile, delimiter=',')
    plots.next()
    for row in plots:
        temp = row[0].split()
        x.append(int(temp[0]))
        y.append(int(row[1]))

The first problem in your program is that you are trying to convert the strings in the file headers to int using an int command. 程序中的第一个问题是您试图使用int命令将文件头中的字符串转换为int To avoid this you can skip the header using plots.next() . 为了避免这种情况,您可以使用plots.next()跳过标题。

The next problem is that row[0] is an actual string with date that cannot be converted directly to an int using int command. 接下来的问题是, row[0]是一个实际的字符串与日期,不能直接转换为int使用int命令。 To fix this you can split() the row[0] string and use it's first element. 为了解决这个问题,您可以split() row[0]字符串并使用它的第一个元素。 The later part is left as it is. 后面的部分保持原样。

These modifications should solve your actual problem which I assume is plotting the data against time appearing as time stamps on the x-axis: 这些修改应该可以解决您的实际问题,我认为这是根据x轴上的时间戳显示的时间来绘制数据:

labels = []
y = []
with open('time_temp.csv', 'r') as csvfile:
    plots = csv.reader(csvfile, delimiter=',')
    plots.next()
    for row in plots:
        labels.append(row[0])
        y.append(int(row[1]))

labels = labels[::-1]
x = range(len(labels))
plt.xticks(x, labels, rotation='horizontal')

The new parts here is that the time stamp data from row[0] is now appended to a list labels that is later used to generate tick labels for the x-axis. 这里的新部分是,现在将来自row[0]时间戳数据附加到列表labels ,该列表labels随后用于为x轴生成刻度标签。 The x-axis values are actually just sequential integers generated with a range command which length matches the data length. x轴值实际上只是由range命令生成的顺序整数,其长度与数据长度匹配。

Also, in your example data set the dates seem to go from most recent to the least recent. 同样,在您的示例数据集中,日期似乎从最近到最近。 This is taken care of by inverting the labels using labels = labels[::-1] . 这可以通过使用labels = labels[::-1]反转标签来解决。 Labels are added to the plot using xticks . 使用xticks将标签添加到绘图中。

I would suggest not to reinvent the wheel and use some existing functionality to obtain datetimes directly. 我建议不要重新发明轮子,而是使用一些现有功能直接获取日期时间。 One option is to use pandas. 一种选择是使用熊猫。

If the data looks like this (I added some data to show the effect of dissimilar spacings and unordered data): 如果数据看起来像这样(我添加了一些数据以显示不同的间距和无序数据的效果):

Time,Temp
05 Oct 10:32 am,10
05 Oct 4:35 pm,20
05 Oct 4:30 pm,30
05 Oct 4:20 pm,68

the code could then look like this: 代码如下所示:

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv("data/timetemp.csv")
df["Time"] = pd.to_datetime(df["Time"], format="%d %b %I:%M %p")
df.sort_values("Time", inplace=True)

plt.plot(df["Time"],df["Temp"])

plt.show()

在此处输入图片说明

You could optionally also use pandas for plotting: 您还可以选择使用熊猫进行绘图:

# optionally use pandas for plotting:
df.plot(x="Time", y="Temp")

在此处输入图片说明

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM