Issue
Let me introduce a bit of context. I have a binary file where I have to extract data. This is a huge matrix which depth of the pixel for each longitude/latitude. The first part of the code is a little code in order to read and store each data (in a lon/lat interval predefined). However, I would like to draw these data on a map of the world and I'm blocked. I can plot data on a blank figure with grids but I don't know how to insert a map of the world (using Basemap).
# -*- coding: utf-8 -*-
import numpy as np
from matplotlib import pyplot as plt
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
from matplotlib.colors import LinearSegmentedColormap
# Definition of the grid
GLDB_res = 1./120.
GLDB_lon = np.linspace(-180.+0.5*GLDB_res, 180.-0.5*GLDB_res, 43200)
GLDB_lat = np.linspace( 90.-0.5*GLDB_res, -90.+0.5*GLDB_res, 21600)
j_Asia = pl.find(pl.logical_and(GLDB_lon>=0.,GLDB_lon<=40.))
i_Asia= pl.find(pl.logical_and(GLDB_lat>=50.,GLDB_lat<=90.))
# Read depth
GLDB_depth_file = 'fileURL/GlobalLakeDepth.dat'
GLDB_depth = np.fromfile(GLDB_depth_file,'<i2')
GLDB_depth_r = GLDB_depth.reshape((21600,43200))
GLDB_depth_Asia = GLDB_depth_r[i_Asia[0]:i_Asia[-1]+1,j_Asia[0]:j_Asia[-1]+1].astype(float)
GLDB_depth_Asia[np.where(GLDB_depth_Asia==0.)] = np.nan
# Création de la Basemap
map=Basemap(projection='cyl', llcrnrlat=50,urcrnrlat=90,\
llcrnrlon=0,urcrnrlon=180, resolution='l')
map.drawcoastlines()
map.fillcontinents(color='#cc9955')
#Colormap
cmap_depth = LinearSegmentedColormap.from_list("my_colormap", ((200./255,200/255.,1.), (20./255,20./255,165./255)), N=255, gamma=1.0)
#Graphique
plt.close()
fig=plt.figure(1,facecolor='w',figsize=(11,7))
ax=plt.subplot(1,2,1)
x,y=map(j_Asia,i_Asia) #associe un x et un y au lon et lat
plt.scatter(x,y, c='my_colormap') #trace les données lacs avec Basemap
c=plt.colorbar(orientation='horizontal')
for i in np.arange(0,len(i_Asia),120/2): #res:1/120th deg
map=plt.plot(np.array([0,len(i_Asia)]),np.array([i,i]),'-',c=(150./255,150./255,150./255))
map=plt.plot(np.array([i,i]),np.array([0,len(i_Asia)]),'-',c=(150./255,150./255,150./255))
plt.show()
In summary, I want to insert my array Global_Depth_Asia which represents all the depth on a map of the region. The problem is that I have a problem of invalid rgb argument!
Thank you
Solution
I obviously cannot run the code to test, but there are some strange things in there.
plt.close()
closes your basemap plot. Why would you do that?plt.figure
creates a new figure without the basemap plot. This is probably undesired. Also remove theplt.subplot
if you want to use the basemap plot to plot to.- Use
map.scatter()
to plot the data to the map. - Provide the data according to which the color should be chosen to the
scatter
'sc
argument Provide the colormap to the
cmap
argument.m.scatter(x,y, c=data, cmap=cmap_depth)
Answered By - ImportanceOfBeingErnest
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.