绘制 pd.Series 对象未正确显示年份
Plotting pd.Series object does not show year correctly
我正在绘制湿度传感器随时间变化的测量结果。
我正在使用 Python 3.7.1 和 Pandas 0.24.2.
我有一个名为 dateTimeList 的列表,其中包含日期和时间字符串:
dateTimeList = ['15.3.2019 11:44:27', '15.3.2019 12:44:33', '15.3.2019 13:44:39']
我写了这段代码,其中 index
是一个 DatetimeIndex 对象,humList
是一个浮点数列表。
index = pd.to_datetime(dateTimeList, format='%d.%m.%Y %H:%M:%S')
ts = pd.Series(humList, index)
plt.figure(figsize=(12.80, 7.20))
ts.plot(title='Gráfico de Humedad en el Tiempo', style='g', marker='o')
plt.xlabel('Tiempo [días]')
plt.ylabel('Humedad [V]')
plt.grid()
plt.savefig('Hum_General'+'.png', bbox_inches='tight')
plt.show()
我有这两个结果,一个是二月份的数据1 and the other one with data from March2。
问题是在 3 月,x 轴上出现了 00 12 00 12 序列,而不是离开 2019 年。我认为需要注意的是,这只发生在三月份的数据上,因为二月份没问题,而且两个月份的数据具有相同的结构。日和月在两个图上都正确显示。
我也试过:
index = [ pd.to_datetime(date, format='%d.%m.%Y %H:%M:%S') for date in dateTimeList]
现在 index
是时间戳对象的列表。相同的结果。
创建情节后立即添加此内容
import matplotlib.dates as mdates # this should be on the top of the script
xfmt = mdates.DateFormatter('%Y-%m-%d')
ax = plt.gca()
ax.xaxis.set_major_formatter(xfmt)
我的猜测是,由于 3 月的数据点较少,Matplotlib 更喜欢将日期标记为月-日-小时而不是年-月-日,因此当您在 3 月有更多数据时,问题应该会自行解决。无论用于绘制的数据点数量如何,我发布的代码都应保持年-月-日格式。
我正在绘制湿度传感器随时间变化的测量结果。 我正在使用 Python 3.7.1 和 Pandas 0.24.2.
我有一个名为 dateTimeList 的列表,其中包含日期和时间字符串:
dateTimeList = ['15.3.2019 11:44:27', '15.3.2019 12:44:33', '15.3.2019 13:44:39']
我写了这段代码,其中 index
是一个 DatetimeIndex 对象,humList
是一个浮点数列表。
index = pd.to_datetime(dateTimeList, format='%d.%m.%Y %H:%M:%S')
ts = pd.Series(humList, index)
plt.figure(figsize=(12.80, 7.20))
ts.plot(title='Gráfico de Humedad en el Tiempo', style='g', marker='o')
plt.xlabel('Tiempo [días]')
plt.ylabel('Humedad [V]')
plt.grid()
plt.savefig('Hum_General'+'.png', bbox_inches='tight')
plt.show()
我有这两个结果,一个是二月份的数据1 and the other one with data from March2。
问题是在 3 月,x 轴上出现了 00 12 00 12 序列,而不是离开 2019 年。我认为需要注意的是,这只发生在三月份的数据上,因为二月份没问题,而且两个月份的数据具有相同的结构。日和月在两个图上都正确显示。
我也试过:
index = [ pd.to_datetime(date, format='%d.%m.%Y %H:%M:%S') for date in dateTimeList]
现在 index
是时间戳对象的列表。相同的结果。
创建情节后立即添加此内容
import matplotlib.dates as mdates # this should be on the top of the script
xfmt = mdates.DateFormatter('%Y-%m-%d')
ax = plt.gca()
ax.xaxis.set_major_formatter(xfmt)
我的猜测是,由于 3 月的数据点较少,Matplotlib 更喜欢将日期标记为月-日-小时而不是年-月-日,因此当您在 3 月有更多数据时,问题应该会自行解决。无论用于绘制的数据点数量如何,我发布的代码都应保持年-月-日格式。