I want to add the total calories of the macronutrients of each stacked bar (every stacked bar representing a day) by showcasing the sum of all the calories of the grams of proteins, carbs and fats.
### ASK USER THE NAME FOR FOR PERSONALIZED EXPERIENCE
import matplotlib
user_name = input(
"Hey! \\n\\nBefore we give you the chance to see how you could cycle your calories in a cool animation...\\n\\n"
"What's your name? ")
### ASK USER THE CALORIE INFORMATION
print("Awesome! Now... Let's start,", user_name, ('!\\n\\n'))
equation_choice = input("Do you know what your macros are? (Y/N): \\n").upper()
if equation_choice == "Y":
print("\\nAwesome! This will make it much quicker")
known_maintenance_calories = int(input("\\nHow much calories is your maintenance right now? "))
elif equation_choice == "N":
def calculate_bmr():
\# Prompt the user for necessary information
global weight
gender = input("Enter your gender (M/F): ")
age = int(input("Enter your age in years: "))
weight = float(input("Enter your weight in kilograms: "))
height = float(input("Enter your height in centimeters: "))
# Calculate BMR based on gender
global bmr
if gender.upper() == 'M':
bmr = int(88.362 + (13.397 * weight) + (4.799 * height) - (5.677 * age))
elif gender.upper() == 'F':
bmr = int(447.593 + (9.247 * weight) + (3.098 * height) - (4.330 * age))
else:
print("Invalid gender entered.")
return None
calculate_bmr()
activity_levels = int(input("\\nWhat is your activity level in your lifestyle? \\n"
"\\n1 = Sedentary (Little or No Exercise)"
"\\n2 = Slightly Active (Light Sports 1-3 Days/Week)"
"\\n3 = Moderately Active (Moderate Exercise 3-5 Days/Week)"
"\\n4 = Very Active (Hard Exercise 6-7 Days/Week)"
"\\n5 = Extra Active (Very Hard Exercise & Physical Job)"
"\\n\\nEnter your answer here: "))
global calories
if activity_levels == 1:
calories = int(bmr \* 1.2)
elif activity_levels == 2:
calories = int(bmr \* 1.375)
elif activity_levels == 3:
calories = int(bmr \* 1.55)
elif activity_levels == 4:
calories = int(bmr \* 1.725)
elif activity_levels == 5:
calories = int(bmr \* 1.9)
\# Call the function to calculate BMR
user_bmr = calculate_bmr()
if user_bmr is not None:
print("Your Maintenance Calories are:", calories, "cals")
### CHOOSE MACROS
# MACROS GRAMS#
default_protein_grams = int(weight \* 2)
default_fats_grams = int(((calories - (default_protein_grams \* 4)) \* 0.5) / 9)
default_carbs_grams = int(((calories - (default_protein_grams \* 4)) \* 0.5) / 4)
# MACROS CALORIES
protein_cals = default_protein_grams \* 4
carbs_cals = default_carbs_grams \* 4
fats_cals = default_fats_grams \* 9
# WEEKLY AND DAILY CALORIES
total_calories = int(protein_cals + carbs_cals + fats_cals)
weekly_calories = total_calories \* 7
print("\\nYour Macros are:"
"\\nProtein =", default_protein_grams, "gr"
"\\nCarbs =", default_carbs_grams, "gr"
"\\nFats =", default_fats_grams, "gr\\n")
### ADJUST THE CALORIES INTERACTIVELY
import matplotlib.pyplot as plt
import pandas as pd
from matplotlib.widgets import Slider, Button, RadioButtons
df = pd.DataFrame({
'Protein': [default_protein_grams,
default_protein_grams,
default_protein_grams,
default_protein_grams,
default_protein_grams,
default_protein_grams,
default_protein_grams],
'Carbs': [default_carbs_grams,
default_carbs_grams,
default_carbs_grams,
default_carbs_grams,
default_carbs_grams,
default_carbs_grams,
default_carbs_grams],
'Fats': [default_fats_grams,
default_fats_grams,
default_fats_grams,
default_fats_grams,
default_fats_grams,
default_fats_grams,
default_fats_grams],
})
weekdays = \["M", "T", "W", "TH", "F", "S", "SD"\]
ax = df.plot(stacked=True, kind='bar', edgecolor='black', width=0.75,)
for bar in ax.patches:
height = bar.get_height()
width = bar.get_width()
x = bar.get_x()
y = bar.get_y()
label_text = height
label_x = x + width / 2
label_y = y + height / 2
ax.text(label_x, label_y, label_text, ha='center', va='center')
ax.set_xticklabels(weekdays, rotation='horizontal')
plt.subplots_adjust(bottom=0.25)
ax_slider = plt.axes(\[0.1, 0.1, 0.8, 0.05\], facecolor="grey")
protein_macros = Slider(ax_slider, "Day Calories", valmin=1200, valmax=total_calories \* 2, valinit=total_calories, valstep=1)
plt.xlabel("Days of the Week")
plt.ylabel("Macros of the Day")
plt.show()
Every piece of code I've tried to implement gives me problem, since I can't seem to add a top label that uses a variable, that I can also link to a slider and all of this be connected so that it auto-adjusts the graph