openearth / oceanwaves-python

Toolbox to handle ocean waves datasets
http://oceanwaves.readthedocs.io
MIT License
64 stars 39 forks source link

spectra data without time and location is plotted as x-y plot #24

Closed raybellwaves closed 6 years ago

raybellwaves commented 6 years ago

I have some spectra data and I am putting it into an oceanwave object to create a 2d polar spectra plot. However doing ow.plot() gives a x-y contour instead of a polar contour. Is there a way can request a polar contour?

import oceanwaves
import xarray as xr
import numpy as np
import matplotlib.pyplot as plt

# Create a spectra file
dir_bins = np.arange(7.5, 352.5 + 15, 15)
freq_bins = np.full(30, 0.03453, dtype=np.float) * (1.1 ** np.arange(0, 30))
data = np.ones((len(freq_bins), len(dir_bins)))
spec = xr.DataArray(data, coords=[freq_bins, dir_bins], dims=['frequency', 'direction'])
print(spec)
print('')

# Put it in an oceanwaves object
ow = oceanwaves.OceanWaves(frequency=spec.frequency,
                           direction=spec.direction,
                           energy=spec)
print(ow)
ow.plot()
screen shot 2018-09-14 at 1 42 04 pm

It produces a polar plot when time and location are specified

import xarray as xr
import numpy as np
from datetime import datetime

# Create a spectra file
time     = [datetime(2013,12,1), datetime(2014,12,2)]
location = [(0,0), (0,1)]
dir_bins = np.arange(7.5, 352.5 + 15, 15)
freq_bins = np.full(30, 0.03453, dtype=np.float) * (1.1 ** np.arange(0, 30))
data = np.ones((len(time), len(location), len(freq_bins), len(dir_bins)))
spec = xr.DataArray(data,
                    coords=[time, [0,1], freq_bins, dir_bins],
                    dims=['time', 'location', 'frequency', 'direction'])
print(spec)

# Put it in an oceanwaves object
ow = oceanwaves.OceanWaves(time=time,
                           time_var='datetime',
                           location=location,
                           crs='epsg:28992',
                           frequency=spec.frequency,
                           direction=spec.direction,
                           energy=spec)
print(ow)
ow.plot(col='location', row='datetime')

screen shot 2018-09-14 at 2 13 52 pm

hoonhout commented 6 years ago

Hi Ray,

The plotting routines are essentially the xarray plotting routines with some extra functions. You should therefore be able to use the subplot_kws argument to change the projection:

ow.plot(subplot_kws=dict(projection='polar'))

The fact that the projection is automatically set in case of a faceted grid plot is a workaround for a bug in older xarray versions. See https://github.com/pydata/xarray/pull/1148.

Bas