[英]Matplotlib Subplot Datetime X-Axis Ticks Not Working As Intended
我正在尝试绘制许多图,这是数据组织方式的一个示例:
我的意图是使用Google Analytics(分析)数据在数小时或数天(例如一周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
但是,这将产生以下带有轴标签错误的非常时髦的图:
但是遇到相同的行为,只有最后一个子图的轴似乎在工作,其余的轴却不工作。
任何见识将不胜感激!
所以答案是几年前与set_major_locator()
和set_major_formatter()
对象有关的以下github问题:
https://github.com/matplotlib/matplotlib/issues/1086/
引用eric:
“您丢失了一些东西,但这是非常不直观且容易错过的事情:定位器无法在轴之间共享。set_major_locator()方法将其轴分配给该定位器,覆盖先前分配的任何轴。 “
因此解决方案是为每个新轴实例化一个新的dates.MinuteLocator
和dates.DateFormatter
对象,例如:
for ax in list_of_axes:
minutes = dates.MinuteLocator(interval=5)
minutes_ = dates.DateFormatter('%I:%M %p')
ax.xaxis.set_major_locator(minutes)
ax.xaxis.set_major_formatter(minutes_)
我进行了实验,看起来您不需要在绘图后引用date.Locator和dates.Formatter对象,因此可以使用相同的名称重新初始化每个循环是可以的。 (我可能在这里错了!)
我有同样的子图日期时间x轴刻度线丢失问题。 以下代码与OP的代码非常相似,似乎可以正常工作,请参见附图。 但是,我正在使用matplotlib 3.1.0,也许此版本已解决了该问题? 但是我确实有一个观察:如果为第二fig.autofmt_xdate()
图启用fig.autofmt_xdate()
,则第一fig.autofmt_xdate()
图日期时间x轴将不会显示。
fig = plt.figure()
plt.rcParams['figure.figsize'] = (width, height)
plt.subplots_adjust(wspace=0.25, hspace=0.2)
ax = fig.add_subplot(2,1,1)
ax.xaxis.set_major_locator(MonthLocator(bymonthday=1))
ax.xaxis.set_major_formatter(DateFormatter('%Y-%b'))
ax.plot(df1['DATE'], df1['Movement'], '-')
plt.ylabel(r'$D$', fontsize=18)
plt.xticks(fontsize=12)
plt.yticks(fontsize=16)
plt.legend(fontsize=16, frameon=False)
fig.autofmt_xdate()
ax = fig.add_subplot(2,1,2)
ax.xaxis.set_major_locator(MonthLocator(bymonthday=1))
ax.xaxis.set_major_formatter(DateFormatter('%Y-%b'))
ax.plot(df2['DATE'], df2['Movement'], '-')
#plt.ylabel(r'$D`enter code here`$', fontsize=18)
plt.xticks(fontsize=16)
plt.yticks(fontsize=16)
plt.legend(fontsize=16, frameon=False)
#fig.autofmt_xdate()
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.