python-3.x - 收到一段代码的错误,但没有收到类似代码段的错误
问题描述
我一直在
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
解决方案
推荐阅读
- nunit - NUnit 中的断点和停止调试 - 如何终止测试?
- asp.net - 如何将不同实体中的项目动态添加到 ASP.NET Core MVC 中的列表中
- wordpress - 如何编辑已经存在的 Wordpress 主题?
- node.js - mongodb匹配和组2查询合二为一
- json - 我如何在 artifactory 上托管 json 文件,artifcatory 可以用作网络服务器吗
- firebase - 颤振:FlutterFcmService - 致命:找不到回调
- batch-file - 尝试使用批处理将输入写入正在运行的控制台应用程序时出错
- react-native - 使用expo在react-native项目中基于多个条件禁用按钮
- javascript - json数据到treeview javascript
- r - R:lapply with data.table 慢,有更快的选择吗?