Cell # 10, and 11 (Getting the percentages on the legend)

Code
  • This cell repeats the process done in cell 10 and makes a pie chart for every year.
  • “autopct='%1.1f%%'” makes the percentages appear on the pie chart.
    • # describes how many digits in the percentage, f means “float”
    • Deleting this command, and adding the percentages to the title makes them appear on the legend.
  • This code gives graph with legend and percentages:
labels = ['Delaware Community School-36.8%','Albany High School-5.3%','Myers Middle School-10.5%','Homeschool-2.6%','KIPP-2.6%','North Albany Academy-2.6%','Pine Hill School-28.9%','Albany School of Humanity-2.6%','Hackett Middle School-5.3%','Homeschooled-2.6%']
sizes = sizes
colors = colors
patches, text = plt.pie(sizes, colors=colors, shadow=True, startangle=200)
plt.legend(patches, labels, loc=[1,0])
plt.axis('equal')
plt.title('ATTITUDES SURVEY - SCHOOLS 2014')
plt.show(ScountS14)

Here is the graph:




Notes
  • Adjusted labels, colors, and legends
  • When the cells take too long to load, close notebook and open it up again.
  • Once I delete “autopct='%1.1f%%'” I can’t get it back, I get an error that says “too many values to unpack”
  • Percentages in cell 10 have been appearing and disappearing without me changing the code is this a glitch?
  • I added the command “plt.pie(sizes, autopct='%1.0f%%', pctdistance=1.1, labeldistance=1.2)” This put the percentages outside of the pie chart, but it also changed the colors. Not sure why or how the colors changed…
    • I tried rerunning all the cells and that did not fix the color problem.
    • I tried reverting the cell to the last checkpoint, and then readding the command, but it keeps changing the colors (the colors on the pie chart are different than the colors in the legend)
    • Deleting “sizes,” gives me an error
    • Deleting “labeldistance=1.2” does nothing
    • Changing the # in “auropct” does not fix the colors
  • I tried mimicking the commands shown on () replacing “y” with auctpct’%1.1f%%’ and deleting the line that defines labels (because my labels were already defined. This is what my code looks like:
patches, text = plt.pie(sizes, colors=colors, shadow=True, startangle=200)
plt.legend(patches, labels, loc=[1,0])
patches, texts = plt.pie(autopct='%1.1f%%', colors=colors, startangle=200, radius=1.2)
sort_legend = True
if sort_legend:
patches, labels, dummy =  zip(*sorted(zip(patches, labels, autopct='%1.1f%%'),
                                         key=lambda labels: labels[2],
                                         reverse=True))

plt.legend(patches, labels, loc='left center', bbox_to_anchor=(-0.1, 1.),
          fontsize=8)

plt.savefig('piechart.png', bbox_inches='tight')
plt.axis('equal')
plt.title('ATTITUDES SURVEY - SCHOOLS 2014')
plt.show(ScountS14)

    • This did not work, and gave me this error: TypeError: pie() takes at least 1 argument (4 given)

Comments