Skip to content

Commit a93835b

Browse files
authored
Merge pull request #159 from lukelbd/dataframe-standardize_2d
Fix standardize_2d issue with pandas dataframes
2 parents 3e3f45b + 8c988c0 commit a93835b

File tree

2 files changed

+45
-20
lines changed

2 files changed

+45
-20
lines changed

proplot/axes/base.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,20 +1297,37 @@ def get_tightbbox(self, renderer, *args, **kwargs):
12971297
self._tightbbox = bbox
12981298
return bbox
12991299

1300-
def heatmap(self, *args, **kwargs):
1300+
def heatmap(self, *args, aspect=None, **kwargs):
13011301
"""
13021302
Pass all arguments to `~matplotlib.axes.Axes.pcolormesh` then apply
1303-
settings that are suitable for heatmaps: no gridlines, no minor ticks,
1304-
and major ticks at the center of each grid box.
1303+
settings that are suitable for heatmaps: square grid boxes by default,
1304+
major ticks at the center of each grid box, no minor ticks, and no gridlines.
1305+
1306+
Parameters
1307+
----------
1308+
aspect : {'equal', 'auto'} or float, optional
1309+
Controls the aspect ratio of the axes. The aspect is of particular
1310+
relevance for heatmaps since it may distort the heatmap, i.e. a grid box
1311+
will not be square. This parameter is a shortcut for explicitly calling
1312+
`~matplotlib.axes.set_aspect`.
1313+
1314+
The default is :rc:`image.heatmap`. The options are:
1315+
1316+
- ``'equal'``: Ensures an aspect ratio of 1. Grid boxes will be square.
1317+
- ``'auto'``: The axes is kept fixed and the aspect is adjusted so
1318+
that the data fit in the axes. In general, this will result in non-square
1319+
grid boxes.
13051320
"""
13061321
obj = self.pcolormesh(*args, **kwargs)
1322+
aspect = _not_none(aspect, rc['image.aspect'])
13071323
xlocator, ylocator = None, None
13081324
if hasattr(obj, '_coordinates'):
13091325
coords = obj._coordinates
13101326
coords = (coords[1:, ...] + coords[:-1, ...]) / 2
13111327
coords = (coords[:, 1:, :] + coords[:, :-1, :]) / 2
13121328
xlocator, ylocator = coords[0, :, 0], coords[:, 0, 1]
13131329
self.format(
1330+
aspect=aspect,
13141331
xgrid=False, ygrid=False, xtickminor=False, ytickminor=False,
13151332
xlocator=xlocator, ylocator=ylocator,
13161333
)

proplot/wrappers.py

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -467,25 +467,29 @@ def standardize_2d(self, func, *args, order='C', globe=False, **kwargs):
467467
468468
* If *x* and *y* or *latitude* and *longitude* coordinates were not
469469
provided, and a `~pandas.DataFrame` or `~xarray.DataArray` is passed, we
470-
try to infer them from the metadata. Otherwise,
471-
``np.arange(0, data.shape[0])`` and ``np.arange(0, data.shape[1])``
472-
are used.
470+
try to infer them from the metadata. Otherwise, ``np.arange(0, data.shape[0])``
471+
and ``np.arange(0, data.shape[1])`` are used.
473472
* For ``pcolor`` and ``pcolormesh``, coordinate *edges* are calculated
474473
if *centers* were provided. For all other methods, coordinate *centers*
475474
are calculated if *edges* were provided.
476475
477-
For `~proplot.axes.CartopyAxes` and `~proplot.axes.BasemapAxes`, the
478-
`globe` keyword arg is added, suitable for plotting datasets with global
479-
coverage. Passing ``globe=True`` does the following:
480-
481-
1. "Interpolates" input data to the North and South poles.
482-
2. Makes meridional coverage "circular", i.e. the last longitude coordinate
483-
equals the first longitude coordinate plus 360\N{DEGREE SIGN}.
484-
485-
For `~proplot.axes.BasemapAxes`, 1d longitude vectors are also cycled to
486-
fit within the map edges. For example, if the projection central longitude
487-
is 90\N{DEGREE SIGN}, the data is shifted so that it spans
488-
-90\N{DEGREE SIGN} to 270\N{DEGREE SIGN}.
476+
Parameters
477+
----------
478+
order : {'C', 'F'}, optional
479+
If ``'C'``, arrays should be shaped as ``(y, x)``. If ``'F'``, arrays
480+
should be shaped as ``(x, y)``. Default is ``'C'``.
481+
globe : bool, optional
482+
Whether to ensure global coverage for `~proplot.axes.GeoAxes` plots.
483+
Default is ``False``. When set to ``True`` this does the following:
484+
485+
#. Interpolates input data to the North and South poles by setting the data
486+
values at the poles to the mean from latitudes nearest each pole.
487+
#. Makes meridional coverage "circular", i.e. the last longitude coordinate
488+
equals the first longitude coordinate plus 360\N{DEGREE SIGN}.
489+
#. For `~proplot.axes.BasemapAxes`, 1D longitude vectors are also cycled to
490+
fit within the map edges. For example, if the projection central longitude
491+
is 90\N{DEGREE SIGN}, the data is shifted so that it spans -90\N{DEGREE SIGN}
492+
to 270\N{DEGREE SIGN}.
489493
490494
See also
491495
--------
@@ -532,8 +536,12 @@ def standardize_2d(self, func, *args, order='C', globe=False, **kwargs):
532536
x = Z.coords[Z.dims[idx]]
533537
y = Z.coords[Z.dims[idy]]
534538
else: # DataFrame; never Series or Index because these are 1d
535-
x = Z.index
536-
y = Z.columns
539+
if order == 'C':
540+
x = Z.columns
541+
y = Z.index
542+
else:
543+
x = Z.index
544+
y = Z.columns
537545

538546
# Check coordinates
539547
x, y = _to_array(x), _to_array(y)

0 commit comments

Comments
 (0)