我试图绘制许多情节,这里的数据是如何组织的样本:Matplotlib插曲日期时间X轴蜱未如预期运行
我的目的是要建立一系列的使用谷歌分析数据的小时数或天数(例如一周7天,或一天24小时)。我的索引是日期时间对象。
下面是一个示例,说明当轴正确完成时单个绘图的外观。
from datetime import datetime, date, timedelta
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
import matplotlib.dates as dates
#creating our graph and declaring our locator/formatters used in axis labelling.
hours = dates.HourLocator(interval=2)
hours_ = dates.DateFormatter('%I %p')
el = datetime(year=2016, day=1, month=3, hour=0)
fig, ax = plt.subplots(ncols = 1, nrows= 1)
fig.set_size_inches(18.5, 10.5)
fig.tight_layout()
ax.set_title(el.strftime('%a, %m/%d/%y'))
ax.plot(df_total.loc[el:el+timedelta(hours=23, minutes=59),:].index,
df_total.loc[el:el+timedelta(hours=23, minutes=59),:].hits, '-')
ax.xaxis.set_major_locator(hours)
ax.xaxis.set_major_formatter(hours_)
fig.show()
正如你所看到的,x轴看起来不错,如预期运行与正确的蜱/日期标签。
但是,当我尝试在子系列图上运行同一个图时,我遇到以下错误。这里是我的代码:
fig, ax = plt.subplots(ncols = 3, nrows= 2)
fig.set_size_inches(18.5, 10.5)
fig.tight_layout()
nrows=2
ncols=3
count = 0
for row in range(nrows):
for column in range(ncols):
el = cleaned_date_range[count]
ax[row][column].set_title(el.strftime('%a, %m/%d/%y'))
ax[row][column].xaxis.set_major_locator(hours)
ax[row][column].xaxis.set_major_formatter(hours_)
ax[row][column].plot(df_total.loc[el:el+timedelta(hours=23,minutes=59),:].index, df_total.loc[el:el+timedelta(hours=23,minutes=59),:].hits)
count += 1
if count == 7:
break
然而,得到下面的非常时髦的情节,与贴错标签的轴:
但面临相同的行为,只有最后一个子图的轴似乎与其他工作不工作。
任何有识之士将不胜感激!