This program plots daily temperatures over one week with dates on the x-axis formatted as 'YYYY-MM-DD'. The dates are rotated for easy reading.
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from datetime import datetime, timedelta
# Create dates for one week
start_date = datetime(2024, 4, 1)
dates = [start_date + timedelta(days=i) for i in range(7)]
# Sample values for each date
values = [20, 22, 19, 24, 23, 25, 21]
# Plot the data
plt.plot(dates, values, marker='o')
# Format the x-axis to show dates nicely
plt.gca().xaxis.set_major_locator(mdates.DayLocator())
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
plt.xlabel('Date')
plt.ylabel('Temperature (Β°C)')
plt.title('Daily Temperatures Over One Week')
plt.gcf().autofmt_xdate() # Rotate date labels for better fit
plt.tight_layout()
plt.show()