Skip to content

Instantly share code, notes, and snippets.

@KMouratidis
Created October 2, 2024 19:40
Show Gist options
  • Save KMouratidis/bf2bc3ca92d090c486064bc437e521c8 to your computer and use it in GitHub Desktop.
Save KMouratidis/bf2bc3ca92d090c486064bc437e521c8 to your computer and use it in GitHub Desktop.
"Godot's funding over time" code
import pandas as pd
import matplotlib.pyplot as plt
# I had it as an xlsx but converted to csv for github, hopefully it won't have issues
file_path = "~/Godot-Funding.csv"
df = pd.read_csv(file_path)
# Data "cleaning"
df['Year'] = df['Year'].astype(int)
df['Month'] = df['Month'].astype(int)
df['Day'] = df['Day'].astype(int)
# August is completely missing so I dropped the first month
df = df[(df['Year'] != 2023) | (df['Month'] != 7)]
df['Date'] = pd.to_datetime(df[['Year', 'Month', 'Day']])
df['Donors'] = df['Members'] + df['Sponsors']
### First plot ###
fig, ax1 = plt.subplots(figsize=(12, 6))
# Plot Income on the left y-axis
color = 'tab:blue'
ax1.set_xlabel('Date')
ax1.set_ylabel('Income', color=color)
ax1.plot(df['Date'], df['Income'], color=color)
ax1.tick_params(axis='y', labelcolor=color)
# Create a second y-axis for Donors
ax2 = ax1.twinx()
color = 'tab:red'
ax2.set_ylabel('Donors', color=color)
ax2.plot(df['Date'], df['Donors'], color=color, linestyle='--')
ax2.tick_params(axis='y', labelcolor=color)
# "Styling" and "fixes"
ax1.set_ylim([0, 65000])
ax2.set_ylim([0, 2000])
ax1.tick_params(axis='x', labelrotation=45)
plt.title('Income and Donors')
fig.tight_layout()
plt.show()
### Second plot ###
fig, ax1 = plt.subplots(figsize=(12, 6))
# End of month & diff data
df_monthly = df.resample('M', on='Date').last()
df_monthly['Income_change'] = df_monthly['Income'].diff()
df_monthly['Donors_change'] = df_monthly['Donors'].diff()
# Calculate bar positions (x-values)
bar_width = 0.35
index = range(len(df_monthly))
x_income = [i - bar_width/2 for i in index]
x_donors = [i + bar_width/2 for i in index]
# Plot Income change on the left y-axis as a bar chart
color = 'tab:blue'
ax1.set_xlabel('Date')
ax1.set_ylabel('Income', color=color)
ax1.bar(x_income, df_monthly['Income_change'], width=bar_width, color=color, alpha=0.7, label='Income Change')
ax1.tick_params(axis='y', labelcolor=color)
# Create a second y-axis for Donors change
ax2 = ax1.twinx()
color = 'tab:red'
ax2.set_ylabel('Donors', color=color)
ax2.bar(x_donors, df_monthly['Donors_change'], width=bar_width, color=color, alpha=0.7, label='Donors Change')
ax2.tick_params(axis='y', labelcolor=color)
# Set y-axis limits to align zeros
ax1.set_ylim([-6000, 8000])
ax2.set_ylim([-600, 800])
# Set x-axis ticks to match the original dates
plt.xticks(index, df_monthly.index.strftime('%Y-%m'), rotation=45, ha='right')
ax1.tick_params(axis='x', labelrotation=45)
# Add a title and show the plot
plt.title('Monthly Increase/Decrease of Income and Donors')
fig.tight_layout()
plt.show()
Year Month Day Income Members Sponsors
2023 7 11 19146 255 6
2023 7 12 19846 298 6
2023 7 16 23060 400 8
2023 7 18 23138 407 8
2023 7 19 23300 418 8
2023 7 20 25841 421 9
2023 7 23 25952 432 9
2023 9 12 25007 404 9
2023 9 13 27701 579 9
2023 9 14 30907 839 9
2023 9 15 32490 968 9
2023 9 16 33361 1067 9
2023 9 17 44395 1119 10
2023 9 18 44395 1119 10
2023 9 19 46899 1294 10
2023 9 20 47236 1322 10
2023 9 21 49578 1391 10
2023 9 23 50265 1437 11
2023 9 24 50432 1471 11
2023 9 26 49698 1500 10
2023 9 28 49872 1522 10
2023 9 29 49948 1536 10
2023 9 30 50096 1551 10
2023 10 1 50192 1561 10
2023 10 2 50304 1568 10
2023 10 3 50323 1569 10
2023 10 4 50356 1572 10
2023 10 5 50410 1581 10
2023 10 6 50532 1583 10
2023 10 7 50603 1586 10
2023 10 8 50543 1578 10
2023 10 9 51771 1595 11
2023 10 10 51794 1598 11
2023 10 11 51828 1598 11
2023 10 12 51214 1579 11
2023 10 13 51247 1574 11
2023 10 14 50534 1566 13
2023 10 15 49727 1505 13
2023 10 16 50126 1551 13
2023 10 17 49706 1543 13
2023 10 18 49636 1550 13
2023 10 19 49656 1547 13
2023 10 20 49833 1544 13
2023 10 21 49482 1545 13
2023 10 22 49423 1538 13
2023 10 23 49252 1531 13
2023 10 24 50372 1542 14
2023 10 25 50349 1541 14
2023 10 26 50377 1542 14
2023 10 27 50404 1547 14
2023 10 28 50512 1549 14
2023 10 29 50485 1547 14
2023 10 30 50305 1530 14
2023 10 31 50254 1517 14
2023 11 1 50295 1522 14
2023 11 2 50425 1542 14
2023 11 3 50430 1542 14
2023 11 4 50706 1542 15
2023 11 9 50709 1550 15
2023 11 27 55048 1511 16
2023 12 3 55409 1548 16
2023 12 6 55417 1550 16
2023 12 11 55479 1555 16
2023 12 19 55312 1523 16
2024 1 2 54754 1507 16
2024 1 4 54744 1507 16
2024 1 8 54824 1501 16
2024 1 11 53005 1337 17
2024 1 19 53817 1425 17
2024 1 21 54540 1447 17
2024 1 27 54855 1469 17
2024 2 4 54923 1478 17
2024 2 5 54576 1474 17
2024 2 6 54625 1470 17
2024 2 7 54856 1476 17
2024 2 8 54906 1482 17
2024 3 7 53493 1470 18
2024 3 24 54117 1434 18
2024 3 26 54076 1432 18
2024 4 9 53832 1427 19
2024 4 16 52939 1347 19
2024 4 18 52775 1360 19
2024 5 3 54027 1443 17
2024 5 4 54032 1444 17
2024 5 16 54292 1449 17
2024 5 23 54817 1454 18
2024 5 30 54757 1444 18
2024 5 31 51531 1446 16
2024 6 14 52026 1455 17
2024 6 22 51951 1454 17
2024 6 24 52006 1457 17
2024 6 29 51936 1450 17
2024 7 5 52336 1442 18
2024 7 9 52321 1441 18
2024 7 11 52436 1451 18
2024 7 12 52466 1456 18
2024 7 13 52451 1457 18
2024 7 15 52511 1461 18
2024 7 16 52536 1462 18
2024 8 15 52666 1474 18
2024 8 22 59015 1486 19
2024 8 28 59715 1494 19
2024 8 31 59725 1492 19
2024 9 15 59418 1494 19
2024 9 17 59538 1495 19
2024 9 19 59483 1491 19
2024 9 28 54052 1483 18
2024 9 29 54032 1481 18
2024 9 30 54302 1484 18
2024 10 1 56002 1533 18
2024 10 2 56397 1550 18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment