用Python绘制列表

用Python绘制列表,python,list,matplotlib,Python,List,Matplotlib,我试着画出两个列表,一个是用timeit测量时间,另一个是我在循环中运行的次数。我只得到了一个空的情节,所以我觉得有点不对劲。谁能告诉我错在哪里吗? 这些函数并不是很重要,但我将发布整个过程以提供上下文。 代码如下: import random import timeit import matplotlib.pyplot as plt def generateSequences(n): RandomSequences = [] dna = ["A","G","C","T"]

我试着画出两个列表,一个是用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()
随机导入
导入时间信息
将matplotlib.pyplot作为plt导入
def生成序列(n):
随机序列=[]
dna=[“A”、“G”、“C”、“T”]
对于范围内的i(int(n)):
randseq=''
对于范围(50)内的i:
随机选择(dna)
RandomSequences.append(randseq)
返回随机序列
def generatePrefixes(p,随机序列):
First20Chars=[x[:20]表示随机序列中的x]
随机选择=[]
对于范围(p)内的i:
randomPrefix=random.choice(前20个字符)
RandomChoices.append(随机前缀)
返回前20个字符,随机选择
def searchReadsInList(随机序列、随机选项):
start\u time=timeit.default\u timer()
匹配\u RS\u RC=[]
对于随机选择中的i:
对于随机序列中的j:
如果我在j中:
匹配\u RS\u RC.append(j)
已用\u sRL=timeit.default\u timer()-开始\u时间
返回匹配项\u RS\u RC,经过的\u sRL
如果名称=“\uuuuu main\uuuuuuuu”:
计数=10
当计数小于1000时:
随机序列=生成序列(计数)
First20Chars,RandomChoices=generatePrefixes(5,RandomSequences)
匹配\u RS\u RC,经过的\u sRL=searchReadsInList(随机序列,随机选择)
ListCounts=[]
追加(计数)
ListTime=[]
追加(已用\u sRL)
计数=计数+10
plt.绘图(列表时间、计数)
plt.xlabel(“时间”)
plt.ylabel(“#of Reads”)
plt.savefig(“TimePlot.pdf”)
plt.show()

我改进了主功能,您在每次迭代中都清除了列表:

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()
如果名称=“\uuuuu main\uuuuuuuu”:
计数=10
ListCounts=[]
ListTime=[]
当计数小于1000时:
随机序列=生成序列(计数)
First20Chars,RandomChoices=generatePrefixes(5,RandomSequences)
匹配\u RS\u RC,经过的\u sRL=searchReadsInList(随机序列,随机选择)
追加(计数)
追加(已用\u sRL)
计数=计数+10
打印列表计数
打印列表时间
plt.绘图(列表时间、列表计数)
plt.xlabel(“时间”)
plt.ylabel(“#of Reads”)
plt.savefig(“TimePlot.pdf”)
plt.show()

您的列表仅存储两个值:[990]和[0.0012807846069335938]。打印时使用
plt.plot(ListTime,ListCounts)