使用Matplotlib创建线图

时间:2017-10-29 23:49:15

标签: python matplotlib

我的主要目标是使用matplotlib创建一个线图。但每次,我都会得到散点图。 这是我的示例代码:

import matplotlib.pyplot as plt

def function():
    with open(filename) as f:
            next(f) #i want to skip first line
            for i,line in enumerate(f):
                x=line.split(',')
                a=[float (j) for j in x] #list 'a' now has float values not strings

                OY=a[2:34] #creating list which will start from index 2 

                OX=a[1] #creating list which has only values with index 1

                plt.plot(OX, OY,'-',color='red')


    plt.show()

function()

不幸的是我得到了散点图,但我预计会有一个线图。这有什么不对?我怎样才能改变它?

1 个答案:

答案 0 :(得分:0)

您可以通过替换...

来获得线图
OX=a[1] ## results in ValueError: x and y must have same first dimension

......有了......

OX=numpy.arange(len(OY)) ## create a linear list of indexes

我假设您的数据行遵循此表单...

2, 4, 8, 16, 32, 64, 128, 256