我试图绘制许多图,这里是一个如何组织数据的示例:
我的目的是使用谷歌分析数据建立一系列的小时或几天(比如一周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()
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,x轴看起来很好,正常工作时使用正确的刻度/日期标签.
但是,当我尝试在子图系列上运行相同的图时,我遇到了以下错误.这是我的代码:
fig, ax = plt.subplots(ncols = 3, nrows= 2) …Run Code Online (Sandbox Code Playgroud) 我有这样的情节:
我想更改 12 个位置的刻度,以这种格式指示相应的月份:Jan-Feb_Mar...
当我使用 MonthLocator 函数时,刻度从绘图中消失
ax = plt.gca()
ax.set_xlim([0, 365])
ax.xaxis.set_major_locator(MonthLocator())
ax.xaxis.set_minor_locator(MonthLocator(bymonthday=15))
ax.xaxis.set_major_formatter(ticker.NullFormatter())
ax.xaxis.set_minor_formatter(dates.DateFormatter('%b'))
Run Code Online (Sandbox Code Playgroud)
我不知道这段代码中的错误在哪里。谢谢