AusClimateService / plotting_maps

Standardising hazard maps for ACS
4 stars 1 forks source link

Plotting error with contourf=True #10

Closed xenct closed 2 months ago

xenct commented 2 months ago

In plot_acs_hazard default behaviour, the map produced is not as expected. The error does not occur with all data. The white area is transparent/NaN The remaining coloured area is not aligned correctly

For example:

from acs_plotting_maps import plot_acs_hazard, regions_dict, cmap_dict, tick_dict
import xarray as xr
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import cartopy.crs as ccrs

ds = xr.open_dataset('/g/data/ia39/ncra/heat/data/HWF/bias-corrected/ensemble/GWL-change/HWF_AGCD-05i_MME50_ssp370_v1-r1-ACS-QME-AGCD-1960-2022_GWL20-GWL12-change.nc')

plot_acs_hazard(data = ds['HWF'],
                        cmap = cmap_dict["anom_coolwarm"],
                        ticks = np.arange(-100,101,20),
                        regions = regions_dict['ncra_regions'],
                        cbar_label = "units",
                        cbar_extend = "both",
                        title = f"Change in ",
                        dataset_name = "INFO",
                        date_range = ds.GWL.replace('degC',u'\N{DEGREE SIGN}C'),
                        watermark=None,
                        outfile = 'temp.png',
                        contour = False,
                        contourf=True,
                        );

expected output:

# turn off contourf and use pcolormesh instead
contourf=False
xenct commented 2 months ago

After trying to make contourf work for this data (modifying levels, norm, transform, data, etc) I found a similar issue https://github.com/SciTools/cartopy/issues/1076. I believe it is a similar issue due to this comment https://github.com/SciTools/cartopy/issues/1076#issuecomment-552407421

... The bug appears very often (more than 50% of the cases) when a contourf is crossing the 0 value, ... ... This makes it impossible to reliably produce filled contours when looking at the poles...

Our data here also crosses zero and the projection has a singularity at the south pole.

I believe that there may not be a satisfactory fix available. The best course of action may be to change the default function to contourf=False or remove the contourf functionality altogether.

xenct commented 2 months ago

From further testing, I am confident it is a problem with combining this anomaly data, contourf and the projections.

Stripping the plotting down to the minimum for projecting on crs = ccrs.LambertConformal(...) , plotting works ok with da.plot() and da.plot.contour(), but performs strangely on da.plot.contourf()

import xarray as xr
import cartopy.crs as ccrs
import matplotlib.pyplot as plt

ds = xr.open_dataset('/g/data/ia39/ncra/heat/data/HWF/bias-corrected/ensemble/GWL-change/HWF_AGCD-05i_MME50_ssp370_v1-r1-ACS-QME-AGCD-1960-2022_GWL20-GWL12-change.nc')

crs = ccrs.LambertConformal(
    central_latitude=-24.75,
    central_longitude=134.0,
    cutoff=30,
    standard_parallels=(-10, -40),
)

plt.figure()
ds['HWF'].plot(transform=ccrs.PlateCarree(), subplot_kws=dict(projection=crs),)
plt.figure()
ds['HWF'].plot.contour(transform=ccrs.PlateCarree(), subplot_kws=dict(projection=crs),)
plt.figure()
ds['HWF'].plot.contourf(transform=ccrs.PlateCarree(), subplot_kws=dict(projection=crs),)

image

The strange behaviour also depends on the input data. eg the same code works fine for

ds = xr.open_dataset('/g/data/ia39/ncra/heat/data/HWF/bias-corrected/ensemble/GWL-average/HWF_AGCD-05i_MME50_ssp370_v1-r1-ACS-QME-AGCD-1960-2022_GWL20.nc')

image (1)