Tenebris Mortem
Tenebris Mortem

Reputation: 93

Pie chart color text matplotlib

I am tasked to make a pie chart based on the passing students and the failing students. I'm wondering is there a way in changing the text in the pie chart. The black is being covered by the fail part and the White is being covered by the back ground

import random import matplotlib.pyplot as plt

def main():
    students = get_students()
    Student_Count = Random_Grades(students)
    Total = Average (Student_Count,students)
    Pie_Graph(Total,students)
def get_students():
    students = int(input("How many students do you want to enter? \n Students: "))
    while(students > 50):
        print("The data size must be less than or equal to 50")
        students = int(input("How many students do you want to enter? \n Students: "))
    return students
def Random_Grades(students):
    Student_Count =  [([0] * 4) for i in range(students)]
    for a in range(students):
        for b in range(4):
            Student_Count[a][b] = random.randint(1,100)
    return Student_Count
def Average (Student_Count,students):
    total = 0
    Total  = [] # will put the Total in final row
    for a in range (students):
        for b in range(4):
            total += Student_Count [a][b]
        Average = total/4
        Total.append(Average) # will put the total value in the Total list
        total = 0
    return Total
def Pie_Graph(Total,students):
    b = 0
    c = 0
    for a in range (students):
        if Total[a] >= 60:
            b += 1
        elif Total[a] < 60:
            c += 1
    values=[b,c]
    slice_labels=(['Pass','Fail'])
    explode = [0.05,0.05]
    plt.pie(values, labels=slice_labels, colors=('b','k'),autopct="%.1f%%",explode=explode,textprops={'color':"w"})
    plt.title('Student Pass/Fail Chart')
    plt.show()
print()
x="Y"
while(x!="N"):
    main()
    x=input("Press Y to continue, N to stop : ").upper()

Output

enter image description here

Expected Output

enter image description here

Upvotes: 0

Views: 1177

Answers (2)

Zabir Al Nazi Nabil
Zabir Al Nazi Nabil

Reputation: 11198

You can use custom autopct to render any string in the piechart you want, you can use legend to mark which color corresponds to which set.

There are many ways to set a border, patch.Rectangle, or just using axes with piechart.

def make_autopct(values):
    def my_autopct(pct):
        total = sum(values)
        val = int(round(pct*total/100.0))
        return '{p}%'.format(p=val)
    return my_autopct

def Pie_Graph(Total,students):
    b = 0
    c = 0
    for a in range (students):
        if Total[a] >= 60:
            b += 1
        elif Total[a] < 60:
            c += 1
    values=[b,c]
    slice_labels=(['Pass','Fail'])
    explode = [0.05,0.05]

    fig1, ax1 = plt.subplots(figsize=(5, 5))
    fig1.subplots_adjust(0.1, 0.1, 1, 1)

    b = ax1.pie(values, labels=slice_labels, colors=('b','k'),autopct= make_autopct(values),explode=explode,textprops={'color':"w"})

    plt.legend(['Pass', 'Fail'], loc = 8, bbox_to_anchor=(0.5, -0.25), ncol=2, fancybox=True)
    plt.title('Student Pass/Fail Chart')
    ax1.axis('equal')
    plt.show()

Upvotes: 1

Murtaza Nazir
Murtaza Nazir

Reputation: 91

You just need to add a legend.

use,

plt.legend()

just before plt.show()

You can change the location of the legend as well.

Upvotes: 0

Related Questions