Home > OS >  How to hide categories in a piechart that have no data in python?
How to hide categories in a piechart that have no data in python?

Time:11-16

I am creating a program that takes a user input and counts the instances of each letter used in the input, and then turns it into a piechart. However, I only want the letters used in the input to be displayed, and hide all other letters. This is what I have now, that results in all the letters appearing in the legend, even if they are 0%.

list1 = []
answer = str(input('Please describe your thoughts on cats in a couple sentences:'))
list1.extend(answer)
print(list1)
labels = (['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n','o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'])
''' need to count instances of each letter, number, or symbol used'''
a = list1.count('a')
b = list1.count('b')
c = list1.count('c')
d = list1.count('d')
e = list1.count('e')
f = list1.count('f')
g = list1.count('g')
h = list1.count('h')
i = list1.count('i')
j = list1.count('j')
k = list1.count('k')
l = list1.count('l')
m = list1.count('m')
n = list1.count('n')
o = list1.count('o')
p = list1.count('p')
q = list1.count('q')
r = list1.count('r')
s = list1.count('s')
t = list1.count('t')
u = list1.count('u')
v = list1.count('v')
w = list1.count('w')
x = list1.count('x')
y = list1.count('y')
z = list1.count('z')
'''numbers and symbols also need to be calculated to take away from final total'''
one = list1.count('1')
two = list1.count('2')
three = list1.count('3')
four = list1.count('4')
five = list1.count('5')
six = list1.count('6')
seven = list1.count('7')
eight = list1.count('8')
nine = list1.count('9')
period = list1.count('.')
exclamation = list1.count('!')
question = list1.count('?')
comma = list1.count(',')
space = list1.count(' ')
apostraphe = list1.count("'")
pie = (len(list1) - one - two - three - four - five - six - seven - eight - nine - period - exclamation - question - comma - space - apostraphe)
size = [(a/pie)*100, (b/pie)*100, (c/pie)*100, (d/pie)*100, (e/pie)*100, (f/pie)*100, (g/pie)*100, (h/pie)*100, (i/pie)*100, (j/pie)*100, (k/pie)*100, (l/pie)*100, (m/pie)*100, (n/pie)*100, (o/pie)*100, (p/pie)*100, (q/pie)*100, (r/pie)*100, (s/pie)*100, (t/pie)*100, (u/pie)*100, (v/pie)*100, (w/pie)*100, (x/pie)*100, (y/pie)*100, (z/pie)*100] # one/pie, two/pie, three/pie, four/pie, five/pie, six/pie, seven/pie, eight/pie, nine/pie, period/pie, exclamation/pie, question/pie, comma/pie, space/pie]
# fig1, ax1 = plt.subplots()
colors = ['red', 'orange', 'yellow', 'green', 'blue', 'purple', 'springgreen', 'moccasin', 'mediumslateblue', 'teal', 'maroon', 'salmon', 'yellowgreen', 'dodgerblue', 'pink', 'deeppink', 'plum', 'grey', 'white', 'black', 'wheat', 'slategrey', 'gold', 'aqua', 'indigo', 'chartreuse']
plt.pie(size, colors=colors, normalize = True, startangle = 90)
labels = ['%s, %1.1f %%' % (l, s) for l, s in zip(labels, size)]
plt.rcParams["figure.autolayout"] = True
plt.legend(loc='best', labels=labels)
plt.axis('equal')
plt.title("Number of instances of each character in user input")
plt.show()

CodePudding user response:

When you define your labels for your legend, instead of using the lines:

labels = ['%s, %1.1f %%' % (l, s) for l, s in zip(labels, size)]
plt.legend(loc='best', labels=labels)

you could use:

labels_plot = []
for l,s in zip(labels, size):
    if s>0.:
        labels_plot.append('%s, %1.1f %%' % (l, s))
    else: 
        labels_plot.append('_nolegend_')
plt.legend(loc='best', labels=labels_plot)

This will discard the letters with a frequency of 0% from the legend. Below is an example of the output of the code for the word "stackoverflow":

enter image description here

  • Related