Skip to content

Commit

Permalink
Fix issue with crs arg and set tick methods
Browse files Browse the repository at this point in the history
  • Loading branch information
tomvothecoder committed Feb 7, 2024
1 parent 5e11598 commit ea71520
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 16 deletions.
9 changes: 4 additions & 5 deletions e3sm_diags/plot/cartopy/zonal_mean_2d_plot.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from typing import List, Tuple

import cartopy.crs as ccrs
import matplotlib
import numpy as np
import xarray as xr
Expand Down Expand Up @@ -139,7 +138,7 @@ def _add_colormap(
ax = fig.add_axes(DEFAULT_PANEL_CFG[subplot_num], projection=None)

contour_plot = _add_contour_plot(
ax, parameter, var, lat, plev, ccrs.PlateCarree(), norm, c_levels, color_map
ax, parameter, var, lat, plev, color_map, None, norm, c_levels
)

# Configure the aspect ratio and plot titles.
Expand All @@ -149,7 +148,7 @@ def _add_colormap(

# Configure x and y axis.
# --------------------------------------------------------------------------
_configure_x_and_y_axes(ax, X_TICKS, None, parameter.current_set)
_configure_x_and_y_axes(ax, X_TICKS, None, None, parameter.current_set)
ax.set_xlim(X_LIM)

if parameter.plot_log_plevs:
Expand Down Expand Up @@ -181,7 +180,7 @@ def _add_colormap(
# Add metrics text.
# --------------------------------------------------------------------------
# Min, Mean, Max
fig = _add_min_mean_max_text(fig, subplot_num, DEFAULT_PANEL_CFG, metrics)
_add_min_mean_max_text(fig, subplot_num, DEFAULT_PANEL_CFG, metrics)

if len(metrics) == 5:
fig = _add_rmse_corr_text(fig, subplot_num, DEFAULT_PANEL_CFG, metrics)
_add_rmse_corr_text(fig, subplot_num, DEFAULT_PANEL_CFG, metrics)
41 changes: 30 additions & 11 deletions e3sm_diags/plot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def _add_colormap(
lat_south, lat_north = lat_slice
y_ticks = _get_y_ticks(lat_south, lat_north)

# Get the projection based on region info.
# Get the cartopy projection based on region info.
# --------------------------------------------------------------------------
# TODO: Move lat_lon set specific code to child function
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand All @@ -218,7 +218,7 @@ def _add_colormap(
# defined above.
# TODO: Add comment for why this is done.
contour_plot = _add_contour_plot(
ax, parameter, var, lon, lat, ccrs.PlateCarree(), norm, c_levels, color_map
ax, parameter, var, lon, lat, color_map, ccrs.PlateCarree(), norm, c_levels
)

# Configure the aspect ratio and coast lines.
Expand All @@ -243,7 +243,9 @@ def _add_colormap(
# Configure the titles, x and y axes, and colorbar.
# --------------------------------------------------------------------------
_configure_titles(ax, title)
_configure_x_and_y_axes(ax, x_ticks, y_ticks, parameter.current_set)
_configure_x_and_y_axes(
ax, x_ticks, y_ticks, ccrs.PlateCarree(), parameter.current_set
)
_add_colorbar(fig, subplot_num, panel_configs, contour_plot, c_levels)

# Add metrics text to the figure.
Expand Down Expand Up @@ -331,10 +333,10 @@ def _add_contour_plot(
var: xr.DataArray,
x: xr.DataArray,
y: xr.DataArray,
projection: ccrs.PlateCarree,
color_map: str,
projection: ccrs.PlateCarree | None,
norm: colors.BoundaryNorm | None,
c_levels: List[float] | None,
color_map: str,
) -> mcontour.QuadContourSet:
"""Add the contour plot to the figure axes object.
Expand All @@ -350,8 +352,8 @@ def _add_contour_plot(
The coordinates of the X axis for the plot.
y : xr.DataArray
The coordinates of the Y axis for the plot.
projection : ccrs.PlateCarree
The projection.
projection : ccrs.PlateCarree | None
The optional cartopy projection.
norm : colors.BoundaryNorm | None
The optional norm boundaries.
c_levels : List[float] | None
Expand All @@ -370,10 +372,10 @@ def _add_contour_plot(
x,
y,
var,
cmap=cmap,
transform=projection,
norm=norm,
levels=c_levels,
cmap=cmap,
extend="both",
)

Expand Down Expand Up @@ -513,6 +515,7 @@ def _configure_x_and_y_axes(
ax: matplotlib.axes.Axes,
x_ticks: np.ndarray,
y_ticks: np.ndarray | None,
projection: ccrs.PlateCarree | None,
set_name: str,
):
"""Configure the X and Y axes.
Expand All @@ -526,11 +529,27 @@ def _configure_x_and_y_axes(
y_ticks : np.ndarray | None
The optional array of Y ticks. Some set plotters pass None to configure
the Y axis ticks using other ticks such as Z axis plevs instead.
set_name : set_name
The name of the current set which determines whether the latitude and
longitude major formatters are used.
projection : ccrs.PlateCarree | None
The optional cartopy projection to use for X and Y ticks.
"""
ax.set_xticks(x_ticks, crs=ccrs.PlateCarree())
# For `ax.set_xticks` and `ax.set_yticks`, `crs` cannot be `None` and we
# must split up arguments passed to these methods using a conditional
# statement. Otherwise, this error is raised: `ValueError: Incorrect use of
# keyword argument 'crs'. Keyword arguments other than 'minor' modify the
# text labels and can only be used if 'labels' are passed as well.`
if projection is not None:
ax.set_xticks(x_ticks, crs=projection)

if y_ticks is not None:
ax.set_yticks(y_ticks, crs=projection)
else:
ax.set_xticks(x_ticks)

if y_ticks is not None:
ax.set_yticks(y_ticks, crs=ccrs.PlateCarree())
if y_ticks is not None:
ax.set_yticks(y_ticks)

ax.tick_params(labelsize=8.0, direction="out", width=1)

Expand Down

0 comments on commit ea71520

Please sign in to comment.