-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathxarray_matplotlib.py
66 lines (44 loc) · 1.54 KB
/
xarray_matplotlib.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python
"""
matplotlib with datetime64 testing
"""
import xarray
from datetime import datetime
from matplotlib.pyplot import figure, show
import matplotlib.dates as mdates
import numpy as np
def test_plot2d_datetime():
t = np.arange("2010-05-04T12:05:00", "2010-05-04T12:05:01", dtype="datetime64[ms]")
y = np.random.randn(t.size)
# t = t.astype(datetime) # Matplotlib < 2.2
ax = figure().gca()
ax.plot(t, y)
def test_plot2d_xarray():
t = np.arange("2010-05-04T12:05:00", "2010-05-04T12:05:01", dtype="datetime64[ms]")
y = np.random.randn(t.size)
dat = xarray.DataArray(y, coords={"time": t}, dims=["time"])
dset = xarray.Dataset({"random1Dstuff": dat})
fg = figure()
ax = fg.subplots(3, 1, sharex=True)
dat.plot(ax=ax[0])
ax[1].plot(dat.time, dat)
dset["random1Dstuff"].plot(ax=ax[2])
def test_imshow_datetime():
"""
keogram using matplotlib imshow
"""
Ny = 500 # arbitrary
t = np.arange("2010-05-04T12:05", "2010-05-04T12:06", dtype="datetime64[s]").astype(datetime)
im = np.random.random((Ny, t.size))
y = range(t.size) # arbitrary
mt = mdates.date2num((t[0], t[-1])) # at least through Matplotlib 2.2
fig = figure()
ax = fig.gca()
ax.imshow(im, extent=[mt[0], mt[1], y[0], y[-1]], aspect="auto")
# %% datetime formatting
ax.xaxis_date() # like "num2date"
# ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M:%S'))
fig.autofmt_xdate()
if __name__ == "__main__":
np.testing.run_module_suite()
show()