[GRASS-SVN] r74299 - in grass-addons/grass7/display: . d.vect.colbp

svn_grass at osgeo.org svn_grass at osgeo.org
Sat Mar 23 06:37:41 PDT 2019


Author: pvanbosgeo
Date: 2019-03-23 06:37:41 -0700 (Sat, 23 Mar 2019)
New Revision: 74299

Added:
   grass-addons/grass7/display/d.vect.colbp/
   grass-addons/grass7/display/d.vect.colbp/Makefile
   grass-addons/grass7/display/d.vect.colbp/d.vect.colbp.html
   grass-addons/grass7/display/d.vect.colbp/d.vect.colbp.py
   grass-addons/grass7/display/d.vect.colbp/d_vect_colbp_01.png
   grass-addons/grass7/display/d.vect.colbp/d_vect_colbp_02.png
Log:
New addon d.vect.colbp: Draws the boxplot(s) of values in a selected column of  vector attribute table

Added: grass-addons/grass7/display/d.vect.colbp/Makefile
===================================================================
--- grass-addons/grass7/display/d.vect.colbp/Makefile	                        (rev 0)
+++ grass-addons/grass7/display/d.vect.colbp/Makefile	2019-03-23 13:37:41 UTC (rev 74299)
@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../..
+
+PGM = d.vect.colbp
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script


Property changes on: grass-addons/grass7/display/d.vect.colbp/Makefile
___________________________________________________________________
Added: svn:mime-type
## -0,0 +1 ##
+text/x-makefile
\ No newline at end of property
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: grass-addons/grass7/display/d.vect.colbp/d.vect.colbp.html
===================================================================
--- grass-addons/grass7/display/d.vect.colbp/d.vect.colbp.html	                        (rev 0)
+++ grass-addons/grass7/display/d.vect.colbp/d.vect.colbp.html	2019-03-23 13:37:41 UTC (rev 74299)
@@ -0,0 +1,70 @@
+<h2>DESCRIPTION</h2>
+
+<p>
+<em>d.vect.colbp</em> draws a boxplot of the values in a vector 
+<em>map</em> attribute <em>column</em>. The user can use the 
+<em>where</em> option to only select a subset of the attribute table. 
+There is also the option to group the values of the column according to 
+the categories in a second column (<em>group_by</em> and create one plot 
+with for each group a separate boxplot.
+
+<p>
+By default, the resulting plot is displayed on screen (default). 
+However, the user can also save the plot to file using the 
+<em>plot_output</em> option. The format is determined by the extension 
+given by the user. So, if plot_output = outputfile.png, the plot will 
+be saved as a png file.
+
+<p>
+There are a few additonal layout options, including the option to rotate 
+the plot and the x-axis labels. Furthermore, the user can optinally plot 
+the boxplot(s) with notches and without outliers.
+
+<h2>NOTE</h2>
+<p>
+This script, based on d.vect.colhist, is a quick and dirty solution 
+sing basic matplotlib, and will need some further fine-tuning and 
+testing.
+
+<h2>EXAMPLE</h2>
+
+<h3>Example 1</h3>
+Use the vector layer <em>schools_wake</em> from the 
+<a href="https://grass.osgeo.org/download/sample-data/">NC sample 
+dataset</a> to create boxplots of the core capacity of the schools in 
+Wake County, North Carolina. Use the <em>Where</em> clause to exclude 
+all records with no data.
+
+<div class="code"><pre>
+d.vect.colbp -n map=schools_wake column=CORECAPACI where="CORECAPACI >0"
+</pre>
+</div>
+
+<p>
+<img src="d_vect_colbp_01.png"><br>
+<em>Figure 1: Boxplot of core capacity of schools in Wake County.</em>
+
+
+<h3>Example 2</h3>
+Use the vector layer <em>schools_wake</em> from the 
+<a href="https://grass.osgeo.org/download/sample-data/">NC sample 
+dataset</a> to create boxplots of the core capacity of the schools in 
+Wake County, North Carolina, grouped by city. Use the <em>Where</em> 
+clause to exclude all records with no data. 
+
+<div class="code">
+<pre>
+d.vect.colbp -h --overwrite map=schools_wake column=CORECAPACI where="CORECAPACI >0" group_by=ADDRCITY order=ascending
+	</pre>
+</div>
+
+<p>
+<img src="d_vect_colbp_02.png"><br>
+<em>Figure 2" Boxplot of core capacity of schools in Wake County, grouped by city.</em>
+
+
+<h2>AUTHOR</h2>
+Paulo van Breugel<br>
+Based on the d.vect.colhist addon by Moritz Lennert  
+
+<p><em>Last changed: $Date$</em>


Property changes on: grass-addons/grass7/display/d.vect.colbp/d.vect.colbp.html
___________________________________________________________________
Added: svn:mime-type
## -0,0 +1 ##
+text/html
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+Author Date Id
\ No newline at end of property
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: grass-addons/grass7/display/d.vect.colbp/d.vect.colbp.py
===================================================================
--- grass-addons/grass7/display/d.vect.colbp/d.vect.colbp.py	                        (rev 0)
+++ grass-addons/grass7/display/d.vect.colbp/d.vect.colbp.py	2019-03-23 13:37:41 UTC (rev 74299)
@@ -0,0 +1,173 @@
+#!/usr/bin/env python
+############################################################################
+#
+# MODULE:       d.vect.colboxplot
+# AUTHOR:       Paulo van Breugel
+# PURPOSE:      Draws the boxplot(s) of values in a vector attribute column
+#
+# COPYRIGHT:    (c) 2019 Paulo van Breugel, and the GRASS Development Team
+#               This program is free software under the GNU General Public
+#               License (>=v2). Read the file COPYING that comes with GRASS
+#               for details.
+#
+#############################################################################
+
+#%module
+#% description: Draws the boxplot of values in a vector attribute column
+#% keyword: display
+#% keyword: vector
+#% keyword: plot
+#% keyword: histogram
+#% keyword: boxplot
+#%end
+
+#%option G_OPT_V_MAP
+#% guisection: General
+#%end
+
+#%option G_OPT_V_FIELD
+#%end
+
+#%option G_OPT_DB_COLUMN
+#% key: column
+#% description: Attribute column value to be plotted
+#% required: yes
+#% guisection: General
+#%end
+
+#%option G_OPT_DB_WHERE
+#%guisection: General
+#%end
+
+#%option G_OPT_F_OUTPUT
+#% key: plot_output
+#% required: no
+#% guisection: General
+#%end
+
+#%option G_OPT_DB_COLUMN
+#% key: group_by
+#% description: Attribute column with categories to group the data by
+#% required: no
+#% guisection: Plot options
+#%end
+
+#%option
+#% key: order
+#% type: string
+#% label: Sort boxplots
+#% description: Sort boxplots based on their median values
+#% required: no
+#% options: descending,ascending
+#% guisection: Plot options
+#%end
+
+#%flag
+#% key: h
+#% label: horizontal boxplot(s)
+#% description: Draw the boxplot horizontal
+#% guisection: Plot options
+#%end
+
+#%flag
+#% key: o
+#% label: hide outliers
+#% description: Draw boxplot(s) without outliers
+#% guisection: Plot options
+#%end
+
+#%flag
+#% key: n
+#% label: notch
+#% description: Draw boxplot(s) with notch
+#% guisection: Plot options
+#%end
+
+#%flag
+#% key: r
+#% label: Rotate labels
+#% description: rotate x-axis labels
+#% guisection: Plot options
+#%end
+
+import sys
+import matplotlib #required by windows
+matplotlib.use('wxAGG') #required by windows
+import matplotlib.pyplot as plt
+import grass.script as gscript
+import operator
+import numpy as np
+
+def main():
+    # input
+    vector = options['map']
+    column = options['column']
+    group_by = options['group_by'] if options['group_by'] else None
+    output = options['plot_output'] if options['plot_output'] else None
+    where = options['where'] + " AND " + column + " IS NOT NULL" \
+            if options['where'] else column + " IS NOT NULL"
+    sort = options['order'] if options['order'] else None
+    if sort == 'descending':
+        reverse = True
+    elif sort == 'ascending':
+        reverse = False
+    else:
+        reverse = None
+    cols = filter(None, [group_by, column])
+    flag_h = not flags['h']
+    flag_o = not flags['o']
+    flag_n = flags['n']
+    flag_r = flags['r']
+    
+    # Get data with where clause
+    if where:
+        df=[x for x in gscript.read_command('v.db.select',
+                                            map_=vector,
+                                            column=cols,
+                                            where=where,
+                                            flags='c').splitlines()]
+    # Get all column data
+    else:
+        df=[x for x in gscript.read_command('v.db.select',
+                                            map_=vector,
+                                            column=cols,
+                                            flags='c').splitlines()]
+    # for grouped boxplot
+    if group_by:
+        # Split columns and create list with data and with labels
+        df=[x.split('|') for x in df]
+        vals = [float(i[1]) for i in df]
+        groups = [i[0] for i in df] 
+        uid = list(set(groups))
+        data = []
+        sf = []
+        for i,m in enumerate(uid):
+            a = [ j for j, grp in enumerate(groups) if grp == m]
+            data.append([vals[i] for i in a])
+            sf.append([m, np.median([vals[i] for i in a])])
+        
+        # Order boxes
+        if sort:
+            sf.sort(key = operator.itemgetter(1), reverse=reverse)
+        sf = [i[0] for i in sf] 
+        ii = { e: i for i, e in enumerate(sf) }
+        sfo = [(ii[e]) for i, e in enumerate(uid) if e in ii]
+      
+        # Draw boxplot
+        plt.boxplot(data, notch=flag_n, sym='gD', labels=uid, vert=flag_h,
+                        showfliers=flag_o, positions=sfo)
+    else:
+        data=[float(x) for x in df]
+        plt.boxplot(data, notch=flag_n, sym='gD', vert=flag_h,
+                        showfliers=flag_o)
+    if flag_r:
+        plt.xticks(rotation=90)
+    plt.tight_layout()
+    if output:
+        plt.savefig(output)
+    else:
+        plt.show()
+        
+if __name__ == "__main__":
+    options, flags = gscript.parser()
+    main()


Property changes on: grass-addons/grass7/display/d.vect.colbp/d.vect.colbp.py
___________________________________________________________________
Added: svn:executable
## -0,0 +1 ##
+*
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/x-python
\ No newline at end of property
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: grass-addons/grass7/display/d.vect.colbp/d_vect_colbp_01.png
===================================================================
(Binary files differ)

Index: grass-addons/grass7/display/d.vect.colbp/d_vect_colbp_01.png
===================================================================
--- grass-addons/grass7/display/d.vect.colbp/d_vect_colbp_01.png	2019-03-22 08:52:53 UTC (rev 74298)
+++ grass-addons/grass7/display/d.vect.colbp/d_vect_colbp_01.png	2019-03-23 13:37:41 UTC (rev 74299)

Property changes on: grass-addons/grass7/display/d.vect.colbp/d_vect_colbp_01.png
___________________________________________________________________
Added: svn:mime-type
## -0,0 +1 ##
+image/png
\ No newline at end of property
Added: grass-addons/grass7/display/d.vect.colbp/d_vect_colbp_02.png
===================================================================
(Binary files differ)

Index: grass-addons/grass7/display/d.vect.colbp/d_vect_colbp_02.png
===================================================================
--- grass-addons/grass7/display/d.vect.colbp/d_vect_colbp_02.png	2019-03-22 08:52:53 UTC (rev 74298)
+++ grass-addons/grass7/display/d.vect.colbp/d_vect_colbp_02.png	2019-03-23 13:37:41 UTC (rev 74299)

Property changes on: grass-addons/grass7/display/d.vect.colbp/d_vect_colbp_02.png
___________________________________________________________________
Added: svn:mime-type
## -0,0 +1 ##
+image/png
\ No newline at end of property


More information about the grass-commit mailing list