Skip to content

Commit 3a9f83c

Browse files
committed
Fix issue with contours of 2d dataframes
1 parent bf6ed2c commit 3a9f83c

File tree

2 files changed

+33
-41
lines changed

2 files changed

+33
-41
lines changed

docs/2dplots.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -272,19 +272,20 @@
272272

273273
# DataArray
274274
state = np.random.RandomState(51423)
275-
data = 50 * (
276-
np.sin(np.linspace(0, 2 * np.pi, 20) + 0) ** 2
277-
* np.cos(np.linspace(0, np.pi, 20) + np.pi / 2)[:, None] ** 2
275+
linspace = np.linspace(0, np.pi, 20)
276+
data = 50 * state.normal(1, 0.2, size=(20, 20)) * (
277+
np.sin(linspace * 2) ** 2
278+
* np.cos(linspace + np.pi / 2)[:, None] ** 2
278279
)
279280
lat = xr.DataArray(
280281
np.linspace(-90, 90, 20),
281282
dims=('lat',),
282-
attrs={'units': 'degN'}
283+
attrs={'units': 'deg_north'}
283284
)
284285
plev = xr.DataArray(
285286
np.linspace(1000, 0, 20),
286287
dims=('plev',),
287-
attrs={'long_name': 'pressure', 'units': 'hPa'}
288+
attrs={'long_name': 'pressure', 'units': 'mb'}
288289
)
289290
da = xr.DataArray(
290291
data,
@@ -295,14 +296,14 @@
295296
)
296297

297298
# DataFrame
298-
data = state.rand(20, 20)
299+
data = state.rand(12, 20)
299300
df = pd.DataFrame(
300-
data.cumsum(axis=0).cumsum(axis=1),
301-
index=[*'JFMAMJJASONDJFMAMJJA']
301+
(data - 0.4).cumsum(axis=0).cumsum(axis=1),
302+
index=list('JFMAMJJASOND'),
302303
)
303304
df.name = 'temporal data'
304-
df.index.name = 'index'
305-
df.columns.name = 'time (days)'
305+
df.index.name = 'month'
306+
df.columns.name = 'variable (units)'
306307

307308
# %%
308309
import proplot as plot
@@ -311,13 +312,13 @@
311312

312313
# Plot DataArray
313314
axs[0].contourf(
314-
da, cmap='RdPu', cmap_kw={'left': 0.05}, colorbar='l', lw=0.7, color='gray7'
315+
da, cmap='RdPu', cmap_kw={'left': 0.05}, colorbar='l', lw=0.7, color='k'
315316
)
316317
axs[0].format(yreverse=True)
317318

318319
# Plot DataFrame
319320
axs[1].contourf(
320-
df, cmap='YlOrRd', colorbar='r', linewidth=0.7, color='gray7'
321+
df, cmap='YlOrRd', colorbar='r', linewidth=0.7, color='k'
321322
)
322323
axs[1].format(xtickminor=False)
323324

proplot/axes/plot.py

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ def _is_string(data):
251251
return len(data) and isinstance(_to_ndarray(data).flat[0], str)
252252

253253

254-
def _to_array(data):
254+
def _to_arraylike(data):
255255
"""
256256
Convert list of lists to array-like type.
257257
"""
@@ -434,7 +434,7 @@ def standardize_1d(self, func, *args, **kwargs):
434434
ys, args = (y, args[0]), args[1:]
435435
else:
436436
ys = (y,)
437-
ys = [_to_array(y) for y in ys]
437+
ys = [_to_arraylike(y) for y in ys]
438438

439439
# Auto x coords
440440
y = ys[0] # test the first y input
@@ -444,7 +444,7 @@ def standardize_1d(self, func, *args, **kwargs):
444444
or any(kwargs.get(s, None) for s in ('means', 'medians'))
445445
)
446446
x, _ = _axis_labels_title(y, axis=axis)
447-
x = _to_array(x)
447+
x = _to_arraylike(x)
448448
if x.ndim != 1:
449449
raise ValueError(
450450
f'x coordinates must be 1-dimensional, but got {x.ndim}.'
@@ -637,7 +637,7 @@ def standardize_2d(self, func, *args, order='C', globe=False, **kwargs):
637637
# Ensure DataArray, DataFrame or ndarray
638638
Zs = []
639639
for Z in args:
640-
Z = _to_array(Z)
640+
Z = _to_arraylike(Z)
641641
if Z.ndim != 2:
642642
raise ValueError(f'Z must be 2-dimensional, got shape {Z.shape}.')
643643
Zs.append(Z)
@@ -649,10 +649,12 @@ def standardize_2d(self, func, *args, order='C', globe=False, **kwargs):
649649
# Retrieve coordinates
650650
if x is None and y is None:
651651
Z = Zs[0]
652-
if order == 'C': # TODO: check order stuff works
652+
if order == 'C':
653653
idx, idy = 1, 0
654654
else:
655655
idx, idy = 0, 1
656+
# x = np.arange(Z.shape[idx])
657+
# y = np.arange(Z.shape[idy])
656658
if isinstance(Z, ndarray):
657659
x = np.arange(Z.shape[idx])
658660
y = np.arange(Z.shape[idy])
@@ -667,8 +669,19 @@ def standardize_2d(self, func, *args, order='C', globe=False, **kwargs):
667669
x = Z.index
668670
y = Z.columns
669671

672+
# Optionally re-order
673+
# TODO: Double check this
674+
if order == 'F':
675+
x, y = x.T, y.T # in case they are 2-dimensional
676+
Zs = tuple(Z.T for Z in Zs)
677+
elif order != 'C':
678+
raise ValueError(
679+
f'Invalid order {order!r}. Choose from '
680+
'"C" (row-major, default) and "F" (column-major).'
681+
)
682+
670683
# Check coordinates
671-
x, y = _to_array(x), _to_array(y)
684+
x, y = _to_arraylike(x), _to_arraylike(y)
672685
if x.ndim != y.ndim:
673686
raise ValueError(
674687
f'x coordinates are {x.ndim}-dimensional, '
@@ -691,7 +704,7 @@ def standardize_2d(self, func, *args, order='C', globe=False, **kwargs):
691704
kw['xlocator'] = mticker.FixedLocator(xi)
692705
kw['xformatter'] = mticker.IndexFormatter(x)
693706
kw['xminorlocator'] = mticker.NullLocator()
694-
if _is_string(x):
707+
if _is_string(y):
695708
yi = np.arange(len(y))
696709
kw['ylocator'] = mticker.FixedLocator(yi)
697710
kw['yformatter'] = mticker.IndexFormatter(y)
@@ -750,17 +763,6 @@ def standardize_2d(self, func, *args, order='C', globe=False, **kwargs):
750763
f'Z borders {tuple(i+1 for i in Z.shape)}.'
751764
)
752765

753-
# Optionally re-order
754-
# TODO: Double check this
755-
if order == 'F':
756-
x, y = x.T, y.T # in case they are 2-dimensional
757-
Zs = (Z.T for Z in Zs)
758-
elif order != 'C':
759-
raise ValueError(
760-
f'Invalid order {order!r}. Choose from '
761-
'"C" (row-major, default) and "F" (column-major).'
762-
)
763-
764766
# Enforce centers
765767
else:
766768
# Get centers given edges. If 2d, don't raise error, let matplotlib
@@ -793,17 +795,6 @@ def standardize_2d(self, func, *args, order='C', globe=False, **kwargs):
793795
f'or Z borders {tuple(i+1 for i in Z.shape)}.'
794796
)
795797

796-
# Optionally re-order
797-
# TODO: Double check this
798-
if order == 'F':
799-
x, y = x.T, y.T # in case they are 2-dimensional
800-
Zs = (Z.T for Z in Zs)
801-
elif order != 'C':
802-
raise ValueError(
803-
f'Invalid order {order!r}. Choose from '
804-
'"C" (row-major, default) and "F" (column-major).'
805-
)
806-
807798
# Cartopy projection axes
808799
if (
809800
getattr(self, 'name', '') == 'cartopy'

0 commit comments

Comments
 (0)