Basemap Heat error / empty map
Asked Answered
M

1

6

I am trying to plot a scattered heat map on a defined geo location. I can very well plot a normal scattered map with no background but I want to combine it with a given lat and lon. I get the following empty mapenter image description here.

Input

Input: col[2] and col[3] are the x and y co ordinates & Geo Location Lat:19.997453, Lon:73.789802

000000000023 61.0 19.006113 73.009168 
000000000054 65.0 19.009249 73.000342 
000000000003 19.0 19.001051 73.000080 
000000000012 20.0 19.009390 73.008638 
000000000061 82.0 19.008550 73.003605 
000000000048 86.0 19.006597 73.001057 
00000000005d 60.0 19.003857 73.009618 
000000000006 60.0 19.003370 73.009112 
000000000037 91.0 19.002558 73.000546 
000000000047 32.0 19.006061 73.008239 

Program

from matplotlib import pyplot as plt 
from matplotlib import cm as CM
from matplotlib import mlab as ml
from mpl_toolkits.basemap import Basemap
import numpy as np 

m = Basemap(width=12000000, height=9000000, projection='lcc', 
            resolution='c', lat_0=19.,lon_0=73.)
m.drawcoastlines(linewidth=0.25)

data = np.loadtxt('random_lat_lon_0', unpack=True, 
                  dtype='str, float, float, float')

x  = data[2]
y  = data[3]
z  = data[1]

gridsize = 100 
m.hexbin(x, y, C=z, gridsize=gridsize)

cb = m.colorbar()
#m.set_label('Density')
plt.show()  

No Error But I see only empty map but no scatter plot of data on that.

How to fix ? Thanks !!

Molal answered 14/5, 2015 at 18:31 Comment(8)
Matplotlib is complaining at m.imshow(data, interpolation = 'none'). The data array has shape (N, 6). This cannot be interpreted as an image. You need to feed imshow a grid of pixel values, like something of the shape (N, M, 3) where the last dimension represents rgb values.Amply
It also looks like you've been trying to plot this for weeks... 1, 2, 3Amply
Do you have any image data for your geolocation?Amply
@Amply Yes I have been trying to plot too many maps for weeks every map serves a different purpose but questions seem similar.Molal
do you have any actual image data to plot?Amply
I am little confused here .. Image data ?Molal
I was confused. You are drawing a Basemap, then you plot a hexbin separately. Are you sure you don't want to do m.hexbin(x, y, C=z)? It's unclear to me what image you are trying to show with m.imshow(data).Amply
@Amply I think now I understood what you are saying.. let me try and edit the questions accordingly.Molal
A
2

I now understand. You are trying to combine answers you received from here-imshow and here-hexbin.

Your problem boils down to the fact that you want to use your Basemap as the "canvas" on which you plot your 2D histogram. But instead of doing that, you are making a Basemap then plotting a 2D histogram separately (using plt.hexbin which makes a separate canvas from your Basemap).

Use m.hexbin and get rid of plt.imshow(). If you really want to use imshow, you will need to make a separately 2D histogram array first, then plot it with imshow. Below is how I would proceed with hexbin.


EDIT: Below I randomized some x, y, z data so I could make a plot (and made the coastlines bigger). It's not perfect, but it shows the data plotted.

from matplotlib import pyplot as plt 
from matplotlib import cm as CM
from matplotlib import mlab as ml
from mpl_toolkits.basemap import Basemap
import numpy as np 

m = Basemap(width=12000000, height=9000000, projection='lcc', 
            resolution='c', lat_0=19.,lon_0=73.)
m.drawcoastlines(linewidth=0.25) # I added color='red', lw=2.0

#data = np.loadtxt('inputfile', unpack=True, 
                  dtype='str, int, int, int, int, float')
#
#x  = data[1]
#y  = data[2]
#z  = data[5]
x, y, z = np.random.rand(3, 1000000)
x *= 12e6
y *= 9e6
z *= 20000

gridsize = 100 
m.hexbin(x, y, C=z, gridsize=gridsize, cmap=plt.cm.YlGnBu)

cb = m.colorbar()
m.set_label('Density')
plt.show()

enter image description here

Amply answered 14/5, 2015 at 19:47 Comment(6)
Exactly .. Thank you so much .. They look minor mistakes but make huge difference. trying the above code now ..Molal
I am getting an empty map :( will try my way to full solution .. Thank you so much for the solutions. May be coz the col[1] and col[2] are not exact lat and long those I am giving for basemap.Molal
@SitzBlogz Yes, if you look at the size of the basemap and then the bounds on your hexgrid, they are much much different. Your hexgrid may be showing up, but as an extremely tiny grid in the lower left hand corner of the Basemap (somewhere in East Africa?)Amply
I generated some random data with given lat n long range and still i get empty map.. I also tried reducing and increasing the width and height but its the same.Molal
This works only for random data but when read from file containing same range of lat and long it still shows empty maps.Molal
@SitzBlogz That sounds like a problem with a.) your data file or b.) how you read/store/parse your data, not plotting. Perhaps your grid is too sparse. Either way, it's best to ask a new question showing your updated code, a sample of your input file, and the total shape/size of x, y, and z. Link to the new question here.Amply

© 2022 - 2024 — McMap. All rights reserved.