使用循环中的轴对象的matplotlib散点图

使用循环中的轴对象的matplotlib散点图,matplotlib,axes,arcpy,Matplotlib,Axes,Arcpy,使用Matplotlib在循环中绘制多个系列时遇到问题(Matplotlib 1.0.0、Python 2.6.5、ArcGIS 10.0)。论坛研究向我指出了Axis对象的应用,以便在同一个绘图上绘制多个系列。我知道这对于在循环之外生成的数据(示例脚本)是如何工作的,但是当我插入相同的语法并将第二个系列添加到从数据库提取数据的循环中时,我得到了以下错误: “:不支持的操作数类型-:'NoneType'和'NoneType'未能执行(图表8)。” 下面是我的代码-任何建议或意见都非常感谢 imp

使用Matplotlib在循环中绘制多个系列时遇到问题(Matplotlib 1.0.0、Python 2.6.5、ArcGIS 10.0)。论坛研究向我指出了Axis对象的应用,以便在同一个绘图上绘制多个系列。我知道这对于在循环之外生成的数据(示例脚本)是如何工作的,但是当我插入相同的语法并将第二个系列添加到从数据库提取数据的循环中时,我得到了以下错误:

“:不支持的操作数类型-:'NoneType'和'NoneType'未能执行(图表8)。”

下面是我的代码-任何建议或意见都非常感谢

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
x1 = 1 # category 1 locator along x-axis
x2 = 2 # category 2 locator along x-axis
fig = plt.figure()
for row in cur:
    y1 = row.getValue(P1_fld)
    y2 = row.getValue(P2_fld)
    i += 1
    ax1 = fig.add_subplot(nsubp, 1, i)
    ax1.scatter(x1, y1, s=10, c='b', marker="s")
    ax1.scatter(x2, y2, s=10, c='r', marker="o")
del row, cur

#Save plot to pdf, open
figPDf = r"path.pdf"
plt.savefig(figPDf)
os.startfile("path.pdf")

如果您想做的是重复使用同一个绘图来绘制多个对象,那么您应该做的是在循环之外创建figure对象,然后每次都绘制到同一个对象,如下所示:

fig = plt.figure()

for row in cur:
    y1 = row.getValue(P1_fld)
    y2 = row.getValue(P2_fld)
    i += 1

    ax1 = fig.add_subplot(nsubp, 1, i)
    ax1.scatter(x1, y1, s=10, c='b', marker="s")
    ax1.scatter(x2, y2, s=10, c='r', marker="o")
del row, cur

谢谢你的反馈。我根据您的建议修改了脚本,但是仍然得到了与以前相同的错误。@gamarra您仍然在循环的每次迭代中创建一个新的figure对象。Paul,我编辑了上面的代码以反映脚本中的更改。据我所知,figure对象现在只创建了一次,但是subplot对象确实会在每个记录中重复出现,这就是目标。但是,我只能在循环中添加一个子地块对象(对于系列2,最终我需要再添加12个系列)。只要我添加第二个子图对象,如上面代码所示,我就会得到NoneType错误。关于如何解决这个问题有什么想法吗?我没有运气解决这个问题来生成需要显示的摘要图。我不想放弃matplotlib的功能,但我正在研究ReportLab作为替代方案。