Skip to content

Commit ae81b1d

Browse files
authored
fix datetime plotting (#125)
1 parent 9be3d4d commit ae81b1d

File tree

7 files changed

+13
-9
lines changed

7 files changed

+13
-9
lines changed

xvec/plotting.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import shapely
66
import xarray as xr
77
import xproj # noqa: F401
8+
from xarray.core.formatting import format_item
89
from xarray.plot.utils import _determine_cmap_params, label_from_attrs
910

1011

@@ -122,7 +123,7 @@ def _plot_faceted(arr, axs, row, col, hue, geometry, cmap_params=None, **kwargs)
122123
for i_r, row_val in enumerate(arr[row]):
123124
for i_c, col_val in enumerate(arr[col]):
124125
_plot_single_panel(
125-
arr.sel({row: [row_val.item()], col: [col_val.item()]}).drop_vars(
126+
arr.sel({row: [row_val.values], col: [col_val.values]}).drop_vars(
126127
[col, row]
127128
),
128129
axs[i_r, i_c],
@@ -133,12 +134,13 @@ def _plot_faceted(arr, axs, row, col, hue, geometry, cmap_params=None, **kwargs)
133134
)
134135
if i_r == 0:
135136
axs[0, i_c].set_title(
136-
f"{col} = {arr[col][i_c].astype(str).item()}", fontsize="small"
137+
f"{col} = {format_item(arr[col][i_c].values)}",
138+
fontsize="small",
137139
)
138140
if i_c == len(arr[col]) - 1:
139141
axs[i_r, -1].yaxis.set_label_position("right")
140142
axs[i_r, -1].set_ylabel(
141-
f"{row} = {arr[row][i_r].item()}",
143+
f"{row} = {format_item(arr[row][i_r].values)}",
142144
fontsize="small",
143145
rotation=270,
144146
labelpad=12,
@@ -155,7 +157,7 @@ def _plot_faceted(arr, axs, row, col, hue, geometry, cmap_params=None, **kwargs)
155157
**kwargs,
156158
)
157159
axs_flat[i_c].set_title(
158-
f"{col} = {arr[col][i_c].astype(str).item()}", fontsize="small"
160+
f"{col} = {format_item(arr[col][i_c].values)}", fontsize="small"
159161
)
160162
return arr[col].shape[0] # Return used axes count
161163

Loading
286 Bytes
Loading
Loading
Loading
Loading

xvec/tests/test_plotting.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import geodatasets
22
import geopandas as gpd
3+
import pandas as pd
34
import pytest
45
import xarray as xr
56
import xproj # noqa: F401
@@ -30,6 +31,7 @@ def glaciers():
3031
glaciers_df = gpd.read_file(
3132
"https://github.com/loreabad6/post/raw/refs/heads/main/inst/extdata/svalbard.gpkg"
3233
)
34+
glaciers_df["year"] = pd.to_datetime(glaciers_df["year"].astype(int), format="%Y")
3335
return (
3436
glaciers_df.set_index(["name", "year"])
3537
.to_xarray()
@@ -117,7 +119,7 @@ def test_var_geom(glaciers):
117119
ax0 = ax[0][0]
118120
assert ax0.get_xlabel() == "Easting\n[metre]"
119121
assert ax0.get_ylabel() == "Northing\n[metre]"
120-
assert ax0.get_title() == "year = 1936.0"
122+
assert ax0.get_title() == "year = 1936-01-01"
121123

122124

123125
@image_comparison(
@@ -130,7 +132,7 @@ def test_var_geom_facet(glaciers):
130132
assert ax[2][0].get_xlabel() == "Easting\n[metre]"
131133
assert ax[0][0].get_ylabel() == "Northing\n[metre]"
132134
assert ax[0][0].get_title() == "name = Austre Brøggerbreen"
133-
assert ax[0][-1].get_ylabel() == "year = 1936.0"
135+
assert ax[0][-1].get_ylabel() == "year = 1936-01-01"
134136

135137

136138
@image_comparison(
@@ -143,7 +145,7 @@ def test_var_geom_ds(glaciers):
143145
ax0 = ax[0][0]
144146
assert ax0.get_xlabel() == "Easting\n[metre]"
145147
assert ax0.get_ylabel() == "Northing\n[metre]"
146-
assert ax0.get_title() == "year = 1936.0"
148+
assert ax0.get_title() == "year = 1936-01-01"
147149

148150

149151
@image_comparison(baseline_images=["hue"], extensions=["png"], style=[], tol=0.01)
@@ -154,7 +156,7 @@ def test_hue(glaciers):
154156
ax0 = ax[0][0]
155157
assert ax0.get_xlabel() == "Easting\n[metre]"
156158
assert ax0.get_ylabel() == "Northing\n[metre]"
157-
assert ax0.get_title() == "year = 1936.0"
159+
assert ax0.get_title() == "year = 1936-01-01"
158160

159161

160162
@image_comparison(
@@ -183,7 +185,7 @@ def test_categorical(glaciers):
183185
ax0 = ax[0][0]
184186
assert ax0.get_xlabel() == "Easting\n[metre]"
185187
assert ax0.get_ylabel() == "Northing\n[metre]"
186-
assert ax0.get_title() == "year = 1936.0"
188+
assert ax0.get_title() == "year = 1936-01-01"
187189

188190

189191
@image_comparison(

0 commit comments

Comments
 (0)