После добавления меридианов, параллей и масштаба базовая карта возвращается пустой - PullRequest
0 голосов
/ 06 июля 2019

Я пытаюсь построить карту глубины, используя Basemap в python. Контур и pcolormesh работают, но они, когда я добавляю меридианы, параллели и масштаб, возвращают пустое изображение.

Я попытался построить один за другим, исключая меридианы и параллы, и добавил только масштаб, но возвращает пустую карту, и то же самое с остальными. Я использовал тот же код раньше, и он работал ...

before add merdians,paralles or scale

after add merians and paralllels

import netCDF4 as nc 
from netCDF4 import Dataset
import numpy as np
import matplotlib.pyplot as plt 
from mpl_toolkits.basemap import Basemap 
from matplotlib import ticker 


grid = nc.Dataset('remo_grd.nc', mode='r')
h = grid.variables['h'][:]
h = h.astype(int)
h=-h

lon= grid.variables['lon_rho'][:]
lat= grid.variables['lat_rho'][:]

latmin = np.min(lat)
latmax= np.max(lat)
lonmax= np.max(lon)
lonmin= np.min(lon)

fig = plt.figure(1, figsize = (7,5.4), dpi = 100)

ax = fig.add_subplot(111)
m = Basemap(projection='merc', llcrnrlon=lonmin-0.2, urcrnrlon=lonmax+0.2,   llcrnrlat=latmin-0.2, urcrnrlat=latmax+0.2, lat_ts=0, resolution='i')
xi, yi = m(lon,lat)

m.ax = ax

cs= m.pcolormesh(xi, yi, np.squeeze(h), shading = 'flat', zorder = 2)
levels = [-1000, -200]
a = m.contour(xi, yi, np.squeeze(h), levels, colors = 'black', linestyles = 'solid', linewidth= 1.5, extend = 'both', zorder = 3 ) 
plt.clabel(a, inline=2, fontsize= 10, linewidth= 1.0, fmt = '%.f', zorder= 4)

ax.text(0.5, -0.07, 'Longitude', transform=ax.transAxes, ha='center', va='center', fontsize = '10')
ax.text(-0.15, 0.5, 'Latitude', transform=ax.transAxes, ha= 'center', va='center', rotation='vertical', fontsize = '10') 

m.drawcoastlines(linewidth=1.5, color = '0.1',zorder=5) 
m.fillcontinents(color=('gray'),zorder=5 )
m.drawstates(linewidth = 0.5, zorder = 7)
m.drawmapboundary(color = 'black', zorder = 8, linewidth =1.2)
m.drawparallels(np.arange(int(latmin),int(latmax),3),labels=[1,0,0,0], linewidth=0.0, zorder =0)
m.drawmeridians(np.arange(int(lonmin),int(lonmax),3),labels=[0,0,0,1], linewidth=0.0)

cbar = plt.colorbar(cs, shrink=0.97, extend = 'both')
cbar.set_ticks([-10, -250, -500, -750, -1000, -1250, -1500, -1750, -2000, -2250, -2500])
cbar.set_ticklabels([-10, -250, -500, -750, -1000, -1250, -1500, -1750, -2000, -2250, -2500])

cbar.set_label('Meters (m)' , size = 10, labelpad = 20, rotation = 270)
ax = cbar.ax.tick_params(labelsize = 9)

titulo='Depth'
plt.title(titulo, va='bottom', fontsize='12')

#plot scale
dref=200

# Coordinates
lat0=m.llcrnrlat+0.9
lon0=m.llcrnrlon+1.9

#Tricked distance to provide to the the function 
distance=dref/np.cos(lat0*np.pi/180.)

# Due to the bug, the function will draw a bar of length dref
scale=m.drawmapscale(lon0,lat0,lon0,lat0,distance, barstyle='fancy', units='km', labelstyle='simple',fillcolor1='w', fillcolor2='#555555', fontcolor='#555555', zorder = 8)        

 #Modify the labels with dref instead of distance
 scale[12].set_text(dref/2)
 scale[13].set_text(dref)
 plt.show()

1 Ответ

0 голосов
/ 08 июля 2019

Я решил проблему! Я устанавливал определенный порядок для построения каждой детали с помощью функции zorder, поэтому я перекрывал данные

...