Plotting a blank graph

On some occasions I need to plot a blank graph, print it, and then fill the point manually. Of course, I’d like to have values on the x-axis, and those are usually dates.

The easiest way I found so far is to use any Jupyter instance with venerable matplotlib and run the following code:

import datetime as dt
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from matplotlib.ticker import MaxNLocator, AutoMinorLocator

start_dt = dt.date.fromisoformat('2021-11-10')
end_dt = dt.date.fromisoformat('2022-02-08')

min_y = 80
max_y = 100

f, ax = plt.subplots(figsize=(16, 10))
xfmt = mdates.DateFormatter('%d/%m')

ax.xaxis.set_major_formatter(xfmt)
ax.xaxis.set_major_locator(MaxNLocator((end_dt - start_dt).days))
ax.xaxis.set_tick_params(rotation=90)
ax.xaxis.grid(True)
ax.set_xlim(start_dt, end_dt)

ax.yaxis.set_major_locator(MaxNLocator(max_y-min_y))
ax.yaxis.set_minor_locator(AutoMinorLocator())
ax.yaxis.grid(True)
ax.set_ylim(min_y, max_y)

plt.tight_layout()

filename = "./static/images/blank-graph.png"
plt.savefig(filename, bbox_inches='tight')

The last two lines are there to save the figure into a png file that one can then print. Jupyter doesn’t offer a nice way to print just one cell, thus I resorted to this trick. As this is a standalone code block, one can also run it using pure python.

The final output will be the following graph:

blank graph with dates on x-axis