2017-04-22 4 views
1

高値から低値に並べ替える特定の値が辞書にありますが、最大1000個以上の値があります。 1から10の間の数字で、dictの上位1〜10の最高値を持つグラフの出力を与える場合、どのようにしてユーザー入力を受け取ることができますか。それは3つの最高値などをグラフ化します彼らは入力3にあれば...おかげで、高度なユーザーの入力に応じて、最高値から次の値への値のプロット方法

from collections import Counter 
from scipy import * 
from matplotlib.pyplot import * 
import matplotlib.pyplot as plot 


frequency1 = Counter({'1':100,'2':400,'3':200,'4':300,}) 



response1 = input("How many top domains from source? Enter a number between 1-10: ") 

if response1 == "1":   
    if len(frequency1) >= 1: 

     print("\nTop 1 most is:") 
     for key, frequency1_value in frequency1.most_common(1): 
       print("\nNumber:",key,"with frequency:",frequency1_value) 

       ########Graph for this output 

       x = [1,2] 
       y = [frequency1_value,0] 

       figure(1) 

       ax = plot.subplot(111) 
       ax.bar(x,y,align='center', width=0.2, color = 'm') 


       ax.set_xticklabels(['0', '1']) 
       xlabel("This graph shows amount of protocols used") 
       ylabel("Number of times used") 
       grid('on')  


################################## END GRAPH 
    else: 
     print("\nThere are not enough domains for this top amount.") 

if response1 == "2":   
    if len(frequency1) >= 2: 
     print("\nTop 2 most is:") 
     for key, frequency1_value in frequency1.most_common(2): 
       print("\nNumber:",key,"with frequency:",frequency1_value) 

       ########Graph for this output 

       x = [1,2,3] 
       y = [frequency1_value,frequency1_value,0] 

       figure(1) 

       ax = plot.subplot(111) 
       ax.bar(x,y,align='center', width=0.2, color = 'm') 


       ax.set_xticklabels(['0', '1','','2']) 
       xlabel("This graph shows amount of protocols used") 
       ylabel("Number of times used") 
       grid('on')  


################################## END GRAPH 

    else: 
     print("\nThere are not enough domains for this top amount.") 

答えて

1

に以下のコードは、辞書内の値のソートされたリストを作成し、最大の対応するグラフをプロットします数字は、ユーザーの入力に応じて異なります。例えば

import numpy as np 
import matplotlib.pyplot as plt 

d = {'1':100,'2':400,'3':200,'4':300,} 

vals = sorted(d.values(), reverse=True) 

response = input("How many top domains from source? Enter a number between 1-10: ") 

if response > 0 and response < len(vals)+1: 

    y = vals[:response] 

    print ("\nTop %i most are:" %response) 
    print (y) 

    x = np.arange(1,len(y)+1,1) 

    fig, ax = plt.subplots() 

    ax.bar(x,y,align='center', width=0.2, color = 'm') 

    ax.set_xticks(x) 
    ax.set_xticklabels(x) 
    ax.set_xlabel("This graph shows amount of protocols used") 
    ax.set_ylabel("Number of times used") 
    ax.grid('on') 
else: 
    print ("\nThere are not enough domains for this top amount.") 

plt.show() 

ユーザーがコードに3を入力した場合、次のグラフは、出力を生成することになります。あなたの応答のための

Top 3 most are: 
[400, 300, 200] 

enter image description here

+0

ありがとう!これはpython 3.5で動作しますか? 19行目にstr()> int()というエラーが出ます。 – k5man001

+0

実際には、私はそれを得ました、入力はintにキャストする必要がありました..あなたの助けをありがとう! – k5man001

関連する問題