Home > Net >  Add N to Title in Pie Chart
Add N to Title in Pie Chart

Time:10-28

I wrote a code that outputs a pie chart as such:

AGE_COUNTS=PIE_DATA.Age_Group.value_counts()
ax = AGE_COUNTS.plot(kind='pie', autopct='%.0f%%', shadow = True, label="")
ax.set_title("LWBS Age Distribution")

enter image description here

I would like to add the N of the total counts by age to the title so for example the title would read 'LWBS Age Distribution (n=14)'. So for example, if I add up all of the counts for each age group the total population would equal 14. I want to do this automated so I don't have to manually change the title every time I update the date range for the data. I haven't been able to find much literature online that speaks to my request.

CodePudding user response:

Try setting the title with an f-string:

ax.set_title(f"LWBS Age Distribution (n={len(PIE_DATA)})")
  • Related