用Python在图中打印浮点数(x,y)数据

0 投票
1 回答
3429 浏览
提问于 2025-04-18 05:36

我需要把保存在文件里的数据以这种格式打印出来:

0.1545,0.68954
0.1548,0.87854
0.2545,0.54854
0.7956,0.41548 

(所有的数值都在0.0到1.0之间)

而且还希望能在同一张图上打印多个图形,并且能区分它们(比如用不同的颜色或者线条样式)

有人告诉我用Python来做这个,因为它比较简单,但我看过的所有文档和像这个这样的例子都对我没用。

如果有人能帮我一下,我会非常感激,这对我的论文很重要。我只需要用Python来打印图形,没时间深入学习。

1 个回答

1

要加载数据:

# open the file so you can read from it
with open("myfile.txt") as inf:
    # for each line in the file,
    # split it on commas (results in a list of strings)
    # then convert each string to a float (results in a list of floats)
    items = (map(float, line.split(",")) for line in inf)
    # transpose (convert columns to rows),
    # then assign each row to a variable)
    xs, ys = zip(*items)

要绘制图表:

import matplotlib.pyplot as plt

plt.scatter(xs, ys)
plt.show()

撰写回答