为什么不通过底图显示功能在x轴和y轴上获得经度和纬度?

时间:2018-08-31 05:48:14

标签: python matplotlib-basemap imshow

我在map.imshow中使用了纬度和经度作为范围,但没有在x轴和y轴上获得经度和纬度,而是获得了其他v值,但是在plt.imshow中可以正常工作吗?请帮助我简化这个问题,谢谢

from __future__ import print_function

import numpy as np  

import pandas as pd  

import matplotlib.pyplot as plt

from matplotlib_scalebar.scalebar import ScaleBar

from matplotlib_scalebar.scalebar import SI_LENGTH

import matplotlib.font_manager as fm

from mpl_toolkits.basemap import Basemap

import rasterio

from rasterio.plot import show

from matplotlib import colors

from affine import Affine

from pyproj import Proj, transform

#basemap funnction call

map = Basemap(llcrnrlon=-10.5,llcrnrlat=35,urcrnrlon=4.,urcrnrlat=44.,
               projection='tmerc', lat_0 = 39.5, lon_0 = -3.25, 
               suppress_ticks=False)


raster = rasterio.open('lt51350412010304.tif')

#band 3
red = raster.read(3)

red = red.astype(float)

#band 4
nir = raster.read(4)

nir = nir.astype(float)

A = raster.read()

T0 = raster.affine

np.seterr(divide='ignore', invalid='ignore')

#print(raster.shape)
ndvi = np.zeros(raster.shape, dtype=rasterio.float32)

check = np.logical_or ( red > 0, nir > 0 )

ndvi_calc = np.where ( check,  (nir - red ) / ( nir + red ),  0.00000001)

#ndvi calculation 
ndvi = (nir - red ) / ( nir + red )

p1 = Proj(raster.crs)

# All rows and columns
cols, rows = np.meshgrid(np.arange(A.shape[2]), np.arange(A.shape[1]))

# Get affine transform for pixel centres
T1 = T0 * Affine.translation(0.5, 0.5)

# Function to convert pixel row/column index (from 0) to easting/northing at 
centre
rc2en = lambda r, c: (c, r) * T1

所有东边和北边(可能有一种更快的方法)东边,北边= np.vectorize(rc2en,otypes = [np.float,np.float])(行,cols)

# Project all longitudes, latitudes
p2 = Proj(proj='latlong',datum='WGS84')

longs, lats = transform(p1, p2, eastings, northings)

我想显示的范围

extents = (longs.min(), longs.max(), lats.min(), lats.max())

print(extents)

#print("crs")
#print(p1)

print('NDVI matrix: ')

print(ndvi_calc)

print('\nMax NDVI: {m}'.format(m=ndvi_calc.max()))

print('Mean NDVI: {m}'.format(m=ndvi_calc.mean()))

print('Median NDVI: {m}'.format(m=np.median(ndvi_calc)))

print('Min NDVI: {m}'.format(m=ndvi_calc.min()))

print('Deviation NDVI: {m}'.format(m=ndvi_calc.std()))

print('Longitude: {m}'.format(m=longs))

print('Latitude: {m}'.format(m=lats))

fig = plt.figure(figsize=(10, 8))

map.drawmapscale(-7., 35.8, -3.25, 39.5, 500, barstyle='fancy')

在某种程度上,我正在使用纬度和经度,但得到了其他东西

map.imshow(ndvi, aspect='auto', extent = extents)

map.colorbar(location='bottom', pad=0.5, label='Mean amplitude (kA)')

plt.show()

0 个答案:

没有答案