2013-08-27 20 views
22

Ho il seguente set di dati. Vorrei usare python o gnuplot per tracciare i dati. Le tuple hanno forma (x, y). L'asse Y dovrebbe essere un asse di registro. OSSIA log (y). Un grafico a dispersione o trama lineare sarebbe l'ideale.Python: lista di tuple di stampa

Come si può fare?

[(0, 6.0705199999997801e-08), (1, 2.1015700100300739e-08), 
(2, 7.6280656623374823e-09), (3, 5.7348209304555086e-09), 
(4, 3.6812203579604238e-09), (5, 4.1572516753310418e-09)] 

risposta

32

Se ottengo la tua domanda correttamente, potresti fare qualcosa di simile.

>>> import matplotlib.pyplot as plt 
>>> testList =[(0, 6.0705199999997801e-08), (1, 2.1015700100300739e-08), 
(2, 7.6280656623374823e-09), (3, 5.7348209304555086e-09), 
(4, 3.6812203579604238e-09), (5, 4.1572516753310418e-09)] 
>>> from math import log 
>>> testList2 = [(elem1, log(elem2)) for elem1, elem2 in testList] 
>>> testList2 
[(0, -16.617236475334405), (1, -17.67799605473062), (2, -18.691431541177973), (3, -18.9767093108359), (4, -19.420021520728017), (5, -19.298411635970396)] 
>>> zip(*testList2) 
[(0, 1, 2, 3, 4, 5), (-16.617236475334405, -17.67799605473062, -18.691431541177973, -18.9767093108359, -19.420021520728017, -19.298411635970396)] 
>>> plt.scatter(*zip(*testList2)) 
>>> plt.show() 

che darebbe qualcosa come

enter image description here

O come una trama di linea,

>>> plt.plot(*zip(*testList2)) 
>>> plt.show() 

enter image description here

EDIT - Se si desidera aggiungere un titolo e le etichette per l'asse, si potrebbe fare qualcosa di simile

>>> plt.scatter(*zip(*testList2)) 
>>> plt.title('Random Figure') 
>>> plt.xlabel('X-Axis') 
>>> plt.ylabel('Y-Axis') 
>>> plt.show() 

che darebbe

enter image description here

+2

Come faccio ad aggiungere ad un asse titolo e l'etichetta? – olliepower

+2

@olliepower: controlla la modifica.:) –

+0

L'interrogazione dell'OP non era chiara se si desiderava che l'asse del registro fosse y o che si volessero le coordinate y (y_data). Potresti usare 'plt.semilogy()' invece di 'plt.plot()'. –

9

In matplotlib sarebbe:

import matplotlib.pyplot as plt 

data = [(0, 6.0705199999997801e-08), (1, 2.1015700100300739e-08), 
(2, 7.6280656623374823e-09), (3, 5.7348209304555086e-09), 
(4, 3.6812203579604238e-09), (5, 4.1572516753310418e-09)] 

x_val = [x[0] for x in data] 
y_val = [x[1] for x in data] 

print x_val 
plt.plot(x_val,y_val) 
plt.plot(x_val,y_val,'or') 
plt.show() 

che produrrebbe:

enter image description here

+0

L'OP ha chiesto che l'asse y sia logaritmico. Usa 'plt.yscale ('log')' per la modalità interattiva di pyplot. –

7

Come altri hanno risposto, scatter() o plot() genererà la trama desiderata. Suggerisco due perfezionamenti alle risposte che sono già qui:

  1. Usa numpy per creare l'elenco coordinata x e la lista coordinata y. Lavorare con insiemi di dati di grandi dimensioni è più veloce in numpy che usare l'iterazione in Python suggerita in altre risposte.

  2. Utilizzare pyplot per applicare la scala logaritmica anziché operare direttamente sui dati, a meno che non si desideri effettivamente avere i registri.

    import matplotlib.pyplot as plt 
    import numpy as np 
    
    data = [(2, 10), (3, 100), (4, 1000), (5, 100000)] 
    data_in_array = np.array(data) 
    ''' 
    That looks like array([[  2,  10], 
             [  3, 100], 
             [  4, 1000], 
             [  5, 100000]]) 
    ''' 
    
    transposed = data_in_array.T 
    ''' 
    That looks like array([[  2,  3,  4,  5], 
             [ 10, 100, 1000, 100000]]) 
    '''  
    
    x, y = transposed 
    
    # Here is the OO method 
    # You could also the state-based methods of pyplot 
    fig, ax = plt.subplots(1,1) # gets a handle for the AxesSubplot object 
    ax.plot(x, y, 'ro') 
    ax.plot(x, y, 'b-') 
    ax.set_yscale('log') 
    fig.show() 
    

result

Ho anche usato ax.set_xlim(1, 6) e ax.set_ylim(.1, 1e6) per renderlo abbastanza.

Ho usato l'interfaccia orientata agli oggetti su matplotlib. Poiché offre maggiore flessibilità e chiarezza esplicita utilizzando i nomi degli oggetti creati, l'interfaccia OO è preferibile all'interfaccia interattiva basata sullo stato.

1

Si potrebbe anche usare zip

import matplotlib.pyplot as plt 

l = [(0, 6.0705199999997801e-08), (1, 2.1015700100300739e-08), 
    (2, 7.6280656623374823e-09), (3, 5.7348209304555086e-09), 
    (4, 3.6812203579604238e-09), (5, 4.1572516753310418e-09)] 

x, y = zip(*l) 

plt.plot(x, y)