[GRASS-user] GRASS GIS 7.0/bundled python/matplolib

Jon Eiriksson jeir at hi.is
Thu Mar 14 13:21:34 PDT 2013


Being unable to import matplotlib in my python scripts in the GRASS GIS 
environment (and not able to find binaries for matplotlib 1.2.0 for 32 
bit python 2.6, which is needed for the wxgui) - I took the following 
workaround steps, perhaps they may be useful to someone or save some 
time. Or, even better, perhaps someone knows how to access matplotlib 
from the bundled python.

Downloaded python 2.7 and matplotlib 1.2.0 (as well as numpy and 
ipython), following recommendations at

http://matplotlib.org/faq/installing_faq.html#os-x-notes

I now have two versions of python on my system (Mac OS X Version 
10.7.5). The new installation (2.7), and version 2.6 bundled with 
Michael Barton's GRASS GIS 7.0 binaries (William Kingesburye's 
frameworks).

In my .bash_profile I have these GRASS and python related lines:

#
export 
PATH=$PATH:/usr/local/mysql/bin:/usr/bin/python:/Applications/GRASS-7.0.app/Contents/MacOS/scripts
export PATH=/Library/Frameworks/GDAL.framework/Programs:$PATH
export 
PATH=/Library/Frameworks/Python.framework/Versions/Current/bin:${PATH}
#

The following python script file was saved to  my home directory:

/Users/joneiriksson/je_01.py

#!/usr/bin/env python-32

import sys
from grass.script import core as grass
from grass.script import db as grass
from array import array

import matplotlib.pyplot as plt

def get_convergence():

     returned_conv_angle = grass.read_command('g.region', flags = 'n')

     conv_angle = grass.parse_key_val(returned_conv_angle)

     return str(conv_angle)

def plot_points_graph():

     xy = []

     zero = 0.0
     xy = [[zero for i in range(2)] for j in range(6)]

     ix,target = 0,5

     while ix <= target:
         xy[ix][0] = ix
         xy[ix][1] = ix

         ix = ix + 1
         print ix
     print xy

     plt.plot(xy, 'ro')
     plt.axis([-1, 6, -1, 6])

     plt.grid(True)

     plt.show()

def main():

     c_angle_string = get_convergence()
     print 'convergence angle string = ',c_angle_string
     plot_points_graph()

     return 0

# this "if" condition instructs execution of code contained in this 
script,
# *only* if the script is being executed directly
if __name__ == "__main__": # this allows the script to be used as a 
module in other scripts or as a standalone script
     options, flags = grass.parser() #
     sys.exit(main()) #
#
Note the alias in the first line of the script:

#!/usr/bin/env python-32

- instead of the normally used

#!/usr/bin/env python

- this notation activates python 2.7 instead of the bundled python 2.6

This enables me to call matplotlib functions from my script, which is 
activated in the GRASS GIS Terminal window by the command:

GRASS 7.0.svn (je_location):~ >je_01.py

Other GRASS related python scripts reside in:

/Applications/GRASS/GRASS-7.0.app/Contents/MacOS/scripts/

and work as before, using python 2.6 (#!/usr/bin/env python), compatible 
with the wxgui

Jon




More information about the grass-user mailing list