[Matplotlib-users] Problem using imshow with Matplotlib/Basemap

Mauro Cavalcanti maurobio at gmail.com
Sun Oct 8 14:33:57 EDT 2017


Dear ALL,

I have a simple dataset of longitudes/latitudes (see the attached csv
file).

>From such data, I want to generate a grid like this:

0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 0 1 1 2 0 0 0 0
0 0 0 0 0 1 1 1 1 0 0 0 0
0 0 0 1 0 1 0 0 0 0 0 0 0
0 0 0 2 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 1 3 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0

which gives the number of data records in each cell of the grid, using one
of the variables in the dataset ("spp") as a categorical (grouping) factor.

>From this grid, I then want to create a heat map, superimposed on a
Matplotlib/Basemap.

I wrote some code which does what I want (see the attachments).

It (mostly) works, but te problem is that the grid image is not being
displayed correctly: as shown in the attached figure, it appears too small,
and in the lower left corner of the map, instead of where it should be (the
West coast of Africa, along the Gulf of Guinea).

Thanks in advance for any assistance you can provide.

Best regards,

-- 
Dr. Mauro J. Cavalcanti
E-mail: maurobio at gmail.com
Web: http://sites.google.com/site/maurobio
"Life is complex. It consists of real and imaginary parts."
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20171008/add52fd2/attachment-0001.html>
-------------- next part --------------
import csv
import numpy as np
from mpl_toolkits.basemap import Basemap
from matplotlib import cm as cmap
import matplotlib.pyplot as plt

import warnings
warnings.filterwarnings("ignore")

#read input data
with open('testdata.csv', 'r') as csvfile:
    reader = csv.reader(csvfile, delimiter=',')
    headers = reader.next()
    input_data = list(reader)

#grid dimensions with one-degree resolution
lat_inf, lon_inf = 0, 0
lat_sup, lon_sup = 90, 360
resolution = 1

latitude, longitude = [], []
latitude = range(lat_inf, lat_sup, resolution)
longitude = range(lon_inf, lon_sup, resolution)

#create output grid
output_grid = []
for i in latitude:
   output_grid.append([])
   for j in longitude:
       output_grid[i].append(0)

#traverse the input_data evaluating the lat, lon coordinates
#summing +1 in the output_grid[latitude][longitude].
for row in input_data:
   lat = int(row[2]) 
   lon = int(row[3]) 
   #sp = row[1]

   #check its indexes
   i_lat = latitude.index(lat)
   i_lon = longitude.index(lon)

   #increase counter
   output_grid[i_lat][i_lon] += 1

output_grid = np.array(output_grid, np.int16)

#create map
m = Basemap()
m.drawcoastlines(linewidth=0.25)

#display image
im = m.imshow(output_grid.transpose(), cmap='summer', origin='lower', aspect='auto', interpolation='none')
m.colorbar(im)
plt.show()
-------------- next part --------------
A non-text attachment was scrubbed...
Name: testdata.csv
Type: text/csv
Size: 277 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20171008/add52fd2/attachment-0001.csv>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Figure_1.png
Type: image/png
Size: 63619 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20171008/add52fd2/attachment-0001.png>


More information about the Matplotlib-users mailing list