我正在創建一個程式,該程式接受用戶輸入并計算輸入中使用的每個字母的實體,然后將其轉換為餅圖。但是,我只希望顯示輸入中使用的字母,并隱藏所有其他字母。這就是我現在所擁有的,這導致所有字母都出現在圖例中,即使它們是 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()
uj5u.com熱心網友回復:
當您為圖例定義標簽時,不要使用線條:
labels = ['%s, %1.1f %%' % (l, s) for l, s in zip(labels, size)]
plt.legend(loc='best', labels=labels)
你可以使用:
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)
這將從圖例中丟棄頻率為 0% 的字母。下面是單詞“stackoverflow”的代碼輸出示例:

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/357819.html
標籤:Python matplotlib
上一篇:熊貓中的影片圖
