Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom plot with projection #86

Closed
fabienpaulot opened this issue Mar 22, 2022 · 3 comments
Closed

Custom plot with projection #86

fabienpaulot opened this issue Mar 22, 2022 · 3 comments

Comments

@fabienpaulot
Copy link

fabienpaulot commented Mar 22, 2022

Hello,
Thanks for developing this package.
I am trying to generate a movie based on a two-panel figure, one of which a map.

I can get it to work when I don't use any geo projection (custom_plotfunc2). However, when I use cartopy, the movie is comprised of empty frames (custom_plotfunc1).
I am not sure what is causing the different outcome. Thanks a lot

import xarray as xr
import cmaps
import matplotlib.pyplot as plt
import numpy as np
import cartopy.crs as ccrs
import os
from multiprocessing import Pool
from xmovie import Movie

def custom_plotfunc1(ds, fig, tt, *args, **kwargs):
# Define station location for timeseries
station = dict(lat=44.45, lon=111.1)
ds_station = ds.sel(**station,method='nearest')

fig = plt.figure()
gs = fig.add_gridspec(5, 3)

ax1 = fig.add_subplot(gs[:, 0:2], projection=ccrs.PlateCarree())
ax1.coastlines(resolution='auto', color='k')

ds.isel(time=tt).plot(ax=ax1, vmin=ds.min(), vmax=ds.max(),
                      cmap=cmaps.WhiteBlueGreenYellowRed,transform=ccrs.PlateCarree(),
                      cbar_kwargs={'orientation':'horizontal','label':'cm/s',
                                  'shrink':0.8,'pad':0.01})
ax1.plot(station['lon'], station['lat'], marker='o', markerfacecolor="None",
     markeredgecolor='black',
         markersize=6,transform=ccrs.PlateCarree())
ax1.set_title('');
ax1.coastlines()

ax2 = fig.add_subplot(gs[2:4, 2])

ds_station.isel(time=slice(0,tt+1)).plot(ax=ax2)
ax2.set_xlim([ds_station['time'].min(),ds_station['time'].max()])

ax2.set_ylim(ds_station.min(), ds_station.max())
ax2.set_title('Data at station');


return None,None

def custom_plotfunc2(ds, fig, tt, *args, **kwargs):

station = dict(lat=44.45, lon=111.1)
ds_station = ds.sel(**station,method='nearest')

(ax1, ax2) = fig.subplots(ncols=2)

ds.isel(time=tt).plot(ax=ax1, vmin=ds.min(), vmax=ds.max(),
                      cmap=cmaps.WhiteBlueGreenYellowRed,
                      cbar_kwargs={'orientation':'horizontal','label':'cm/s',
                                  'shrink':0.8,'pad':0.01})
ax1.plot(station['lon'], station['lat'], marker='o', markerfacecolor="None",
     markeredgecolor='black',
         markersize=6)
ax1.set_title('');

ds_station.isel(time=slice(0,tt+1)).plot(ax=ax2)
ax2.set_xlim([ds_station['time'].min(),ds_station['time'].max()])

ax2.set_ylim(ds_station.min(), ds_station.max())
ax2.set_title('Data at station');

return None, None

ds = xr.open_dataset('/work/f1p/H2/Matteo/ERA5_22.50/daily/GFDL_vd_H2_scale_f.nc').sel(time=slice('2005-01-01','2005-02-01'))
ds['time'] = ds.indexes['time'].to_datetimeindex()

mov_custom = Movie(ds['H2']*100., custom_plotfunc2)
mov_custom.save('/ptmp/Fabien.Paulot/movie_custom.gif', progress=True)

@fabienpaulot
Copy link
Author

Sorry I found the issue, I just need to remove 'fig = plt.figure()"

@jbusecke
Copy link
Owner

Thanks for using xmovie @fabienpaulot. If you can share the results, I would be very curious what comes out of this! @JuliusBusecke on twitter 😁

@fabienpaulot
Copy link
Author

fabienpaulot commented Mar 22, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants