【问题标题】:Print float (x,y) data in a graph with Python使用 Python 在图形中打印浮点 (x,y) 数据
【发布时间】:2014-05-06 23:18:22
【问题描述】:

我需要打印以这种格式保存在文件中的数据:

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

(0.0 到 1.0 之间的所有值) 并且还可以在同一图中打印多个图表来区分它们(即:颜色或线条样式)

我被告知要使用 python 来解决这个问题,因为它很容易,但我读过的所有文档和this one 之类的例子都不适合我。

如果有人可以帮助我的论文,我将不胜感激。图形打印只需要python,没时间深入学习。

【问题讨论】:

  • 为什么不自己跟着写代码呢?
  • matplotlib 中的哪些内容不适合您?你能详细说明一下吗?
  • 为什么不能只使用 wolfram alpha 之类的东西来绘制你的点?

标签: python matplotlib


【解决方案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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多