2

I have been trying for a long time now to make this simple basemap work. However, there is something with the format of the data basemap expects, which I cannot figure out. Can anybody help me understand what am I doing wrong?

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
from osgeo import gdal
from numpy import linspace
from numpy import meshgrid
import numpy as np

lats  = [-17.96, -2.88, -3.12, -14.01, -9.28, -15.59, -15.69]
longs = [-38.69, -40.13, -40.04, -52.21, -37.89, -47.62, -41.43]
prod  = [ 24.6, 7.5, 8.5, 68.8, 6.8, 6.6, 31.0]

lats = np.asarray(lats)
longs = np.asarray(longs)
prod = np.asarray(prod)

map = Basemap(projection='tmerc',
          lat_0=0, lon_0=3,
          llcrnrlon=1.819757266426611,
          llcrnrlat=41.583851612359275,
          urcrnrlon=1.841589961763497,
          urcrnrlat=41.598674173123)

x = linspace(0, map.urcrnrx, prod.shape[1])
y = linspace(0, map.urcrnry, prod.shape[0])

xx, yy = meshgrid(x, y)

map.pcolormesh(xx, yy, data)

plt.show()
5
  • What is the result when you run that? Errors, unexpected output, no output at all?
    – Midavalo
    Commented Oct 8, 2016 at 21:38
  • @Midavalo: ... line 23 ... x = linspace(0, map.urcrnrx, prod.shape[1]) IndexError: tuple index out of range ...
    – relima
    Commented Oct 8, 2016 at 21:51
  • Please edit your question to include all relevant info including the errors. Don't just add as comment
    – Midavalo
    Commented Oct 8, 2016 at 21:56
  • I'm not familiar with all the libraries, but I'm curious what you get if you add print(prod.shape[1]) in line 22. You may have issues if it is not a non-negative integer Commented Oct 9, 2016 at 23:40
  • Better yet, print(0, map.urcrnrx, prod.shape[1]) and see if you get two scalars and an integer. I suspect you may get two scalars and a tuple and need something like prod.shape[1][0] for the third argument. If you get an error, perhaps try print(prod.shape) and see if you have two elements. Commented Oct 9, 2016 at 23:49

2 Answers 2

2
+50

It looks like prod is a one dimensional array, so it's shape attribute has a length of one. Thus prod.shape[0] works, but prod.shape[1] raises an index error. While I don't have a clue of what prod represents, it is probably suppose to be a 2D array of data that is mapped in the matrix.

Perhaps you can represent the size of x and y dimensions using the lengths of lats and longs:

x = linspace(0, map.urcrnrx, len(longs))
y = linspace(0, map.urcrnry, len(lats))
1

I think you want

x = linspace(0, map.urcrnrx, prod.shape[0])

in line 23. The prod array has 7 items that match both the 7 lats and 7 longs. It isn't a two-dimensional array, so

prod.shape

returns the tuple (7L,) as a long integer inside a single-item tuple. Your code gives an index out of range because there is only one value in the tuple at index 0 and no value for index of 1. Using prod.shape[0] will return the integer 7, and I presume it is appropriate in your code to use this value for both line 23 and 24.

see linspace and shape

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.