如何在 Python 中绘制元组列表? [英] How do I plot list of tuples in Python?
本文介绍了如何在 Python 中绘制元组列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下数据集.我想使用 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)]
解决方案
如果我正确理解你的问题,你可以这样做.
<预><代码>>>>导入 matplotlib.pyplot 作为 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)]>>>从数学导入日志>>>testList2 = [(elem1, log(elem2)) for elem1, elem2 in testList]>>>测试列表2[(0, -16.617236475334405), (1, -17.67799605473062), (2, -18.691431541177973), (3, -18.9767093108359), (4,20108359), (4,2017), -42017), -420177359>>>zip(*testList2)[(0, 1, 2, 3, 4, 5), (-16.617236475334405, -17.67799605473062, -18.691431541177973, -18.9767093,108935909)-18.97670931089359073081820709161616161616161616182016>>>plt.scatter(*zip(*testList2))>>>plt.show()这会给你类似的东西
或者作为线图,
<预><代码>>>>plt.plot(*zip(*testList2))>>>plt.show()EDIT - 如果要为轴添加标题和标签,可以执行类似操作
<预><代码>>>>plt.scatter(*zip(*testList2))>>>plt.title('随机人物')>>>plt.xlabel('X 轴')>>>plt.ylabel('Y 轴')>>>plt.show()哪个会给你
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.
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
这篇关于如何在 Python 中绘制元组列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文