如何根据文件中不存在的列来绘制csv文件中的列?

时间:2017-11-16 01:30:18

标签: python matplotlib

所以我对使用csvfiles的python和matplotlib很新。我的问题是试图绘制下图。我在创建x变量时遇到问题,我需要对y,'门'进行绘图。更具体地说,我希望能够根据汽车ID绘制“门”的数量,可以假设为0,1,2,3,4,5,6,7 ......等等。 “门”栏中的条目。如果没有专门针对汽车ID的专栏,我该怎么做呢?

import matplotlib.pyplot as plt
import csv

x = []
y = []

with open('9car.data.csv','r') as csvfile:
    plots = csv.reader(csvfile, delimiter=',')

1 个答案:

答案 0 :(得分:1)

来自pyplot.plot documentation

  

例如,以下各项都是合法的:

plot(x, y)        # plot x and y using default line style and color  
plot(x, y, 'bo')  # plot x and y using blue circle markers   
plot(y)           # plot y using x as index array 0..N-1 

因此这样的事情会起作用

import matplotlib.pyplot as plt
import csv

with open('data/estimated.csv','r') as csvfile:
    plots = csv.reader(csvfile, delimiter=',')    
    y = [float(row[1]) for row in plots]

plt.plot(y)

plt.show()

甚至更好,使用numpy

import matplotlib.pyplot as plt
import numpy as np
x,y = np.loadtxt('data/estimated.csv', delimiter=',',unpack=True)

plt.plot(y)

plt.show()