在创建Pandas图时跳过gcf().autofmt_xdate()

2024-09-22 20:32:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着用熊猫数据框绘制多个时间序列。数据帧包含100多个寄存器。

从panda的文档中我读到,当执行panda s.df.plot()时,它也会使用gcf().autofmt_xdate()执行。我想把我的自定义日期时间格式,但当我尝试我的自定义日期格式是重叠的日期默认由熊猫绘图。有没有办法在创建绘图时跳过gcf().autofmt_xdate()??如何向panda提供自定义日期时间格式?

这是生成的图。

enter image description here

这是python代码。

import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import pandas as pd
from pandas import Series
import pickle
datos = pickle.load(open("datos_reporte.pickle", "r"))
reload(plt)
series_o = []
series_p_h = []
series_p_d = []
series_names = []
for cod_estacion in datos.keys():
    x = [d[0] for d in datos[cod_estacion]['historial_semanal']]
    y = [d[1] for d in datos[cod_estacion]['historial_semanal']]
    s = Series(y, x)
    series_o.append(s.groupby(level=0).first())

df1 = pd.concat(series_o, join='outer', axis=1)
interval  = int(len(df1) / 12)
df1.columns = series_names
ax = plt.figure(figsize=(7,5), dpi=100).add_subplot(111)
df1.plot(ax=ax)
ax.xaxis.set_major_locator(mdates.HourLocator(interval=200))
ax.xaxis.set_major_formatter(mdates.DateFormatter('%m/%d-%H:%S'))
ax.xaxis.grid(True, which="minor")
plt.title("Datos observados")
plt.ylabel('Caudal m^3/s')
plt.xlabel('Fecha')
plt.legend(loc=0,prop={'size': 7})
plt.xticks(rotation='vertical', fontsize = 8)
plt.subplots_adjust(bottom=.2)
plt.show()

Tags: importforas格式时间pltaxpanda