本文介绍了底图热误差/空图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在定义的地理位置上绘制散布的热图.我可以很好地绘制没有背景的普通散布图,但我想将其与给定的经纬度结合起来.我得到以下空地图.

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 map.

输入

输入:col[2]col[3]xy坐标&地理位置纬度:19.997453,纬度:73.789802

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

程序

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.

如何解决?谢谢!!

推荐答案

我现在明白了.您正在尝试合并从​​ here-imshow 此处-hexbin .

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

您的问题归结为您要使用底图作为绘制2D直方图的画布"这一事实.但是,您没有这样做,而是制作了底图,然后分别绘制了2D直方图(使用plt.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).

使用m.hexbin并摆脱plt.imshow().如果确实要使用imshow,则需要先制作一个单独的2D直方图数组,然后使用imshow对其进行绘制.以下是我继续hexbin的方法.

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.

编辑:下面,我将一些x,y,z数据随机化,以便进行绘图(并增大海岸线).这并不完美,但是它显示了绘制的数据.

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()

这篇关于底图热误差/空图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 21:03