我有一个循环,可以通过过滤PANDAS数据框来制作大量图表:
def makeChart(name):
plt.clf()
plt.cla()
filename = "plots/" + name.replace(" ","_")
series = df[df['name'] == name]
series_wk = series[['name','start_date','num_nights']].groupby([pd.Grouper(key='start_date', freq='W')]).sum().reset_index()
series_wk['period'] = series_wk.apply(checkperiod, axis=1)
series_wk.set_index('start_date',inplace=True)
series_wk.to_csv(filename + ".csv")
ne = series_wk[series_wk['period'] == "Non-election"]
e = series_wk[series_wk['period'] == "Election"]
fig, ax = plt.subplots(figsize=(15,7))
ax.bar(ne.index, ne['num_nights'], color='b')
ax.bar(e.index, e['num_nights'], color='r')
ax.xaxis.set_major_locator(ticker.MultipleLocator(50))
ax.xaxis.set_major_formatter(mdates.DateFormatter('%d-%m-%Y'))
ax.set_title(name)
plt.xticks(rotation=90)
fig.savefig(filename)
plt.show()
makeChart("CARR Kim")
plt.show()的输出具有正确的数据,并且looks like this 但是,保存的文件中的数据和looks like this不正确。 我是使用matplotlib的新手,实际上无法弄清楚为什么实际数据点在这里会有所不同。任何帮助将不胜感激!