在Python中绘制列表

时间:2016-06-01 09:14:35

标签: python list matplotlib

我正在尝试制作两个列表的情节,一次是用timeit测量的时间,另一个是我在循环中测量的次数。我只得到一个空的情节,所以我认为有些事情是错的。有人可能会告诉我错误在哪里吗? 这些功能并不重要,但我会发布整个内容以提供上下文。 这是代码:

import random
import timeit
import matplotlib.pyplot as plt

def generateSequences(n):

    RandomSequences = []
    dna = ["A","G","C","T"]
    for i in range(int(n)):

        randseq=''

        for i in range(50):
            randseq+=random.choice(dna)

        RandomSequences.append(randseq)

    return RandomSequences

def generatePrefixes(p, RandomSequences):

    First20Chars = [x[:20] for x in RandomSequences]
    RandomChoices = []
    for i in range(p):
        randomPrefix = random.choice(First20Chars)
        RandomChoices.append(randomPrefix)

    return First20Chars, RandomChoices

def searchReadsInList(RandomSequences, RandomChoices):

    start_time = timeit.default_timer()
    Matches_RS_RC = []
    for i in RandomChoices:
        for j in RandomSequences:
            if i in j:
                Matches_RS_RC.append(j)
    elapsed_sRL = timeit.default_timer() - start_time
    return Matches_RS_RC, elapsed_sRL



if __name__ == "__main__":
    count = 10
    while count < 1000:
        RandomSequences = generateSequences(count)
        First20Chars, RandomChoices = generatePrefixes(5, RandomSequences)
        Matches_RS_RC, elapsed_sRL = searchReadsInList(RandomSequences, RandomChoices)
        ListCounts = []
        ListCounts.append(count)
        ListTime = []
        ListTime.append(elapsed_sRL)
        count = count + 10

    plt.plot(ListTime, count)
    plt.xlabel('Time')
    plt.ylabel('# of Reads')
    plt.savefig("TimePlot.pdf")
    plt.show()

1 个答案:

答案 0 :(得分:1)

我改进了main函数,你在每次迭代中清除了列表:

if __name__ == "__main__":
    count = 10
    ListCounts = []
    ListTime = []

    while count < 1000:
        RandomSequences = generateSequences(count)
        First20Chars, RandomChoices = generatePrefixes(5, RandomSequences)
        Matches_RS_RC, elapsed_sRL = searchReadsInList(RandomSequences, RandomChoices)
        ListCounts.append(count)
        ListTime.append(elapsed_sRL)
        count = count + 10

    print ListCounts
    print ListTime

    plt.plot(ListTime, ListCounts)
    plt.xlabel('Time')
    plt.ylabel('# of Reads')
    plt.savefig("TimePlot.pdf")
    plt.show()

enter image description here