简体   繁体   English

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

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

I have the following data set.我有以下数据集。 I would like to use Python or Gnuplot to plot the data.我想使用 Python 或 Gnuplot 到 plot 数据。 The tuples are of the form (x, y) .元组的形式为(x, y) The Y-axis should be a log axis, that is, log(y) . Y 轴应该是对数轴,即log(y) A scatter plot or line plot would be ideal.散点图 plot 或线 plot 将是理想的。

How can this be done?如何才能做到这一点?

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

If I get your question correctly, you could do something like this.如果我正确理解你的问题,你可以做这样的事情。

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

which would give you something like这会给你类似的东西

在此处输入图片说明

Or as a line plot,或者作为线图,

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

在此处输入图片说明

EDIT - If you want to add a title and labels for the axis, you could do something like编辑- 如果要为轴添加标题和标签,可以执行以下操作

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

which would give you这会给你

在此处输入图片说明

In matplotlib it would be:在 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()

which would produce:这将产生:

在此处输入图片说明

As others have answered, scatter() or plot() will generate the plot you want.正如其他人所回答的那样, scatter()plot()将生成您想要的图。 I suggest two refinements to answers that are already here:我建议对已经在这里的答案进行两项改进:

  1. Use numpy to create the x-coordinate list and y-coordinate list.使用 numpy 创建 x 坐标列表和 y 坐标列表。 Working with large data sets is faster in numpy than using the iteration in Python suggested in other answers.在 numpy 中处理大型数据集比在其他答案中建议的 Python 中使用迭代更快。

  2. Use pyplot to apply the logarithmic scale rather than operating directly on the data, unless you actually want to have the logs.使用 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()

结果

I've also used ax.set_xlim(1, 6) and ax.set_ylim(.1, 1e6) to make it pretty.我还使用了ax.set_xlim(1, 6)ax.set_ylim(.1, 1e6)来使它漂亮。

I've used the object-oriented interface to matplotlib.我已经将面向对象的接口用于 matplotlib。 Because it offers greater flexibility and explicit clarity by using names of the objects created, the OO interface is preferred over the interactive state-based interface.因为它通过使用创建的对象的名称提供了更大的灵活性和明确的清晰度,所以 OO 接口优于交互式基于状态的接口。

You could also use zip你也可以使用 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)

With gnuplot using gplot.pygnuplot使用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.

相关问题 如何绘制在 Python 中表示为元组的行列表? - How do I plot list of lines represented as tuples in Python? 如何使用matplotlib绘制元组列表? - How do I plot a list of tuples with matplotlib? 如何创建一个从元组列表中获取随机元组并在 python 中生成基本 plot 的程序? - how do I create a program that takes random tuples from a list of tuples and generates a basic plot in python? 如何在python中绘制元组列表? - How to plot list of tuples in python? 如何在二维列表(python)上 plot 元组 - how to plot tuples on 2d list (python) Python 元组列表如何 plot 具有相同键的所有元组 - Python list of tuples how to plot all the tuples with the same key 如何在 Python 中以字符串形式编码的元组列表作为列表类型读取,元组作为元组类型读取? - How do I read list of tuples as list type and tuples as tuple type in Python that are encoded in the form of string? 如何在python中相同的元组列表中找到所有元素? - How do i find all the elements in a list of tuples that are same in python? 如何在 python 中使用 for 循环用元组填充列表? - How do I fill a list with with tuples using a for-loop in python? 如何对 Python 中的元组列表进行枚举()? - How do I enumerate() over a list of tuples in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM