Skip to content

Instantly share code, notes, and snippets.

@abelgvidal
Last active October 25, 2023 10:22
Show Gist options
  • Save abelgvidal/79a921bf8127954e7b82f226b778769e to your computer and use it in GitHub Desktop.
Save abelgvidal/79a921bf8127954e7b82f226b778769e to your computer and use it in GitHub Desktop.
Visualize a time series dataset using Python
import matplotlib.pyplot as plt
import datetime
# Dates and weights
dates = [
"Monday, August 28, 2023", "Wednesday, August 30, 2023", "Thursday, August 31, 2023",
"Friday, September 1, 2023", "Saturday, September 2, 2023", "Monday, September 4, 2023",
"Tuesday, September 5, 2023", "Wednesday, September 6, 2023", "Thursday, September 7, 2023",
"Friday, September 8, 2023", "Saturday, September 9, 2023", "Monday, September 10, 2023",
"Thursday, September 14, 2023", "Sunday, September 17, 2023", "Monday, September 18, 2023",
"9/19/2023", "Thursday, September 21, 2023", "Friday, September 22, 2023",
"Sunday, September 24, 2023", "Monday, September 25, 2023", "Saturday, September 30, 2023",
"Sunday, October 1, 2023", "Monday, October 2, 2023", "Thursday, October 5, 2023",
"Monday, October 9, 2023", "Tuesday, October 10, 2023", "Saturday, October 14, 2023",
"Monday, October 16, 2023", "Thursday, October 19, 2023"
]
weights = [
110.8, 108.5, 107.8, 107.8, 107.8, 106.9, 108, 107.5, 106.7, 106.8,
105.9, 105.8, 105.2, 105.5, 104.8, 104.9, 104.5, 104.2, 103.6, 104,
103.5, 102.2, 102.4, 101.8, 102.7, 101.1, 100.6, 100.1, 100.1
]
# Convert string dates to datetime objects
date_objects = [datetime.datetime.strptime(date, '%A, %B %d, %Y') if '/' not in date else datetime.datetime.strptime(date, '%m/%d/%Y') for date in dates]
# Plotting the data
plt.figure(figsize=(10, 6))
plt.plot(date_objects, weights, marker='o', linestyle='-', color='b')
plt.title('Weight Over Time')
plt.xlabel('Date')
plt.ylabel('Weight (kg)')
plt.xticks(rotation=45)
plt.tight_layout()
#plt.show()
plt.savefig('weight_plot.png', bbox_inches='tight')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment