2013-08-27 49 views
22

我有以下數據集。我想用python或gnuplot來繪製數據。元組的形式是(x,y)。 Y軸應該是一個對數軸。 I.E.日誌(Y)。散點圖或線圖是理想的。Python:元組的陰謀清單

這怎麼辦?

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

回答

32

如果我正確地得到你的問題,你可以做這樣的事情。

>>> 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() 

這將讓你像

enter image description here

或者作爲一個線圖,

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

enter image description here

編輯 - 如果你想添加一個標題和軸標籤,你可以不喜歡

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

這將使你

enter image description here

+2

如何添加標題和標籤軸? – olliepower

+2

@olliepower:檢查編輯。:) –

+0

OP詢問不清楚是否想在y上記錄座標軸或想要y座標是log(y_data)。你可以使用'plt.semilogy()'而不是'plt.plot()'。 –

9

在matplotlib這將是:

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() 

這將產生:

enter image description here

+0

OP要求y軸爲對數。使用'plt.yscale('log')'爲pyplot交互模式。 –

7

正如其他人已經回答,scatter()plot()將生成你想要的陰謀。我建議對已經在這裏的答案進行兩點改進:

  1. 使用numpy創建x座標列表和y座標列表。使用大數據集在numpy中比在其他答案中使用Python中的迭代更快。

  2. 使用pyplot應用對數刻度而不是直接對數據進行操作,除非您實際上想要擁有日誌。

    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

我也用ax.set_xlim(1, 6)ax.set_ylim(.1, 1e6)使它漂亮。

我用matplotlib的面向對象的接口。因爲它通過使用創建的對象的名稱提供了更大的靈活性和明確的清晰度,所以OO接口優於交互式基於狀態的接口。

1

你也可以使用壓縮

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)