我试图绘制波多黎各地区雷击累积总和的轮廓图,但是数据在该图的右下角绘制轮廓。除此情节定位外,其他所有内容均正常工作,该情节应显示在波多黎各的左上角附近。我很困惑为什么会这样,因为我对相同的纬度和经度数据所做的散点图将其完美地定位在整个区域上,只是下面的轮廓给我带来了麻烦。
通过研究类似的问题,似乎解决方案在于以某种方式调整网格,但是当我对其进行更改时,绘图会变大或变小,但不会移动位置。与底图相比,地块的比例可能有问题吗?
这是我所拥有的:
import numpy as n
import pandas as pd
import matplotlib.pyplot as p
import datetime as datetime
from scipy import stats
import matplotlib.pyplot as p
from mpl_toolkits.basemap import Basemap
from matplotlib.colors import Normalize
data = n.genfromtxt("/home/008918949/eni_tl.txt", usecols=
(0,1,2,3,4,5,6,7,8,9,10,11))
month= data[:,0]
day= data[:,1]
year = data[:,2]
hour= data[:,3]
minute = data[:,4]
second = data[:,5]
latitude = data[:,6]
longitude = data[:,7]
amplitude = data[:,8]
l_type = data[:,10]
n_strikes = data[:,11]
n_strikes=n.cumsum(n_strike) #cumulative sum of the strikes
#Plotting Basemap:
norm = Normalize()
p.figure(figsize=(17,7))
lllat=17.4
ulat=18.6
lllon=-67.6
ulon=-64.4
my_map = Basemap(projection='mill', lat_0=18, lon_0=-66,
resolution = 'h', area_thresh = 50,
llcrnrlon=lllon, llcrnrlat=lllat,
urcrnrlon=ulon, urcrnrlat=ulat)
my_map.drawcoastlines()
my_map.drawcountries()
my_map.fillcontinents(color='lightgreen')
my_map.drawmapboundary(fill_color = 'white')
my_map.drawmeridians(n.arange(lllon, ulon, 1.),
color = '0.25', linewidth = 0.5,
labels=[False, False, False, True])
my_map.drawparallels(n.arange(lllat, ulat, 1.),
color = 'black', linewidth = 0.1,
labels=[True, False, False, False])
my_map.drawmapscale(18 + 0.08, -66 + 0.015,
18, -66,
10.,
barstyle='fancy', labelstyle='simple',
fillcolor1='w', fillcolor2='#555555',
fontcolor='#555555',
zorder=5)
#Transform lon / lat coordinates from data to map projection
lon2012=map(int, longitude) #making sure all data is in integers
lat2012=map(int, latitude)
lon2012, lat2012 = my_map(*(lon2012, lat2012))
#Grid data:(These are the values for the minumum and maximum latitude and longitude of data in map coordinates):
lon2012min= 66716.924570808071 #meters
lon2012max= 400301.54742485163
lat2012min= -45792.225173141342
lat2012max= 68807.774434396997
xi = n.linspace(lon2012min, lon2012max)
yi = n.linspace(lat2012min, lat2012max)
xi, yi = n.meshgrid(xi, yi)
#Interpolate
x, y, z = lon2012, lat2012, n_strikes
from scipy.interpolate import griddata as gd
zi = gd((lon2012, lat2012),n_strikes,(xi, yi),
method='linear')
zimin=n.min(zi)
zimax=n.max(zi)
#Contour plot alpha 0.6
con = my_map.contourf(xi, yi, zi, zorder=4, alpha=0.6, cmap='YlOrRd')
#Add color bar and title
#Add color bar, title, and scale
cbar = p.colorbar(con, orientation='horizontal', fraction=.057, pad=0.05)
cbar.set_label("Cumulative Sum of Lightning Strikes (Counts)")
my_map.drawmapscale(
-67.4, 19.5, 28., -13,
100,
units='km', fontsize=10,
yoffset=None,
barstyle='fancy', labelstyle='simple',
fillcolor1='w', fillcolor2='#000000',
fontcolor='#000000',
zorder=5)
p.title("Cumulative Lightning Strikes Over Puerto Rico and Virgin Islands
Contour Plot (2012-2017)",
fontsize=18)
p.savefig("contourtest.png", dpi=100)
p.show()
结果如下:
另外,万一有什么奇怪的话,meshgrid中lat2012和lon2012的最小值和最大值是正确的,因为我对它们执行了min()和max()函数。我只是将实际数字值写出来作为检查,但是任何一种方法都可以得到相同的结果。
如果这是一个简单的解决方法,我深表歉意,但是我在这个问题上花费了很多时间,因此能提供任何帮助。