import matplotlib.pyplot as plt
import numpy as np
import xarray as xr
# Uncomment these lines and run this cell once, then recomment
# !pip install netcdf4
from google.colab import drive
drive.mount('/content/drive')
Mounted at /content/drive
# Create some data
x = np.array([1,2,3,4,5,6])
y = x**2
# Make a figure
fig = plt.figure()
# Plot the data - c=r, ls='-', marker='o', lw=2
plt.plot(x,y, c='r', ls='-', marker='o', ms=5, lw=2)
# Format the figure
plt.xlabel('x')
plt.ylabel('y')
plt.title('Example line plot')
Text(0.5, 1.0, 'Example line plot')
# Create some data
x = np.array([1,2,3,4,5,6])
y = x**2
# Make a figure
fig = plt.figure()
# Plot the data - c='b', ls='--', marker='s', lw=0.5
plt.plot(x,y, c='b', ls='--', marker='s', ms=5, lw=0.5)
# Format the figure
plt.xlabel('x')
plt.ylabel('y')
plt.title('Example line plot')
Text(0.5, 1.0, 'Example line plot')
# Make some data
x = np.array([1,2,3,4,5,6])
y = x**2
# Create a figure
fig = plt.figure()
# Make a plot - s=40, c='r', ls='-'
scat = plt.scatter(x,y, s=40, c='r', marker='o', ls='-', lw=1, edgecolor='k')
# Format the plot
plt.xlabel('x')
plt.ylabel('y')
plt.title('Example scatter plot')
Text(0.5, 1.0, 'Example scatter plot')
# Make some data
x = np.array([1,2,3,4,5,6])
y = x**2
z = x**3
# Create a figure
fig = plt.figure()
ax = plt.gca()
# Make a plot - s=100, c=z, ls='--', lw=1
scat = plt.scatter(x,y, s=100, c=z, marker='o', ls='--', lw=1, edgecolor='k')
plt.xlabel('x')
plt.ylabel('y')
plt.title('Example plot')
c = plt.colorbar(scat, ax=ax)
c.set_label('x**3')
# Make some data
x = np.array([1,2,3,4,5,6])
y = x**2
z = x**3
# Create a figure
fig = plt.figure()
ax = plt.gca()
# Make a plot - s=100, c=z, ls='--', lw=1
scat = plt.scatter(x,y, s=100, c=z, marker='o', ls='--', lw=1, edgecolor='k', cmap='afmhot')
plt.xlabel('x')
plt.ylabel('y')
plt.title('Example plot')
c = plt.colorbar(scat, ax=ax)
c.set_label('x**3')
# Change the filepath to wherever your data file is in your drive
filepath = '/content/drive/My Drive/Data_folder/woa18_temp.nc'
# Read the data
temp = xr.open_dataset(filepath)
t_data = temp['t_an'].values.flatten()
# Create a figure
fig = plt.figure(figsize=(11,7))
# Plot a histogram- bins=100, alpha=0.5
h = plt.hist(t_data, bins=100, rwidth=0.6, color='b',edgecolor='k', alpha=0.5)
# Format the figure
plt.xlabel('Temperature ˚C')
plt.ylabel('Count')
plt.title('WOA Global Temperature Data')
/usr/local/lib/python3.6/dist-packages/numpy/lib/histograms.py:839: RuntimeWarning: invalid value encountered in greater_equal keep = (tmp_a >= first_edge) /usr/local/lib/python3.6/dist-packages/numpy/lib/histograms.py:840: RuntimeWarning: invalid value encountered in less_equal keep &= (tmp_a <= last_edge)
Text(0.5, 1.0, 'WOA Global Temperature Data')
# Create a figure
fig = plt.figure(figsize=(11,7))
# Plot a histogram- bins=100, alpha=0.9
h = plt.hist(t_data, bins=100, rwidth=0.6, color='b',edgecolor='k', alpha=0.9)
# Format the figure
plt.xlabel('Temperature ˚C')
plt.ylabel('Count')
plt.title('WOA Global Temperature Data')
/usr/local/lib/python3.6/dist-packages/numpy/lib/histograms.py:839: RuntimeWarning: invalid value encountered in greater_equal keep = (tmp_a >= first_edge) /usr/local/lib/python3.6/dist-packages/numpy/lib/histograms.py:840: RuntimeWarning: invalid value encountered in less_equal keep &= (tmp_a <= last_edge)
Text(0.5, 1.0, 'WOA Global Temperature Data')
# Create a figure
fig = plt.figure(figsize=(11,7))
# Plot a histogram- bins=range(0,31), alpha=0.9
h = plt.hist(t_data, bins=range(0,31), rwidth=0.6, color='b',edgecolor='k', alpha=0.9)
# Format the figure
plt.xlabel('Temperature ˚C')
plt.ylabel('Count')
plt.title('WOA Global Temperature Data')
Text(0.5, 1.0, 'WOA Global Temperature Data')
# Change the path to wherever you have the data
filepath = '/content/drive/My Drive/Data_folder/woa18_oxy.nc'
# Read the data - times are in a bad format, so don't parse them
oxy = xr.open_dataset(filepath,decode_times=False)
display(oxy)
# Take the average of the data along longitudes and select the only time level
# Make numpy arrays for lat, depth, and o_data
o_data = oxy['o_an'].mean(dim='lon').isel(time=0).values
lat = oxy['lat'].values
depth = oxy['depth'].values
array([8214.], dtype=float32)
array([0.00e+00, 5.00e+00, 1.00e+01, 1.50e+01, 2.00e+01, 2.50e+01, 3.00e+01, 3.50e+01, 4.00e+01, 4.50e+01, 5.00e+01, 5.50e+01, 6.00e+01, 6.50e+01, 7.00e+01, 7.50e+01, 8.00e+01, 8.50e+01, 9.00e+01, 9.50e+01, 1.00e+02, 1.25e+02, 1.50e+02, 1.75e+02, 2.00e+02, 2.25e+02, 2.50e+02, 2.75e+02, 3.00e+02, 3.25e+02, 3.50e+02, 3.75e+02, 4.00e+02, 4.25e+02, 4.50e+02, 4.75e+02, 5.00e+02, 5.50e+02, 6.00e+02, 6.50e+02, 7.00e+02, 7.50e+02, 8.00e+02, 8.50e+02, 9.00e+02, 9.50e+02, 1.00e+03, 1.05e+03, 1.10e+03, 1.15e+03, 1.20e+03, 1.25e+03, 1.30e+03, 1.35e+03, 1.40e+03, 1.45e+03, 1.50e+03, 1.55e+03, 1.60e+03, 1.65e+03, 1.70e+03, 1.75e+03, 1.80e+03, 1.85e+03, 1.90e+03, 1.95e+03, 2.00e+03, 2.10e+03, 2.20e+03, 2.30e+03, 2.40e+03, 2.50e+03, 2.60e+03, 2.70e+03, 2.80e+03, 2.90e+03, 3.00e+03, 3.10e+03, 3.20e+03, 3.30e+03, 3.40e+03, 3.50e+03, 3.60e+03, 3.70e+03, 3.80e+03, 3.90e+03, 4.00e+03, 4.10e+03, 4.20e+03, 4.30e+03, 4.40e+03, 4.50e+03, 4.60e+03, 4.70e+03, 4.80e+03, 4.90e+03, 5.00e+03, 5.10e+03, 5.20e+03, 5.30e+03, 5.40e+03, 5.50e+03], dtype=float32)
array([-89.5, -88.5, -87.5, -86.5, -85.5, -84.5, -83.5, -82.5, -81.5, -80.5, -79.5, -78.5, -77.5, -76.5, -75.5, -74.5, -73.5, -72.5, -71.5, -70.5, -69.5, -68.5, -67.5, -66.5, -65.5, -64.5, -63.5, -62.5, -61.5, -60.5, -59.5, -58.5, -57.5, -56.5, -55.5, -54.5, -53.5, -52.5, -51.5, -50.5, -49.5, -48.5, -47.5, -46.5, -45.5, -44.5, -43.5, -42.5, -41.5, -40.5, -39.5, -38.5, -37.5, -36.5, -35.5, -34.5, -33.5, -32.5, -31.5, -30.5, -29.5, -28.5, -27.5, -26.5, -25.5, -24.5, -23.5, -22.5, -21.5, -20.5, -19.5, -18.5, -17.5, -16.5, -15.5, -14.5, -13.5, -12.5, -11.5, -10.5, -9.5, -8.5, -7.5, -6.5, -5.5, -4.5, -3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5, 11.5, 12.5, 13.5, 14.5, 15.5, 16.5, 17.5, 18.5, 19.5, 20.5, 21.5, 22.5, 23.5, 24.5, 25.5, 26.5, 27.5, 28.5, 29.5, 30.5, 31.5, 32.5, 33.5, 34.5, 35.5, 36.5, 37.5, 38.5, 39.5, 40.5, 41.5, 42.5, 43.5, 44.5, 45.5, 46.5, 47.5, 48.5, 49.5, 50.5, 51.5, 52.5, 53.5, 54.5, 55.5, 56.5, 57.5, 58.5, 59.5, 60.5, 61.5, 62.5, 63.5, 64.5, 65.5, 66.5, 67.5, 68.5, 69.5, 70.5, 71.5, 72.5, 73.5, 74.5, 75.5, 76.5, 77.5, 78.5, 79.5, 80.5, 81.5, 82.5, 83.5, 84.5, 85.5, 86.5, 87.5, 88.5, 89.5], dtype=float32)
array([-179.5, -178.5, -177.5, ..., 177.5, 178.5, 179.5], dtype=float32)
[6609600 values with dtype=float32]
/usr/local/lib/python3.6/dist-packages/xarray/core/nanops.py:142: RuntimeWarning: Mean of empty slice return np.nanmean(a, axis=axis, dtype=dtype)
# Create a figure
fig = plt.figure(figsize=(10,7))
ax = plt.gca()
# Create a contour plot
cntr = plt.contour(lat, depth, o_data)
c = plt.colorbar(cntr, ax=ax)
c.set_label('Oxygen µmol/L')
# Format the plot
ax.invert_yaxis()
plt.xlabel('Latitude')
plt.ylabel('Depth (m)')
plt.title('WOA Oxygen by Latitude')
Text(0.5, 1.0, 'WOA Oxygen by Latitude')
fig = plt.figure(figsize=(10,7))
ax = plt.gca()
cntr = plt.contour(lat, depth, o_data, levels=20)
c = plt.colorbar(cntr, ax=ax)
c.set_label('Oxygen µmol/L')
ax.invert_yaxis()
plt.xlabel('Latitude')
plt.ylabel('Depth (m)')
plt.title('WOA Oxygen by Latitude')
Text(0.5, 1.0, 'WOA Oxygen by Latitude')
fig = plt.figure(figsize=(10,7))
ax = plt.gca()
cntr = plt.contour(lat, depth, o_data, levels=20, colors='k')
ax.invert_yaxis()
plt.xlabel('Latitude')
plt.ylabel('Depth (m)')
plt.title('WOA Oxygen by Latitude')
plt.clabel(cntr, levels=cntr.levels[::2])
<a list of 16 text.Text objects>
fig = plt.figure(figsize=(10,7))
ax = plt.gca()
cntr = plt.contourf(lat, depth, o_data, levels=20)
c = plt.colorbar(cntr, ax=ax)
c.set_label('Oxygen µmol/L')
ax.invert_yaxis()
plt.xlabel('Latitude')
plt.ylabel('Depth (m)')
plt.title('WOA Oxygen by Latitude')
Text(0.5, 1.0, 'WOA Oxygen by Latitude')
fig = plt.figure(figsize=(10,7))
ax = plt.gca()
cntr = plt.contourf(lat, depth, o_data, levels=20,cmap='plasma')
cntr_lines = plt.contour(lat, depth, o_data, levels=20,colors='k')
c = plt.colorbar(cntr, ax=ax)
c.set_label('Oxygen µmol/L')
ax.invert_yaxis()
plt.xlabel('Latitude')
plt.ylabel('Depth (m)')
plt.title('WOA Oxygen by Latitude')
plt.clabel(cntr_lines, levels=cntr.levels[::2])
<a list of 15 text.Text objects>
fig = plt.figure(figsize=(10,7))
ax = plt.gca()
cntr = plt.pcolormesh(lat, depth, o_data, cmap='plasma')
c = plt.colorbar(cntr, ax=ax)
c.set_label('Oxygen µmol/L')
ax.invert_yaxis()
plt.xlabel('Latitude')
plt.ylabel('Depth (m)')
plt.title('WOA Oxygen by Latitude')
Text(0.5, 1.0, 'WOA Oxygen by Latitude')
fig = plt.figure(figsize=(10,7))
ax = plt.gca()
cntr = plt.pcolormesh(lat, depth, o_data,cmap='plasma')
# cntr_lines = plt.contour(lat, depth, o_data, levels=20,colors='k')
c = plt.colorbar(cntr, ax=ax)
c.set_label('Oxygen µmol/L')
ax.invert_yaxis()
plt.xlabel('Latitude')
plt.ylabel('Depth (m)')
plt.title('WOA Oxygen by Latitude')
# plt.clabel(cntr_lines, levels=cntr_lines.levels[::2])
fig,ax = plt.subplots(nrows=1,ncols=2,figsize=(18,7))
cntr = ax[0].pcolormesh(lat, depth, o_data, cmap='plasma',vmin=220,vmax=340)
c = plt.colorbar(cntr, ax=ax[0])
c.set_label('Oxygen µmol/L')
ax[0].set_ylim((0,100))
ax[0].set_xlim((-60,-30))
ax[0].invert_yaxis()
ax[0].set_xlabel('Latitude')
ax[0].set_ylabel('Depth (m)')
ax[0].set_title('WOA Oxygen by Latitude')
cntr = ax[1].contourf(lat, depth, o_data, levels=40,cmap='plasma',vmin=220,vmax=340)
c = plt.colorbar(cntr, ax=ax[1])
c.set_label('Oxygen µmol/L')
ax[1].set_ylim((0,100))
ax[1].set_xlim((-60,-30))
ax[1].invert_yaxis()
ax[1].set_xlabel('Latitude')
ax[1].set_ylabel('Depth (m)')
ax[1].set_title('WOA Oxygen by Latitude')
Text(0.5, 1.0, 'WOA Oxygen by Latitude')
# # This code allows cartopy to work with Google Colab
# # Run this code only once per notebook
# !grep '^deb ' /etc/apt/sources.list | \
# sed 's/^deb /deb-src /g' | \
# tee /etc/apt/sources.list.d/deb-src.list
# !apt-get -qq update
# !apt-get -qq build-dep python3-cartopy
# !pip uninstall -y shapely
# !pip install shapely --no-binary shapely
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
projections = [ccrs.PlateCarree(),
ccrs.Robinson(),
ccrs.Mercator(),
ccrs.Orthographic(),
ccrs.InterruptedGoodeHomolosine()
]
for proj in projections:
plt.figure()
ax = plt.axes(projection=proj)
ax.coastlines()
ax.set_title(f'{type(proj)}')
fig= plt.figure(figsize=(18,12))
ax1 = fig.add_subplot(1,2,1,projection=ccrs.PlateCarree(central_longitude=180))
ax1.coastlines()
ax2 = fig.add_subplot(1,2,2,projection=ccrs.PlateCarree())
ax2.coastlines()
<cartopy.mpl.feature_artist.FeatureArtist at 0x7f0430f6c208>
fig = plt.figure(figsize=(10,10))
ax = plt.axes(projection=ccrs.PlateCarree())
ax.coastlines(resolution='110m',color='k')
gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=True,
linewidth=2, color='gray', alpha=0.5, linestyle=':')
gl.xlabels_top = False
gl.ylabels_right = False
gl.xformatter = LONGITUDE_FORMATTER
gl.yformatter = LATITUDE_FORMATTER
ax.add_feature(cfeature.LAND, color='papayawhip')
ax.add_feature(cfeature.OCEAN, color='cornflowerblue')
extent = [-170,-70,15,50]
ax.set_extent(extent)
ax.add_feature(cfeature.LAKES,color='navy')
ax.add_feature(cfeature.RIVERS.with_scale('10m'),edgecolor='aqua')
ax.add_feature(cfeature.STATES,edgecolor='k')
seattle = [-122.332, 47.606]
honolulu = [-157.8583, 21.3069]
ax.plot([seattle[0],honolulu[0]],[seattle[1],honolulu[1]],'ro-',lw=5,transform=ccrs.PlateCarree())
ax.plot([seattle[0],honolulu[0]],[seattle[1],honolulu[1]],'ro:',lw=5,transform=ccrs.Geodetic())
[<matplotlib.lines.Line2D at 0x7f0432d6fb38>]
# Using our Oxygen data
# Change the path to wherever you have the data
filepath = '/content/drive/My Drive/Data_folder/woa18_oxy.nc'
# Read the data - times are in a bad format, so don't parse them
oxy = xr.open_dataset(filepath,decode_times=False)
# Take the average of the data along longitudes and select the only time level
# Make numpy arrays for lat, depth, and o_data
lat = oxy['lat'].values
lon = oxy['lon'].values
o_data = oxy['o_an'].sel(depth=0,method='nearest').isel(time=0).values
fig = plt.figure(figsize=(15,8))
ax = plt.axes(projection=ccrs.PlateCarree())
pcm = ax.pcolormesh(lon,lat,o_data,transform=ccrs.PlateCarree())
cntr = ax.contour(lon,lat,o_data,levels=17,transform=ccrs.PlateCarree(),colors='w')
ax.clabel(cntr,levels=cntr.levels[::3],colors='w',fontsize=8)
c = plt.colorbar(pcm, ax=ax)
c.set_label('Oxygen µmol/L')
ax.add_feature(cfeature.LAND, color='k',alpha=0.8)
gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=True,
linewidth=0.5, color='w', alpha=0.7, linestyle='--')
gl.xlabels_top = False
gl.ylabels_right = False
gl.xformatter = LONGITUDE_FORMATTER
gl.yformatter = LATITUDE_FORMATTER
ax.set_title('Oxygen at 0m')
Text(0.5, 1.0, 'Oxygen at 0m')