Commit d67f6793 authored by Clément Haëck's avatar Clément Haëck
Browse files

Up plots

parent b177132a
#!/usr/bin/env ipython
from os import path
import numpy as np
import matplotlib.pyplot as plt
import xarray as xr
import lib
import lib.data.hists_2d as lh
import Plots.util as plot_util
fixes = dict(
Y=2007
)
zone = 'GS3_S'
ds = lh.get_data(fixes=fixes)
ds = ds.squeeze()
grp = ds.groupby('time.month')
ds = xr.combine_by_coords([grp[i] for i in [1, 2, 12]])
hist = ds.hist.sum('time')
hist.attrs['VAR'] = 'CHL'
pdf = lib.data.hists.get_pdf(hist)
pdf.attrs['VAR'] = 'CHL'
med = lib.data.hists.get_median(pdf)
fig, axes = plt.subplots(1, 3, figsize=(10, 3.8), sharex=True, sharey=True)
fig.subplots_adjust(left=0.07, bottom=0.13, right=0.94, top=0.92, wspace=0.08)
cmin = 5e-2
cmax = pdf.max().values
for ax, z, t in zip(axes, 'SIN', ['South', 'Jet', 'North']):
zone = 'GS3_{}'.format(z)
pz = pdf.sel(zone=zone)
addcbar = ax == axes[-1]
cax = None
if addcbar:
cax = plot_util.add_cax(ax)
pz.plot.pcolormesh(
ax=ax, cmap='Reds',
norm=plt.matplotlib.colors.LogNorm(cmin, cmax),
add_labels=False,
add_colorbar=addcbar,
cbar_ax=cax)
# pdf.plot.contour(ax=ax, add_labels=False,
# colors='k', levels=np.geomspace(cmin, cmax, 10))
ax.plot(med.bins_HI, med.sel(zone=zone), color='k')
ax.set_title(t)
ax.set_xlabel('HI ')
ax.set_xlim(0, 19)
ax.set_ylim(0, 1.8)
axes[0].set_ylabel('CHL')
cax.set_ylabel('PDF (normalisée selon Chl)')
fig.savefig(path.join(lib.root_plot, 'Hists', '2D', 'fig.png'),
dpi=150)
......@@ -21,34 +21,24 @@ fixes = dict(
scale=30.,
number=2,
coef=0,
zone=r'GS3_.',
thr_lo=6.,
thr_lo=5.,
thr_hi=10.
)
fixes.update(args['fixes'])
args['fixes'] = fixes
def get_data(var):
args['fixes']['var'] = var
ds = lh.get_data(args)
ds = ds.squeeze(['scale', 'number', 'coef', 'thr_lo', 'thr_hi'])
ds = lh.get_data(args)
ds = ds.squeeze()
ds = lh.resample_8D_multiple_years(ds)
ds = ds.load()
ds = lh.resample_8D_multiple_years(ds)
ds = ds.load()
ds['N'] = lh.get_N(ds)
ds['frac'] = 100. * ds.N / ds.N.sum('mask')
ds['pdf'] = lh.get_pdf(ds).load()
ds['med'] = lh.get_median(ds).load()
ds['med'] = ds.med.where(ds.N.sel(mask='mid') > 10)
ds = ds.drop_vars(['hist', 'N', 'pdf'])
ds = ds.groupby("time.dayofyear").mean()
ds = lh.restore_time(ds)
return ds
N = lh.get_N(ds).sel(var='CHL')
frac = (N / N.sum('mask')) * 100.
ds['frac'] = frac.reset_coords(drop=True)
pdf = lh.get_pdf(ds)
ds['med'] = lh.get_median(pdf).load()
def loop_zones():
......@@ -78,20 +68,16 @@ for ax in axes.flat:
ax.xaxis.set_major_locator(locator)
ax.xaxis.set_major_formatter(md.ConciseDateFormatter(locator))
for i, var in enumerate(['sst', 'CHL']): # CHL Second
ds = get_data(var)
for j, zone in enumerate(loop_zones()):
sel = dict(zone=zone)
for j, zone in enumerate(loop_zones()):
sel = dict(zone=zone)
for i, var in enumerate(['sst', 'CHL']):
sel['var'] = var
ax = axes[i, j]
plot(ax, ds.med, sel, cset.cyan, 'low')
plot(ax, ds.med, sel, cset.orange, 'mid')
plot(ax, ds.med, sel, cset.red, 'hi')
# We use clis from CHL from then on
for j, zone in enumerate(loop_zones()):
sel = dict(zone=zone)
sel.pop('var')
ax = axes[2, j]
plot(ax, ds.frac, sel, color=cset.orange, mask='mid')
plot(ax, ds.frac, sel, color=cset.red, mask='hi')
......@@ -116,9 +102,9 @@ handles = [plt.Line2D((), (), ls='-', color=cset.cyan),
fig.legend(handles, labels, bbox_to_anchor=[0., 1.], loc='upper left',
framealpha=1.0, ncol=3)
fig.canvas.draw()
fig.savefig(path.join(lib.root_plot, 'Hists', 'median_GS3_2thr',
'fig_thr_{:.2f}_{:.2f}.png'
.format(*[args['fixes']['thr_'+h]
for h in ['lo', 'hi']])),
dpi=150)
# fig.canvas.draw()
# fig.savefig(path.join(lib.root_plot, 'Hists', 'median_GS3_2thr',
# 'fig_thr_{:.2f}_{:.2f}.png'
# .format(*[args['fixes']['thr_'+h]
# for h in ['lo', 'hi']])),
# dpi=150)
......@@ -6,75 +6,54 @@ import matplotlib.pyplot as plt
import tol_colors as tc
import lib
import lib.data.hists
import lib.data.hists as lh
args = dict(region='GS', days=1)
args = lib.get_args(['region', 'days', 'fixes'])
fixes = dict(
scale=30.,
number=2,
coef=0,
zone='GS3_N',
var='CHL'
)
fixes.update(args['fixes'])
args['fixes'] = fixes
threshold = lib.data.hists.THRESHOLD_GS3[fixes['zone'][-1]]
ds = lib.data.hists.get_data(args, fixes=fixes)
ds = lh.get_data(args)
ds = ds.squeeze()
ds = lib.data.hists.resample_8D_multiple_years(ds)
ds.load()
lib.data.hists.normalize_hist(ds)
clis = lib.data.hists.get_climatology(ds)
clis = [c.groupby('time.season').sum() for c in clis]
ds = ds.groupby('time.season').sum(keep_attrs=True)
ds.load()
pdf = lh.get_pdf(ds)
cset = tc.tol_cset('vibrant')
TITLE = dict(S='South', I='Jet', N='North')
VMIN = dict(CHL=dict(S=0., I=0., N=0.),
sst=dict(S=10, I=15, N=-5))
VMAX = dict(CHL=dict(S=1., I=1., N=3.),
sst=dict(S=32, I=32, N=27))
def plot(cli, cli_d1, cli_d9, sel=None, color='k'):
def get(ds):
return ds.hist.sel(**sel)
ax.plot(cli.bins, get(cli), color=color)
ax.fill_between(cli.bins, get(cli_d1), get(cli_d9),
color=color, alpha=0.4)
fig, axes = plt.subplots(2, 2, figsize=(8, 8), sharex=True)
fig.subplots_adjust(left=0.05, bottom=0.08, right=0.98, top=0.92,
wspace=0.1, hspace=0.1)
fig.suptitle('Zone {}'.format(TITLE[fixes['zone'][-1]]))
for ax, season in zip(axes.T.flat, clis[0].season.values):
clis_s = [c.sel(season=season) for c in clis]
h_max = clis_s[0].hist.max()*1.1
for ds in clis_s:
ds['hist'] /= h_max
ax.set_title(season)
plot(*clis_s, dict(mask='bkg'), color=cset.teal)
plot(*clis_s, dict(mask='frt'), color=cset.orange)
# vmin = clis_s[0].bins[0]
# vmax = clis_s[0].bins[-1]
vmin = VMIN[fixes['var']][fixes['zone'][-1]]
vmax = VMAX[fixes['var']][fixes['zone'][-1]]
ax.set_xlim(vmin, vmax)
for ax in axes[1, :]:
ax.set_xlabel(fixes['var'].upper())
fig.canvas.draw()
fig.savefig(path.join(lib.root_plot, 'Hists', 'hists_season',
'{}_{}.png'.format(fixes['zone'], fixes['var'])),
dpi=200)
for var in pdf.VARS:
fig, axes = plt.subplots(3, 4, figsize=(10, 8), sharex=True)
fig.subplots_adjust(left=0.05, bottom=0.05, right=0.98, top=0.94,
wspace=0.1, hspace=0.1)
fig.suptitle(var)
for i, (z, z_title) in enumerate(zip('NIS', ['North', 'Jet', 'South'])):
axes[i, 0].set_ylabel(z_title)
zone = 'GS3_{}'.format(z)
for j, season in enumerate(['DJF', 'MAM', 'JJA', 'SON']):
axes[0, j].set_title(season)
p = pdf[lh.var_name('pdf', var)].sel(zone=zone, season=season)
for mask, color in zip(['low', 'mid', 'hi'],
['cyan', 'orange', 'red']):
axes[i, j].plot(p[lh.var_name('bins', var)],
p.sel(mask=mask), color=getattr(cset, color))
for ax in axes.flat:
ax.tick_params(axis='y', labelleft=False)
if var == 'CHL':
axes[0, 0].set_xlim(0, 1.1)
elif var == 'sst':
axes[0, 0].set_xlim(-4, 32)
fig.canvas.draw()
fig.savefig(path.join(lib.root_plot, 'Hists', 'hists_season',
'fig_{}.png'.format(var)),
dpi=200)
#!/usr/bin/env ipython
from os import path
import matplotlib.pyplot as plt
import tol_colors as tc
import lib
import lib.data.hists
args = dict(region='GS', days=1)
fixes = dict(
scale=30.,
number=2,
coef=0,
zone='GS3_N',
var='CHL'
)
threshold = lib.data.hists.THRESHOLD_GS3[fixes['zone'][-1]]
ds = lib.data.hists.get_data(args, fixes=fixes)
ds = ds.squeeze()
ds = lib.data.hists.resample_8D_multiple_years(ds)
ds.load()
lib.data.hists.normalize_hist(ds)
clis = lib.data.hists.get_climatology(ds)
clis = [c.groupby('time.season').sum() for c in clis]
cset = tc.tol_cset('vibrant')
TITLE = dict(S='South', I='Jet', N='North')
VMIN = dict(CHL=dict(S=0., I=0., N=0.),
sst=dict(S=10, I=15, N=-5))
VMAX = dict(CHL=dict(S=1., I=1., N=3.),
sst=dict(S=32, I=32, N=27))
def plot(cli, cli_d1, cli_d9, sel=None, color='k'):
def get(ds):
return ds.hist.sel(**sel)
ax.plot(cli.bins, get(cli), color=color)
ax.fill_between(cli.bins, get(cli_d1), get(cli_d9),
color=color, alpha=0.4)
fig, axes = plt.subplots(2, 2, figsize=(8, 8), sharex=True)
fig.subplots_adjust(left=0.05, bottom=0.08, right=0.98, top=0.92,
wspace=0.1, hspace=0.1)
fig.suptitle('Zone {}'.format(TITLE[fixes['zone'][-1]]))
for ax, season in zip(axes.T.flat, clis[0].season.values):
clis_s = [c.sel(season=season) for c in clis]
h_max = clis_s[0].hist.max()*1.1
for ds in clis_s:
ds['hist'] /= h_max
ax.set_title(season)
plot(*clis_s, dict(mask='bkg'), color=cset.teal)
plot(*clis_s, dict(mask='frt'), color=cset.orange)
# vmin = clis_s[0].bins[0]
# vmax = clis_s[0].bins[-1]
vmin = VMIN[fixes['var']][fixes['zone'][-1]]
vmax = VMAX[fixes['var']][fixes['zone'][-1]]
ax.set_xlim(vmin, vmax)
for ax in axes[1, :]:
ax.set_xlabel(fixes['var'].upper())
fig.canvas.draw()
fig.savefig(path.join(lib.root_plot, 'Hists', 'hists_season',
'{}_{}.png'.format(fixes['zone'], fixes['var'])),
dpi=200)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment