def draw_chart(chart_title, predicted, actual, dates, png_filename): # three pd.Series import matplotlib matplotlib.use("Agg") import matplotlib.pyplot as plt from matplotlib.dates import DayLocator, HourLocator, DateFormatter chart_width, chart_height = 11, 8.5 fig = plt.figure(figsize=(chart_width, chart_height)) day_count = 1 + (dates[-1] - dates[0]).days ax = fig.add_subplot(111) ordinals = [matplotlib.dates.date2num(d) for d in dates] ax.plot_date(ordinals, actual, 'b-', label='Actual') ax.plot_date(ordinals, predicted, 'r-', label='Predicted') ax.xaxis.set_major_locator( DayLocator(interval=compute_date_step(day_count, chart_width))) ax.xaxis.set_major_formatter(DateFormatter('%Y-%b-%d')) locator = HourLocator() locator.MAXTICKS = (day_count + 3) * 24 ax.xaxis.set_minor_locator(locator) ax.autoscale_view() ax.grid(True) fig.autofmt_xdate() ax.legend(loc='upper right', shadow=True) plt.title(chart_title, fontsize=10) fig.savefig(png_filename) plt.close() print(">> wrote: ", png_filename)
def draw_multi_charts(chartlist, main_title, outputfile): import matplotlib matplotlib.use("Agg") import matplotlib.pyplot as plt from matplotlib.dates import DayLocator, HourLocator, DateFormatter chart_width, chart_height = 14, 8.5 fig = plt.figure(figsize=(chart_width, chart_height)) rows, cols = get_page_dim(len(chartlist)) index = 1 for (chart_title, predicted, actual, dates) in chartlist: ax = fig.add_subplot(rows, cols, index) index += 1 day_count = 1 + (dates[-1] - dates[0]).days ordinals = [matplotlib.dates.date2num(d) for d in dates] ax.plot_date(ordinals, actual, 'b-', label='Actual') ax.plot_date(ordinals, predicted, 'r-', label='Predicted') ax.xaxis.set_tick_params(labelsize=6) ax.xaxis.set_major_locator( DayLocator(interval=compute_date_step(day_count, float(chart_width) / cols))) ax.xaxis.set_major_formatter(DateFormatter('%Y-%b-%d')) locator = HourLocator() locator.MAXTICKS = (day_count + 3) * 24 ax.xaxis.set_minor_locator(locator) ax.autoscale_view() ax.grid(True) fig.autofmt_xdate() ax.legend(loc='upper right', shadow=True, fontsize=4) ax.set_title(chart_title) fig.suptitle(main_title) fig.savefig(outputfile) plt.close() print(">> wrote: ", outputfile)