I have created a temperature plot in Python using matplotlib and pyplot but cannot figure out how to add a date label for the very first value on the x-axis and the very last label. I have tried multiple iterations of DayLocator, but to no avail. I would like to keep the bounds of the actual plot the same and would like to keep the minor ticks for each day as is in the plotenter image description here, so there is not empty space at the beginning or end of the line plot. The data is from 06/01 to 08/30. Here is my full code, which plots perfectly except the very first tick for 06/01 and the very last tick for 08/30 do not show up:
import csv
import pylab
from datetime import datetime
from matplotlib import pyplot as plt
import matplotlib.dates as mdates
filename ='Summer17.csv'
with open(filename) as f:
reader = csv.reader(f)
header_row = next(reader)
dates_list, temperature, dewpoint = [], [], []
for row in reader:
if len(row) < 4:
continue
datestring = ("2017/{0:}/{1:} {2:}".format(*row))
date_obj = datetime.strptime(datestring, "%Y/%m/%d %H:%M")
dates_list.append(date_obj)
temp = float(row[3])
temperature.append(temp)
fig = plt.figure(dpi=128, figsize=(10,6))
ax = fig.add_axes([0.1, 0.1, 0.85, 0.75])
plt.plot(dates_list,temperature, linewidth=0.5, c='red')
plt.xlim(dates_list[1],dates_list[-1])
myFmt = mdates.DateFormatter('%m/%d')
months = mdates.MonthLocator()
days1 = mdates.DayLocator(bymonthday=(1,15)) # every Day
dayss = mdates.DayLocator()
ax.xaxis.set_major_formatter(myFmt)
ax.xaxis.set_major_locator(days1)
ax.xaxis.set_minor_locator(dayss)
