簡體   English   中英

如何在 Python 中列出 plot 元組列表?

[英]How do I plot list of tuples in Python?

我有以下數據集。 我想使用 Python 或 Gnuplot 到 plot 數據。 元組的形式為(x, y) Y 軸應該是對數軸,即log(y) 散點圖 plot 或線 plot 將是理想的。

如何才能做到這一點?

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

如果我正確理解你的問題,你可以做這樣的事情。

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

這會給你類似的東西

在此處輸入圖片說明

或者作為線圖,

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

在此處輸入圖片說明

編輯- 如果要為軸添加標題和標簽,可以執行以下操作

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

這會給你

在此處輸入圖片說明

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

這將產生:

在此處輸入圖片說明

正如其他人所回答的那樣, 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()

結果

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

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

你也可以使用 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)

gnuplot使用gplot.py

from gplot import *

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

gplot.log('y')
gplot(*zip(*l))

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM