<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Zonal Histogram Bins in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/zonal-histogram-bins/m-p/230525#M17861</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello all,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I am new to programming with arcpy, so this question may be basic.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I have a raster with positive and negative values.&amp;nbsp; I would like to create a histogram for the values inside a polygon.&amp;nbsp; I have used the Zonal Histogram tool in the spatial analyst tools (arcpy.sa), and I obtain an output table with 256 bins.&amp;nbsp; However, I do not know the ranges associated with the bins.&amp;nbsp;&amp;nbsp; The bins are only given by OBJECTID numbering 1 to 256.&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="text-decoration:underline;"&gt;&lt;STRONG&gt;How do I determine the ranges of the bins using python?&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I know that if I change the symbology of the raster to classified, I will get specified bin ranges given by the column "Label".&amp;nbsp; However, this seems to limit me to 32 bins.&amp;nbsp; Also, I would like to be able to do everything in python without having to change the symbology in arcmap.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks in advance for the help!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 31 May 2013 17:19:55 GMT</pubDate>
    <dc:creator>JonathanMartin1</dc:creator>
    <dc:date>2013-05-31T17:19:55Z</dc:date>
    <item>
      <title>Zonal Histogram Bins</title>
      <link>https://community.esri.com/t5/python-questions/zonal-histogram-bins/m-p/230525#M17861</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello all,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I am new to programming with arcpy, so this question may be basic.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I have a raster with positive and negative values.&amp;nbsp; I would like to create a histogram for the values inside a polygon.&amp;nbsp; I have used the Zonal Histogram tool in the spatial analyst tools (arcpy.sa), and I obtain an output table with 256 bins.&amp;nbsp; However, I do not know the ranges associated with the bins.&amp;nbsp;&amp;nbsp; The bins are only given by OBJECTID numbering 1 to 256.&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="text-decoration:underline;"&gt;&lt;STRONG&gt;How do I determine the ranges of the bins using python?&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I know that if I change the symbology of the raster to classified, I will get specified bin ranges given by the column "Label".&amp;nbsp; However, this seems to limit me to 32 bins.&amp;nbsp; Also, I would like to be able to do everything in python without having to change the symbology in arcmap.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks in advance for the help!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 31 May 2013 17:19:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/zonal-histogram-bins/m-p/230525#M17861</guid>
      <dc:creator>JonathanMartin1</dc:creator>
      <dc:date>2013-05-31T17:19:55Z</dc:date>
    </item>
    <item>
      <title>Re: Zonal Histogram Bins</title>
      <link>https://community.esri.com/t5/python-questions/zonal-histogram-bins/m-p/230526#M17862</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;For a given raster, you can determine the range of values by pulling out the properties from an arcpy.Raster object (code &lt;/SPAN&gt;&lt;A href="https://gist.github.com/scw/5688469" rel="nofollow noopener noreferrer" target="_blank"&gt;as a gist&lt;/A&gt;&lt;SPAN&gt;&lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import arcpy
arcpy.CheckOutExtension("spatial")
raster = arcpy.Raster("c:\\workspace\\input_raster")
range = raster.maximum - raster.minimum
bins = 256
bin_size = (range) / bins

# now we know the bin width, can multiply that by the OBJECTID for the lower bound of each bin
lower_bin_value = [bin_size * i for i in range(bins)]
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You could add this to a table with a CalculateField operation, or otherwise embed this information back in the output table. The only trick I didn't cover here is that you'll need to clip your raster data to the polygon to get your 'input_raster' in this approach, since it doesn't know about the geometry restriction automatically.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 11:12:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/zonal-histogram-bins/m-p/230526#M17862</guid>
      <dc:creator>ShaunWalbridge</dc:creator>
      <dc:date>2021-12-11T11:12:29Z</dc:date>
    </item>
    <item>
      <title>Re: Zonal Histogram Bins</title>
      <link>https://community.esri.com/t5/python-questions/zonal-histogram-bins/m-p/230527#M17863</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks for the help sawlbridge.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;It had occurred to me that the bins would be equally divided by the range of values used.&amp;nbsp; In python, I found the range of values from the zonal statistics tool, and I computed the histogram values.&amp;nbsp; I compared the resulting zonal histograms to the histograms I obtain from using the zonal histogram tool in arcmap with the symbology set to classified.&amp;nbsp; However the results were quite different.&amp;nbsp; I copied the results and graphed them in Excel.&amp;nbsp; I have attached an image of the graphs.&amp;nbsp; As you can see, the histograms created in arcmap are roughly centered about 0, but the ones computed in python are centered about -5.&amp;nbsp; Any initial thoughts, or should I post some code?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;[ATTACH=CONFIG]24941[/ATTACH]&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 03 Jun 2013 18:00:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/zonal-histogram-bins/m-p/230527#M17863</guid>
      <dc:creator>JonathanMartin1</dc:creator>
      <dc:date>2013-06-03T18:00:18Z</dc:date>
    </item>
    <item>
      <title>Re: Zonal Histogram Bins</title>
      <link>https://community.esri.com/t5/python-questions/zonal-histogram-bins/m-p/230528#M17864</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;&amp;nbsp;&amp;nbsp; How do I determine the ranges of the bins using python? &lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The easiest way to do this without ArcMap would be to run is to run Zonal Statistics As Table, using your binned output as the zone raster, and your original raster as the value raster. this will give you stats for each bin.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You may want to look into the Slice tool as well.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;As for your calculations not exactly matching to ArcMap symbology, the renderers use approximations of the histograms; they are designed for display and not quantitative analysis. To get true histogram values you would want to use other tools.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 03 Jun 2013 22:11:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/zonal-histogram-bins/m-p/230528#M17864</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2013-06-03T22:11:34Z</dc:date>
    </item>
    <item>
      <title>Re: Zonal Histogram Bins</title>
      <link>https://community.esri.com/t5/python-questions/zonal-histogram-bins/m-p/230529#M17865</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I looked further at the output of the zonal statistics tool for my 2 zones.&amp;nbsp; According to the output statistics table, the means and standard deviations of the two zone are both 1 and 0, respectively.&amp;nbsp; Obviously, this is not correct.&amp;nbsp; Could there be some issues with the noData parts?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here is the code for the function I wrote:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import arcpy, os, random
from arcpy import env, mapping
from arcpy.sa import *

def calcZonalHistograms(polygonFile, zoneField, inRaster, outDir = None, outTbls=None, cleanup=1):
&amp;nbsp;&amp;nbsp;&amp;nbsp; if outTbls:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; outHistTbl, outStatTbl = outTbls
&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; outHistTbl = os.path.join(os.path.dirname(inRaster), os.path.basename(inRaster).split(".")[0]+"_zoneHist_tbl.dbf")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; outStatTbl = os.path.join(os.path.dirname(inRaster), os.path.basename(inRaster).split(".")[0]+"_zoneStat_tbl.dbf")

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Check out the ArcGIS Spatial Analyst extension license
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CheckOutExtension("Spatial")

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Find frequencies for histogram.&amp;nbsp; This will use 256 bins of equal intervals spanning the range of values in the area. (?)
&amp;nbsp;&amp;nbsp;&amp;nbsp; ZonalHistogram(polygonFile, zoneField, inRaster, outHistTbl)

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Execute ZonalStatisticsAsTable
&amp;nbsp;&amp;nbsp;&amp;nbsp; outZSaT = ZonalStatisticsAsTable(polygonFile, zoneField, inRaster, outStatTbl, "NODATA")

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Find bin sizes and values
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Put histogram frequencies in a dictionary; key word equal to field name (Poly_Area)
&amp;nbsp;&amp;nbsp;&amp;nbsp; fieldList = arcpy.ListFields(outHistTbl)
&amp;nbsp;&amp;nbsp;&amp;nbsp; info_dict = {}
&amp;nbsp;&amp;nbsp;&amp;nbsp; binSizes = {}
&amp;nbsp;&amp;nbsp;&amp;nbsp; for field in fieldList:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if field.name == "OID": pass
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; info_dict[field.name] = []
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows = arcpy.SearchCursor(outHistTbl)
&amp;nbsp;&amp;nbsp;&amp;nbsp; i = 0
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for field in fieldList:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if field.name != "OID": info_dict[field.name].append(row.getValue(field.name))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; i+=1
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Find bin Size from outStatTbl
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows = arcpy.SearchCursor(outStatTbl)
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; binSizes[row.getValue("POLY_AREA")] = [row.getValue("MIN"), (float(row.getValue("MAX"))-float(row.getValue("MIN")))/i]

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Export to txt for use in other programs (excel, etc...)
&amp;nbsp;&amp;nbsp;&amp;nbsp; if not outDir: outDir = os.path.dirname(inRaster)
&amp;nbsp;&amp;nbsp;&amp;nbsp; for key in info_dict.keys():
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; exportHistogramToTxt(binSizes[key][1], binSizes[key][0], info_dict[key], key, os.path.join(outDir, key+"_hist.txt"))

&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Again, thanks for the help!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 11:12:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/zonal-histogram-bins/m-p/230529#M17865</guid>
      <dc:creator>JonathanMartin1</dc:creator>
      <dc:date>2021-12-11T11:12:31Z</dc:date>
    </item>
    <item>
      <title>Re: Zonal Histogram Bins</title>
      <link>https://community.esri.com/t5/python-questions/zonal-histogram-bins/m-p/230530#M17866</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Interesting. I'm not entirely familiar with this tool, but am happy to help dig in further. Could you send me some sample data at &lt;/SPAN&gt;&lt;A href="mailto:swalbridge@esri.com"&gt;swalbridge@esri.com&lt;/A&gt;&lt;SPAN&gt;? I can accept up to 20MB attachments. Failing that, I can set up a dropsite for your data, if possible it'd be great to have the actual data so I can compare directly with the analysis you've posted above.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;It may also be useful to try using the &lt;/SPAN&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#//018v00000023000000"&gt;RasterToNumPyArray &lt;/A&gt;&lt;SPAN&gt; conversion on the data, provided it can fit into memory -- then you can write a simple aggregation function based on the raw values, and work with the data as needed, computing your own histogram, or using &lt;/SPAN&gt;&lt;A href="http://matplotlib.org/examples/api/histogram_demo.html"&gt;matplotlib to compute the histogram&lt;/A&gt;&lt;SPAN&gt; directly from your values. As you mentioned, how NoData is handled is important, as is what happens with the edge pixels that intersect the geometry and the raster -- implementations vary on how they handle these edge pixels.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;cheers,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Shaun&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 04 Jun 2013 03:51:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/zonal-histogram-bins/m-p/230530#M17866</guid>
      <dc:creator>ShaunWalbridge</dc:creator>
      <dc:date>2013-06-04T03:51:23Z</dc:date>
    </item>
  </channel>
</rss>

