读取两列并绘制图形

时间:2017-12-07 14:55:44

标签: python csv matplotlib

我已经问过我的问题:How to read two columns using python,但我的目标是绘制图表oX = f(oY)

我尝试过:

import matplotlib.pyplot as plt
from matplotlib import style    
import csv

with open('file.csv') as f:
    reader = csv.reader(f)
    for row in reader:
        res = row[0].split()
        oX = res[0]
        oY = res[1]
        print (oX , oY)

这对我不起作用。

1 个答案:

答案 0 :(得分:1)

只有在使用matplotlib 2.1.0或更高版本时才能使用scatter绘制标签(字符串)。 2.1.0是撰写本答案时的最新稳定版本。

import pandas as pd
import json
from matplotlib import pyplot as plt

import csv
x = []
y = []
with open('e:/projects/data.txt') as f:
    reader = csv.reader(f)
    for row in reader:
        res = row[0].split()
        oX = res[0]
        oY = res[1]
        x.append(oX)
        y.append(oY)
        print (oX, oY)

仅使用原始代码绘制输入文件中的最后一个坐标。您需要将oXoY存储在列表中,例如xy

plt.scatter (x, y, linewidth = 0.1, color = "black", label = 'V')
plt.show ()

使用其他问题的输入,您的图表应如下所示