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

标签 python numpy matplotlib scipy gnuplot

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

最佳答案

如果我没有正确回答您的问题,您可以这样做。

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

关于python - 如何在 Python 中绘制元组列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18458734/

相关文章:

Python:LogLog 图与线性图相结合

python - 如何在我的损失函数中添加 L2 正则化项

Python - Mechanize - 如何知道它是否有效?

python - `numpy.einsum` 是如何工作的?

python numpy : indexing broadcast

python - 更改极坐标投影子图的大小

python - Django 事件流 : Apps aren't loaded yet

python - 我可以使用 Django Channels 实时显示 MQTT 消息吗?

arrays - Numpy 连接很慢 : any alternative approach?

python - matplotlib:在同一轴上使用 plot 和 imshow 时的限制