首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >matplotlib每条记录的摘要图

matplotlib每条记录的摘要图
EN

Stack Overflow用户
提问于 2013-07-01 22:42:48
回答 1查看 579关注 0票数 0

我需要为我的数据库中的每个记录创建一个单个参数的摘要图。使用下面的代码,我已经设法为每个记录创建了一个子图(测试数据库、Python10.0文件地理数据库、ArcGIS 2.6.5、Matplotlib1.0.0中的5个),但每个子图都是相同的。我在论坛上搜索了总结图/报告、子图语法和循环技术的示例,试图找出正确的语法。我认为我的问题是一个不正确的循环语法,因为我绘制的是每个绘图的所有记录,而不是每个绘图所需的一条记录。在我解决了这个基本的绘图问题之后,我计划将我的代码范围扩展到每个绘图包括10-15个参数,总共3-4个绘图,以及一些一般的摘要信息,所有这些都在每个记录的单页pdf中。我总共处理了几千条记录。

这是我在Stack Overflow上的第一篇文章。在过去的一年里,论坛对我来说是一个非常有帮助的资源。我是python的新手,对matplotlib的使用也是全新的,但我看到了该语言和这个库的巨大潜力。如有任何帮助或建议,我们不胜感激!

代码语言:javascript
复制
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")
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-02 00:16:34

这是因为您有两个嵌套的for循环:第一个循环遍历每个row,而第二个循环使散点图出现在每个子图上。这反过来意味着每个绘制的参数将出现在每个子图上。为了避免这种情况,您应该避免双for循环。

我不确定我是否完全理解您想要实现的目标,但这至少应该能让您上路。

代码语言:javascript
复制
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")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17407204

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档