Import libraries


In [19]:
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
import grass.script as grass
import grass.script.setup as gsetup
rundir   = 'grassdata'
location = 'location'

Set grass region to image bounds


In [9]:
gisbase  = os.environ['GISBASE'] = '/usr/lib/grass74'
gisdbase = os.path.join(os.environ['HOME'], rundir)
sys.path.append(os.path.join(os.environ['GISBASE'], "etc", "python"))
gsetup.init(gisbase,gisdbase, location, 'PERMANENT')
grass.read_command('g.region',raster='image.tif@PERMANENT',flags='p')


Out[9]:
'projection: 3 (Latitude-Longitude)\nzone:       0\ndatum:      wgs84\nellipsoid:  wgs84\nnorth:      61:07:05.09601N\nsouth:      59:02:37.342976N\nwest:       6:25:39.151625W\neast:       2:04:14.98571W\nnsres:      0:00:01.337109\newres:      0:00:01.337099\nrows:       5585\ncols:       11730\ncells:      65512050\n'

Prepare histogram

Default output file name is 'map.png'.


In [17]:
a=grass.read_command('d.histogram', map='image.tif@PERMANENT',style='bar')

Display results using matplotlib


In [15]:
os.environ['GRASS_RENDER_IMMEDIATE']='png'
os.environ['GRASS_RENDER_TRUECOLOR']='TRUE'

In [26]:
import os
img=mpimg.imread('map.png')
fig, ax = plt.subplots(figsize=(180, 20))
ax.imshow(img)


Out[26]:
<matplotlib.image.AxesImage at 0x7fdc18d2a190>

In [13]:
gisdbase


Out[13]:
'/home/jovyan/grassdata'