Home > python > python matplotlib: istogramma

python matplotlib: istogramma

Con questa classe e matplotlib, creo un grafico “named bars”,
da una lista[(nome, valore),] o da un dizionario{nome: valore},
passatogli come argomento.

Utilizzando molto sqlite ho considerato le liste nel formato
tipico del metodo fetchall().

codice:

import numpy as np
import matplotlib.pyplot as plt

class Bars(object):
    def __init__(self, data = {}):
        if type (data) == dict:
            print "dict"
            self.data = data
            
        elif type(data) == list:
            self.data = {}
            print "list"
            for item in data:
                self.data[item[0]] = item[1]
        else:
            print "'%s' in ingresso errato: necessari Liste o Dizionari" %type(data) 
        
        try:
            n_bars = len(self.data)
            pts = self.data.values()
            teams = self.data.keys()
            ind = np.arange(n_bars)     # the x locations for bar
            width = 0.2                 # the width of the bars
            plt.subplot(111)
            
            bars = plt.bar(ind + 0.2, pts, width, color='b', align = 'center')
            
            plt.title('Classifica')
            plt.ylabel('pts')
            plt.xticks(ind + width , teams, size = 'x-small', rotation = 15)
            
            for bar in bars:
                val = int(bar.get_height())
                x_pos = bar.get_x() + 0.4
                y_pos = bar.get_height() - 0.1 * bar.get_height()
                plt.text(x_pos, y_pos, '%d'%val, ha='center', va='bottom',
                         size = 'small', rotation = 90)
            plt.show()
        
        except AttributeError:
            print "Grafico non rappresentabile, controllare dato in ingresso"

if __name__ == '__main__':
#    data = 'str'
#    data = {'Boy SAN': 100, 'Cioppersson': 105, 'Stella Blu Kativeria': 120, 'Pippo': 180,
#            'MiddleSboron': 98, 'Nizzi': 140, 'F.C.Zipangolo': 165, 'Real Ancona': 135,
#            'Gnagna': 88, 'Cento': 98}
    data = [('Boy SAN', 100), ('Cioppersson', 105), ('Stella Blu Kativeria', 120), ('Pippo', 180),
            ('MiddleSboron', 98), ('Nizzi', 140), ('F.C.Zipangolo', 165), ('Real Ancona', 135),
            ('Gnagna', 88), ('Cento', 98)]
    hg = Bars(data)

Categorie:python
  1. novembre 5, 2013 alle 7:22 am

    I know this if off topic but I’m looking into starting my own weblog and was wondering what all
    is needed to get set up? I’m assuming having a blog like yours would cost a pretty penny?
    I’m not very web savvy so I’m not 100% certain. Any recommendations or
    advice would be greatly appreciated. Thank you

  2. aprile 18, 2014 alle 1:02 am

    Hi there to all, how is the whole thing, I think every one is getting more
    from this web site, and your views are pleasant in favor of new people.

  3. settembre 6, 2014 alle 9:51 am

    Hello, I do think your site may be having browser compatibility problems.
    When I look at our web site in Safari, it looks fine however, when opening
    in I.E., it’s got some overlapping issues. I just wanted to provide you with
    a quick heads up! Apart from that, exxcellent website!

  4. settembre 19, 2014 alle 1:16 am

    If you desire to improve your familiarity only keep
    visiting this web page and be updated with the hottest news update posted here.

  5. novembre 14, 2014 alle 8:28 pm

    Because the admin of this website is working, no question very quickly itt will be famous, due to its feature contents.

  1. No trackbacks yet.

Lascia un commento

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...

%d blogger cliccano Mi Piace per questo: