When doing research on earth and planetary science, I often come across a scene where I want to plot multiple maps and data on one figure at the same time. As a trial, this time, using Python's matplotlib,
--Plot monthly 2D data (global sea surface temperature data) from January to December into one figure at the same time with a map.
The goal is that.
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.basemap import Basemap
xlim = [100, 170] #Scope of the figure(x-axis direction: longitude)
ylim = [10, 60] #Scope of the figure(y-axis direction: latitude)
column = 3 #Number of columns
fsizex, fsizey= 16, 18 #Horizontal / vertical length of the figure
line_interval = 15 #Spacing of parallels and meridians drawn on the map
fontsize = 20 #font size
if 12 % column != 0:
raise ValueError('column must be a divisor of 12!')
row = 12 / column #Number of rows
months = [ 'Jan', 'Feb', 'Mar', 'Apr',
'May', 'Jun', 'Jul', 'Aug',
'Sep', 'Oct', 'Nov', 'Dec' ]
fig = plt.figure(figsize = (fsizex, fsizey))
plt.rcParams['font.size'] = fontsize
ax = [0] * 12
for i in range(0, 12):
ax[i] = fig.add_subplot(row, column, 1 + i)
m = Basemap(projection = 'cyl', llcrnrlat = ylim[0], urcrnrlat = ylim[1], \
llcrnrlon = xlim[0], urcrnrlon = xlim[1], resolution = 'c', lon_0 = 180)
m.drawcoastlines(linewidth = 0.5)
m.drawmapboundary()
m.fillcontinents(color = '#eeeeee')
if i == 12 - column:
label = [1, 1]
elif i > 12 - column:
label = [1, 0]
elif i % column == 0:
label = [0, 1]
else:
label = [0, 0]
m.drawmeridians(np.arange(0, 360, line_interval), labels = [0, 0, 0, label[0]], linewidth = 0.5)
m.drawparallels(np.arange(-90, 90, line_interval), labels = [label[1], 0, 0, 0], linewidth = 0.5)
ax[i].set_xlim(xlim)
ax[i].set_ylim(ylim)
ax[i].set_title(months[i])
fig.tight_layout()
plt.show()
If you are plotting 2D data for each month, combine each data for 12 months into a single 3D array.
xn = 360 #X-axis of data(longitude)Number of grids in the direction
yn = 155 #Y-axis of data(latitude)Number of grids in the direction
data = np.zeros((12, yn, xn))
for i in range(0, 12):
data[i, :, :] = ..... # (i+1)Process to get month data
In addition, a one-dimensional array corresponding to the x-axis (longitude) and y-axis (latitude) of the two-dimensional data is acquired. This time, respectively
(Both are numpy.ndarray
).
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.basemap import Basemap
xlim = [100, 170] #Scope of the figure(x-axis direction: longitude)
ylim = [10, 60] #Scope of the figure(y-axis direction: latitude)
column = 3 #Number of columns
fsizex, fsizey= 16, 18 #Horizontal / vertical length of the figure
cb_min, cb_max = -2, 32 #Minimum / maximum color bar
cb_div = 17 #Number of colors used in the color bar
clabel = 'Sea Surface Temperature(deg)' #Label attached to the color bar
line_interval = 15 #Spacing of parallels and meridians drawn on the map
fontsize = 20 #font size
if 12 % column != 0:
raise ValueError('column must be a divisor of 12!')
row = 12 / column #Number of rows
months = [ 'Jan', 'Feb', 'Mar', 'Apr',
'May', 'Jun', 'Jul', 'Aug',
'Sep', 'Oct', 'Nov', 'Dec' ]
fig = plt.figure(figsize = (fsizex, fsizey))
plt.rcParams['font.size'] = fontsize
delta = (cb_max - cb_min) / cb_div
interval_of_cf = np.arange(cb_min, abs(cb_max) * 2 + delta, delta)[0:int(cb_div) + 1]
ax = [0] * 12
for i in range(0, 12):
ax[i] = fig.add_subplot(row, column, 1 + i)
m = Basemap(projection = 'cyl', llcrnrlat = ylim[0], urcrnrlat = ylim[1], \
llcrnrlon = xlim[0], urcrnrlon = xlim[1], resolution = 'c', lon_0 = 180)
m.drawcoastlines(linewidth = 0.5)
m.drawmapboundary()
m.fillcontinents(color = '#eeeeee')
if i == 12 - column:
label = [1, 1]
elif i > 12 - column:
label = [1, 0]
elif i % column == 0:
label = [0, 1]
else:
label = [0, 0]
m.drawmeridians(np.arange(0, 360, line_interval), labels = [0, 0, 0, label[0]], linewidth = 0.5)
m.drawparallels(np.arange(-90, 90, line_interval), labels = [label[1], 0, 0, 0], linewidth = 0.5)
#Draw color contours
x, y = np.meshgrid(xgrid, ygrid)
X, Y = m(x, y)
CF = ax[i].contourf(X, Y, data[i, :, :], interval_of_cf)
ax[i].set_xlim(xlim)
ax[i].set_ylim(ylim)
ax[i].set_title(months[i])
#Draw a color bar
cax = fig.add_axes([1.00, 0.15, 0.04, 0.7])
cb = fig.colorbar(CF, cax)
cb.set_label(clabel)
fig.tight_layout()
plt.show()
Succeeded in creating safely. The combination of vertical and horizontal can be freely changed by changing the value of column.
http://seesaawiki.jp/met-python/d/matplotlib/plot http://bicycle1885.hatenablog.com/entry/2014/02/14/023734 http://nm-player.blogspot.jp/2012/09/matplotlibbasemap-1.html http://qiita.com/AnchorBlues/items/0dd1499196670fdf1c46
Recommended Posts