我需要为数据库中的每条记录创建一个参数的摘要图。使用下面的代码,我设法为每条记录创建一个子图(测试数据库中有5个,ArcGIS 10.0文件地理数据库,Python 2.6.5,Matplotlib 1.0.0),但每个子图都是相同的。我试图通过论坛搜索摘要图/报告,子图语法和循环技术的示例来尝试识别正确的语法。我希望我的问题是一个不正确的循环语法,因为我正在绘制每个绘图的所有记录,而不是每个绘图所需的一个记录。在我解决了这个基本的绘图问题之后,我计划扩展我的代码范围,每个绘图包含10-15个参数,总共3-4个绘图,以及一些总体摘要信息,所有这些都在每个记录的单页pdf上。我正在处理几千条记录。
这是我在Stack Overflow上的第一篇文章。在过去一年中,该论坛在很多场合对我来说都是非常有用的资源。我是python的新手,也是使用matplotlib的新手,但我看到了该语言和这个库的巨大潜力。任何帮助或建议都非常感谢!
import arcpy
import os
import matplotlib
import matplotlib.pyplot as plt
#Variables
FC = arcpy.GetParameterAsText(0) #feature class
P1_fld = arcpy.GetParameterAsText(1) #score field to chart
P2_fld = arcpy.GetParameterAsText(2) #score field to chart
plt.subplots_adjust(hspace=0.4)
nsubp = int(arcpy.GetCount_management(FC).getOutput(0)) #pulls n subplots from FC
last_val = object()
#Sub-plot loop
cur = arcpy.SearchCursor(FC, "", "", P1_fld)
for row in cur:
x=1
y=row.getValue(P1_fld)
if row.OBJECTID != last_val:
for i,v in enumerate(xrange(nsubp)):
v = v+1
i = i+1
ax = plt.subplot(nsubp,1,v) # Create a subplot.
ax.scatter(x,y,s=5,color='blue'); # Generate the Scatter Plot.
oid = str(row.getValue('OBJECTID'))
figPDf = r"filepath.pdf" # Save the Scatter Plot to PDF.
plt.savefig(figPDf)
del row, cur
os.startfile("filepath.pdf")
答案 0 :(得分:0)
这是因为你有两个嵌套的for
循环:第一个循环遍历每个row
,而第二个循环使散点图出现在每个子图上。这反过来意味着每个绘制的参数将出现在每个子图上。为避免这种情况,您应该避免使用双for
循环。
我不确定我到底想要达到什么目标,但这至少应该让你顺利进行。
import arcpy
import os
import matplotlib
import matplotlib.pyplot as plt
#Variables
FC = arcpy.GetParameterAsText(0) #feature class
P1_fld = arcpy.GetParameterAsText(1) #score field to chart
P2_fld = arcpy.GetParameterAsText(2) #score field to chart
plt.subplots_adjust(hspace=0.4)
nsubp = int(arcpy.GetCount_management(FC).getOutput(0)) #pulls n subplots from FC
last_val = object()
#Sub-plot loop
cur = arcpy.SearchCursor(FC, "", "", P1_fld)
i = 0
x = 1
for row in cur:
y = row.getValue(P1_fld)
if row.OBJECTID != last_val:
i += 1
ax = plt.subplot(nsubp, 1, i) # Create a subplot.
ax.scatter(x, y, s=5, color='blue'); # Generate the Scatter Plot.
oid = str(row.getValue('OBJECTID'))
figPDf = r"filepath.pdf" # Save the Scatter Plot to PDF.
plt.savefig(figPDf)
del row, cur
os.startfile("filepath.pdf")