%matplotlib inline
import matplotlib.pylab as plt
import numpy as np
from netCDF4 import Dataset, num2date
from mpl_toolkits.basemap import Basemap
import pandas as pd
We are going to work with REMO model regional simmulation
var167
),
from netCDF4 import Dataset
f = Dataset("../data/1989_2008_2mTmp.nc")
temp=f.variables['var167'][:]
lon = f.variables['lon'][:]
lat = f.variables['lat'][:]
First we use the simple way to look at the data with imshow:
plt.imshow(temp[0,0,:,:])
<matplotlib.image.AxesImage at 0x7f50223024d0>
In order to plot data on actuall map, we have to have information about location of every grid point. Let's have a look how out lat information look like:
lon.shape
(195,)
It is 1d, while we need 2d information. We can easily make 2d arrays from 1d:
lon,lat = np.meshgrid(lon,lat)
Now it's 2d:
lat.shape
(168, 195)
lon.shape
(168, 195)
The library that we are going to use for plotting called Basemap.
from mpl_toolkits.basemap import Basemap
Firs we have to create map instance, that will define projection of the map and and its borders
m = Basemap(llcrnrlon=55,llcrnrlat=0,urcrnrlon=105,urcrnrlat=42,projection='mill')
lon/lat information has to be converted to the coordinates of the image:
x, y = m(lon, lat)
Now we can create our first map:
#set the size of the figure
plt.figure(figsize=(7,7))
#draw costlibes
m.drawcoastlines()
#create contour plot
m.contourf(x,y,temp[0,0,:,:],levels=np.linspace(250,300,21), extend="both");
#plot colorbar
plt.colorbar(orientation="horizontal", pad=0.03)
#plt.savefig('ind_temp_all.png', dpi=100)
<matplotlib.colorbar.Colorbar instance at 0x7f502208e560>
m = Basemap(llcrnrlon=55,llcrnrlat=0,urcrnrlon=105,urcrnrlat=42,projection='mill', resolution='i')
x, y = m(lon, lat)
plt.figure(figsize=(7,7))
m.drawcoastlines()
m.contourf(x,y,temp[0,0,:,:],levels=np.linspace(250,300,41), extend="both");
plt.colorbar(orientation="horizontal", pad=0.03)
<matplotlib.colorbar.Colorbar instance at 0x7f5021c72e18>
Come back to the coarse resolution and plot some nice background:
m = Basemap(llcrnrlon=55,llcrnrlat=0,urcrnrlon=105,urcrnrlat=42,projection='mill', resolution='c')
x, y = m(lon, lat)
plt.figure(figsize=(7,7))
m.drawcoastlines()
m.bluemarble()
m.contourf(x,y,temp[0,0,:,:],levels=np.linspace(250,300,41), extend="both");
plt.colorbar(orientation="horizontal", pad=0.03)
<matplotlib.colorbar.Colorbar instance at 0x7f5021ba21b8>
Put another background and put some transparency:
plt.figure(figsize=(7,7))
m.drawcoastlines()
m.etopo()
m.contourf(x,y,temp[0,0,:,:],levels=np.linspace(250,300,21), extend="both", alpha=0.7);
plt.colorbar(orientation="horizontal", pad=0.03)
<matplotlib.colorbar.Colorbar instance at 0x7f5021bcdea8>
Put some parralels and meredians:
m = Basemap(llcrnrlon=55,llcrnrlat=0,urcrnrlon=105,urcrnrlat=42,projection='mill', resolution='c')
x, y = m(lon, lat)
plt.figure(figsize=(7,7))
m.drawcoastlines()
parallels = np.arange(0.,81,10.)
# labels = [left,right,top,bottom]
m.drawparallels(parallels,labels=[False,True,True,False])
meridians = np.arange(10.,351.,20.)
m.drawmeridians(meridians,labels=[True,False,False,True])
m.contourf(x,y,temp[0,0,:,:],levels=np.linspace(250,300,21), extend="both");
plt.colorbar(orientation="horizontal", pad=0.05)
plt.savefig('myfigure.png', dpi=300)
Use some strange colors:
plt.figure(figsize=(7,7))
m.drawcoastlines()
parallels = np.arange(0.,81,10.)
# labels = [left,right,top,bottom]
m.drawparallels(parallels,labels=[False,True,True,False])
meridians = np.arange(10.,351.,20.)
m.drawmeridians(meridians,labels=[True,False,False,True])
m.contourf(x,y,temp[0,0,:,:],levels=np.linspace(250,300,21), extend="both", cmap=plt.cm.Accent);
plt.colorbar(orientation="horizontal", pad=0.05)
<matplotlib.colorbar.Colorbar instance at 0x7f50205cfea8>
m = Basemap(llcrnrlon=55,llcrnrlat=0,urcrnrlon=105,urcrnrlat=42,projection='mill')
x, y = m(lon, lat)
m= Basemap(projection='rotpole',\
lon_0=180-123.34,\
lat_0=90-79.95,\
o_lon_p=-123.34,\
o_lat_p=79.95,\
llcrnrlat = 4,\
urcrnrlat = 38,\
llcrnrlon = 59,\
urcrnrlon = 105,\
resolution='c')
x, y = m(lon, lat)
plt.figure(figsize=(10,10))
m.drawcoastlines()
m.contourf(x,y,temp[0,0,:,:],levels=np.linspace(250,300,41), extend="both");
plt.colorbar(orientation="horizontal", pad=0.03)
plt.savefig('ind_temp_all.png', dpi=100)
Plot precipitation data ('../data/1989_2008_Precip.nc')