首页 > 解决方案 > 收到一段代码的错误,但没有收到类似代码段的错误

问题描述

我一直在

TypeError:“str”对象不可调用

对于下面的函数,何时style == "scatter"而不是何时style == "categories",我一生都无法弄清楚为什么,对我来说,何时该函数似乎应该在两个地方做同样的事情。关于我做错了什么的任何想法?

编辑:此错误发生在线路graph = graph + "\\node at (%s,%s)[circle,fill,inner sep=1pt]{};\n"(x+random.uniform(-0.5,0.5),y+random.uniform(-0.5,0.5))

def scatterPlotCreator(style,obs,corr):
    obs = int(obs)
    graph="""
    \\documentclass[11pt]{article}
    \\usepackage{geometry}
    \\usepackage{amsmath}
    \\usepackage{amssymb}
    \\usepackage{textpos}
    \\usepackage{setspace}
    \\usepackage{color}
    \\usepackage{tikz}
    \\usetikzlibrary{calc,arrows,automata,shapes.misc,shapes.arrows,chains,matrix,positioning,scopes,decorations.pathmorphing,shadows}
    \\usepackage{graphicx} 
    \\usepackage{everypage}
    \\setlength\\TPHorizModule{1in}
    \\setlength\\TPVertModule{1in}

    \\topmargin .25 in 
    \\headheight 0in 
    \\headsep 0in 
    \\textheight 8.5in 
    \\textwidth 5.5in 
    \\oddsidemargin .5in 
    \\evensidemargin .5in 


    \\begin{document}
     \\pagestyle{empty}

    \\begin{tikzpicture}
    \\draw [very thick, ->] (0,0) -- (5,0);
    \\draw [very thick, ->] (0,0) -- (0,5);
    """

    if style == "scatter":
        if corr == "NONE":
            for o in range(obs):
                graph = graph + "\\node at (%s,%s)[circle,fill,inner sep=1pt]{};\n"(random.uniform(0,5),random.uniform(0,5))
        elif corr == "weakPos":
            for o in range(obs):
                x = random.uniform(2,3)
                y = x
                graph = graph + "\\node at (%s,%s)[circle,fill,inner sep=1pt]{};\n"(x+random.uniform(-2,2),y+random.uniform(-2,2))
        elif corr == "strongPos":
            for o in range(obs):
                x = random.uniform(0.5,4.5)
                y = x
                graph = graph + "\\node at (%s,%s)[circle,fill,inner sep=1pt]{};\n"(x+random.uniform(-0.5,0.5),y+random.uniform(-0.5,0.5))
        elif corr == "weakNeg":
            for o in range(obs):
                x = random.uniform(2,3)
                y = 5-x
                graph = graph + "\\node at (%s,%s)[circle,fill,inner sep=1pt]{};\n"(x+random.uniform(-2,2),y+random.uniform(-2,2))
        elif corr == "strongNeg":
            for o in range(obs):
                x = random.uniform(0.5,4.5)
                x = float(x)
                y = float(5-x)
                x+=random.uniform(-0.5,0.5)
                y+=random.uniform(-0.5,0.5)
                graph = graph + "\\node at (%s,%s)[circle,fill,inner sep=1pt]{};\n"(x,y)
        elif corr == "posQuad":
            for o in range(obs):
                x = random.uniform(0.5,4.5)
                y = (x-2.5)**2
                graph = graph + "\\node at (%s,%s)[circle,fill,inner sep=1pt]{};\n"(x+random.uniform(-0.5,0.5),y+random.uniform(-0.5,0.5))
        elif corr == "negQuad":
            for o in range(obs):
                x = random.uniform(0.5,4.5)
                y = 5-(x-2.5)**2
                graph = graph + "\\node at (%s,%s)[circle,fill,inner sep=1pt]{};\n"(x+random.uniform(-0.5,0.5),y+random.uniform(-0.5,0.5))
    elif style == "categories":
        categories = int(corr)
        yJump = float(3)/float(categories-1)
        if categories == 2:
            bounds = [[0.5,3.5],[1.5,4.5]]
        elif categories == 3:
            bounds = [[0.5,3],[1.25,3.75],[2,4.5]]
        elif categories == 4:
            bounds = [[0.5,2.75],[1.1,3.35],[1.7,3.95],[2.25,4.5]]
        elif categories ==5:
            bounds = [[0.5,2.5],[1,3],[1.5,3.5],[2,4],[2.5,4.5]]
        for c in range(categories):
            for o in range(obs):
                graph = graph + "\\node at (%s,%s)[circle,fill,inner sep=1pt]{};\n"%(random.uniform(bounds[c][0],bounds[c][1]),1+(yJump*c))

    graph = graph +"""
    \\end{tikzpicture}
    \\end{document}
    """

    return graph

标签: python-3.x

解决方案


推荐阅读