【发布时间】:2022-01-16 12:48:20
【问题描述】:
我将很快从事一个包含大量数据的机器学习项目,因此我尝试模拟我将面临的挑战类型,以进行相应的准备。第一个是从 .cvs 中绘制日期时间(x 轴上的日期和 y 轴上的小时数)(使用 2021 年 4 月和 2020 年 4 月的此类数据):
我现在拥有的是 2021 年系列中的以下情节:
但我不明白为什么网格上的点不正确,例如,我在 2021 年 4 月的最新点应该是我在文件上的最后日期(即 07:37:56 30/ 04/2021):
但正如您在下图中看到的那样:
4 月的最后一个点在 y 刻度上比 08:00:00 更接近 07:00:00,并且位于 x 刻度的中间,如第 29 天和第 30 天的中间。这只是整个情节发生的事情的一个例子,我真的不知道为什么会发生。
我在 Google Colab 上的代码是这样的
#LIBRARIES AND PRE PROCESSING
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as pltd
import numpy as np
from datetime import datetime
caminho_dados = "/content/sample.csv"
df = pd.read_csv(caminho_dados,encoding='UTF-8',sep=',')
novo=df['ID;Hora;Data;;;'].str.split(';').str
df['ID']=novo[0]
df['Hora']=novo[1]
df['Data']=novo[2]
df.drop(columns=['ID;Hora;Data;;;'],inplace=True)
df['Data']=pd.to_datetime(df['Data'],format ='%d/%m/%Y')
df['Hora']=pd.to_datetime(df['Hora'],format ='%H:%M:%S')
#SEPARATING THE TWO APRILS I HAVE ON TWO DATAFRAMES, 2021 ONE & 2020 ONE
df_vinteum = df.iloc[:50, :]
df_vinte = df.iloc[50:, :]
#PLOTTING
fig, ax = plt.subplots()
ax.scatter(df_vinteum['Data'], df_vinteum['Hora'])
ax.xaxis.set_major_formatter(pltd.DateFormatter('%d'))
ax.yaxis.set_major_formatter(pltd.DateFormatter('%H'))
start, end = ax.get_xlim()
ax.xaxis.set_ticks(np.arange(start, end, 1))
start, end = ax.get_ylim()
ax.yaxis.set_ticks(np.arange(start,end,(end-start)/24))
plt.title("Abril 2021")
plt.xlabel('Dias')
plt.ylabel('Horas')
plt.xticks(rotation=45)
plt.grid()
plt.rcParams["figure.figsize"] = (20,6)
plt.show()
【问题讨论】:
标签: python datetime matplotlib plot google-colaboratory