【问题标题】:matplotlib scatter plot using axes object in loopmatplotlib 散点图在循环中使用轴对象
【发布时间】:2013-07-01 21:41:11
【问题描述】:

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

": 不支持的操作数类型 -: 'NoneType' 和 'NoneType' 执行失败 (ChartAge8)。"

以下是我的代码 - 非常感谢任何建议或 cmets!

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")

【问题讨论】:

    标签: matplotlib axes arcpy


    【解决方案1】:

    如果你想做的是重复使用同一个图绘制几个东西,你应该做什么它在循环外创建图形对象,然后每次都绘制到同一个对象,如下所示:

    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 你仍然在循环的每次迭代中创建一个新的图形对象
    • Paul,我已经编辑了上面的代码以反映我的脚本中的更改。据我了解,图形对象现在只创建一次,但是子图对象确实为每条记录再次出现,这是目标。但是,我只能在循环内添加一个子图对象(对于系列 2,-最终我需要再添加 12 个系列)。一旦我添加了第二个子图对象,如上面的代码所示,我得到了 NoneType 错误。关于如何解决这个问题的任何想法?
    • 我没有运气解决这个问题来生成摘要图,因为我需要显示它们。讨厌放弃 matplotlib 的功能,但我正在研究 ReportLab 作为替代方案。
    猜你喜欢
    • 1970-01-01
    • 2016-06-19
    • 2023-03-14
    • 2019-11-23
    • 2014-09-18
    • 2017-08-24
    • 2013-10-17
    • 1970-01-01
    • 2017-09-29
    相关资源
    最近更新 更多