通常,pandas和matplotlib的datetimes实用程序是不兼容的。如果您使用
matplotlib.dates
对象,则在大多数情况下都会失败。
以下是一个解决方案,其中pandas用于打印,matplotlib用于格式化(请参阅注释):
import matplotlib.pyplot as plt # version 2.1.0
import matplotlib.ticker as ticker
import pandas as pd # version 0.21.0
df = pd.read_csv('data.csv', delim_whitespace=True, index_col=0, parse_dates=['Created'])
date_index = pd.date_range(df.Created.min(), df.Created.max(), freq='D')
_, ax = plt.subplots()
s1 = df.resample('D', on='Created').size().fillna(0).reindex(date_index, fill_value=0)
s2 = df.groupby('Created')['MachineCount'].first().fillna(0).reindex(date_index, fill_value=0)
s = (s1 / s2).fillna(0).reindex(date_index, fill_value=0)
s1rolling = s1.rolling(window=7, center=False).mean().fillna(0).reindex(date_index, fill_value=0)
s2rolling = s2.rolling(window=7, center=False).mean().fillna(0).reindex(date_index, fill_value=0)
srolling = s.rolling(window=7, center=False).mean().fillna(0).reindex(date_index, fill_value=0)
# Plot with pandas without date axis (i.e. use_index=False).
s1.plot(kind='bar', color='C0', position=0, label='Sales Total', width=0.25, use_index=False)
s.plot(kind='bar', color='C1', position=1, label='Adjusted For Machine Count', width=0.25, use_index=False)
# Plot with pandas without date axis (i.e. use_index=False).
s1rolling.plot(kind='line', color='C0', label='_nolegend_', use_index=False)
srolling.plot(kind='line', color='C1', label='_nolegend_', use_index=False)
plt.ylim(0, s1.max() * 1.1)
plt.legend(loc='upper left')
plt.ylabel('Frequency')
plt.title('Items Deposited Per Day')
# Format date axis with matplotlib.
ticklabels = s1.index.strftime('%Y-%m-%d')
ax.xaxis.set_major_formatter(ticker.FixedFormatter(ticklabels))
plt.xticks(rotation=90)
plt.tight_layout()
plt.show()
我希望这对你有帮助。