如何绘制元组列表?

问题描述 投票:0回答:6

我有以下数据集。我想使用 Python 或 Gnuplot 来绘制数据。元组的形式为

(x, y)
。 Y 轴应该是对数轴,即
log(y)
。散点图或线图是理想的选择。

这怎么办?

 [(0, 6.0705199999997801e-08), (1, 2.1015700100300739e-08), 
 (2, 7.6280656623374823e-09), (3, 5.7348209304555086e-09), 
 (4, 3.6812203579604238e-09), (5, 4.1572516753310418e-09)]
python numpy matplotlib scipy gnuplot
6个回答
117
投票

如果我正确地回答了你的问题,你可以做这样的事情。

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


31
投票

在 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


19
投票

正如其他人回答的那样,

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 接口优于基于交互式状态的接口。


19
投票

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

2
投票

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

enter image description here


0
投票

也可以用 Pandas 来完成。

import pandas as pd
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)]

pd.DataFrame(data, columns=['l','v']).set_index('l').plot(kind='line');

© www.soinside.com 2019 - 2024. All rights reserved.