2013-04-26 5 views
3

J'ai un dictionnaire band1 comme indiqué ci-dessous, je veux imprimer un graphique basé sur le premier et le dernier élément de chaque liste dans le dictionnaire. Le premier élément de chaque liste sur l'axe des x, est une fréquence et le dernier élément est une force de réception et devrait être sur l'axe y. Par exemple, 10812 a une résistance de 16 etcAscii graphique en python

band1 = {'channel1': [10564, 2112.8, 1922.8, 0], 
     'channel10': [10787, 2157.4, 1967.4, 11], 
     'channel11': [10812, 2162.4, 1972.4, 16], 
     'channel12': [10837, 2167.4, 1977.4, 46], 
     'channel2': [10589, 2117.8, 1927.8, 29], 
     'channel3': [10612, 2122.4, 1932.4, 0], 
     'channel4': [10637, 2127.4, 1937.4, 40], 
     'channel5': [10662, 2132.4, 1942.4, 54], 
     'channel6': [10687, 2137.4, 1947.4, 0], 
     'channel7': [10712, 2142.4, 1952.4, 50], 
     'channel8': [10737, 2147.4, 1957.4, 19], 
     'channel9': [10762, 2152.4, 1962.4, 24]} 

Je n'ai aucun problème de tri cela, channel1 -> channel12 mais ce sont des façons agréables pour imprimer un joli graphique, le nombre d'entrées dans le dictionnaire peut varier avec il y a plus ou moins de canaux.

+1

Avez-vous envisagé d'utiliser une bibliothèque graphique comme [pyplot] (http://matplotlib.org/api/pyplot_api.html)? Cela devrait être assez facile à faire avec pyplot. –

+0

Merci je suis intéressé de voir quelles bibliothèques il y a mais aussi de voir s'il n'y a pas de bonne manière sans télécharger quoi que ce soit d'extra. Cela sera simplement imprimé dans la console. Jetez un oeil à pyplot maintenant acclamations. – Paul

+0

Je ne connais pas de solutions graphiques ASCII, mais vous pourriez probablement mettre quelque chose ensemble à la main assez facilement. Si les premier et dernier éléments de chaque liste sont respectivement les axes x et y, comment indiqueriez-vous les noms des canaux, ou n'est-ce pas important? Cela pourrait être utile si vous donniez un exemple simple de l'apparence du graphique résultant. – Aya

Répondre

4

Voici un algorithme de diagramme à points, aussi simple et naïf qu'il pourrait l'être. Bien sûr, ses performances sont loin d'être performantes et pourraient être optimisées, de même que la sortie pourrait comporter quelques axes et numéros.

HEIGHT = 10 
WIDTH = 40 
MARKER = '*' 
FILL_CHARACTER = ' ' 

coords = [(ch[0], ch[3]) for ch in band1.values()] 


# Convert to coordinates of a desired ASCII area 

xmin = min(c[0] for c in coords) 
xmax = max(c[0] for c in coords) 
kx = (WIDTH - 1)/(xmax - xmin) 

ymin = min(c[1] for c in coords) 
ymax = max(c[1] for c in coords) 
ky = (HEIGHT - 1)/(ymax - ymin) 

acoords = [(round((c[0] - xmin) * kx), 
      round((c[1] - ymin) * ky)) for c in coords] 

# Actually draw the graph 

for y in range(HEIGHT, -1, -1): 
    chars = [] 
    for x in range(WIDTH): 
     if (x, y) in acoords: 
      chars.append(MARKER) 
     else: 
      chars.append(FILL_CHARACTER) 
    print(''.join(chars)) 

Résultats à:

   *       
        *     * 
      *        

    *         
          *   
         *   *  
           *  

*  *   *      

Si x coordonnées sont uniques, il peut être modifié facilement pour dessiner des diagrammes de colonne ou ligne.

E.g. pour le cas de barres:

HEIGHT = 10 
WIDTH = 40 
MARKER = '*' 
FILL_CHARACTER = ' ' 

coords = [(ch[0], ch[3]) for ch in band1.values()] 
coords.sort(key=lambda ch: ch[1]) 

xmin = min(c[0] for c in coords) 
xmax = max(c[0] for c in coords) 
kx = (WIDTH - 1)/(xmax - xmin) 

ymin = min(c[1] for c in coords) 
ymax = max(c[1] for c in coords) 
ky = (HEIGHT - 1)/(ymax - ymin) 

acoords = {} 
for c in coords: 
    x = round((c[0] - xmin) * kx) 
    y = round((c[1] - ymin) * ky) 
    if x not in acoords: 
     acoords[x] = y 
    else: 
     acoords[x] = max(acoords[x], y) 

for y in range(HEIGHT, -1, -1): 
    chars = [] 
    for x in range(WIDTH): 
     if acoords.get(x, 0) >= y: 
      chars.append(MARKER) 
     else: 
      chars.append(FILL_CHARACTER) 
    print(''.join(chars)) 

Résultats à:

   *       
       *  *     * 
      * *  *     * 
      * *  *     * 
    *  * *  *     * 
    *  * *  *  *   * 
    *  * *  * * *  * * 
    *  * *  * * * * * * 
    *  * *  * * * * * * 
**************************************** 
+0

Merci, j'aime ça! – Paul

+0

@Paul De rien! – Alexey