如何在Python中绘制元组列表? [英] How do I plot list of tuples in Python?
本文介绍了如何在Python中绘制元组列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下数据集.我想使用Python或Gnuplot绘制数据.元组的格式为(x, y)
. Y轴应为对数轴,即log(y)
.散点图或折线图将是理想的.
I have the following data set. I would like to use Python or Gnuplot to plot the data. The tuples are of the form (x, y)
. The Y-axis should be a log axis, that is, log(y)
. A scatter plot or line plot would be ideal.
这怎么办?
[(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()
这会给你类似的东西
或者作为线条图,
>>> 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()
这会给你
这篇关于如何在Python中绘制元组列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文