scipy 如何在Python中绘制元组列表?

vsnjm48y  于 2023-05-17  发布在  Python
关注(0)|答案(6)|浏览(102)

我有以下数据集。我想使用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)]
mpbci0fu

mpbci0fu1#

如果我没理解错的话,你可以这样做。

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

EDIT-如果要为轴添加标题和标签,可以执行以下操作

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

这会给予你

dffbzjpn

dffbzjpn2#

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

这将产生:

nr9pn0ug

nr9pn0ug3#

正如其他人所回答的那样,scatter()plot()将生成您想要的图。我建议对已经在这里的答案做两个改进:
1.使用numpy创建x坐标列表和y坐标列表。使用numpy处理大型数据集比使用其他答案中建议的Python迭代更快。
1.使用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接口通过使用创建的对象的名称提供了更大的灵活性和明确的清晰性,所以它比基于状态的交互式接口更受欢迎。

zvokhttg

zvokhttg4#

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

zf2sa74q5#

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

bzzcjhmw

bzzcjhmw6#

也可以用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');

相关问题