Skip to content

coord_trans fails with geom_rect #256

Description

@TyberiusPrime

coord_trans, independent of transform used, does not work
with geom_rect (independent of whether you include a geom_point layer)

Also, unrelated coord_flip (and possibly coord_fixed) should also get x and y trans parameters.

Minimal example:

pdf = pd.DataFrame(
                    {
                        "xmin": [-2,],
                        "xmax": [-1,],
                        "ymin": [0.01,],
                        "ymax": [0.05,],
                    }
                )        
g = ggplot(pdf)
g += geom_rect(
                aes(
                    xmin="xmin",
                    xmax="xmax",
                    ymin="ymin",
                    ymax="ymax",                    
                ),
                fill="lightgrey",
                alpha=0.8,                        
        )
g += coord_trans()
g

->

KeyError                                  Traceback (most recent call last)
~/upstream/dev/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   3077             try:
-> 3078                 return self._engine.get_loc(key)
   3079             except KeyError:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'x'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
~/upstream/dev/lib/python3.6/site-packages/IPython/core/formatters.py in __call__(self, obj)
    700                 type_pprinters=self.type_printers,
    701                 deferred_pprinters=self.deferred_printers)
--> 702             printer.pretty(obj)
    703             printer.flush()
    704             return stream.getvalue()

~/upstream/dev/lib/python3.6/site-packages/IPython/lib/pretty.py in pretty(self, obj)
    400                         if cls is not object \
    401                                 and callable(cls.__dict__.get('__repr__')):
--> 402                             return _repr_pprint(obj, self, cycle)
    403 
    404             return _default_pprint(obj, self, cycle)

~/upstream/dev/lib/python3.6/site-packages/IPython/lib/pretty.py in _repr_pprint(obj, p, cycle)
    695     """A pprint that just redirects to the normal repr function."""
    696     # Find newlines and replace them with p.break_()
--> 697     output = repr(obj)
    698     for idx,output_line in enumerate(output.splitlines()):
    699         if idx:

~/plotnine_dev/scale__trans_input_checking/plotnine/ggplot.py in __repr__(self)
     93         # in the jupyter notebook.
     94         if not self.figure:
---> 95             self.draw()
     96         plt.show()
     97         return '<ggplot: (%d)>' % self.__hash__()

~/plotnine_dev/scale__trans_input_checking/plotnine/ggplot.py in draw(self, return_ggplot)
    186         # new frames knowing that they are separate from the original.
    187         with pd.option_context('mode.chained_assignment', None):
--> 188             return self._draw(return_ggplot)
    189 
    190     def _draw(self, return_ggplot=False):

~/plotnine_dev/scale__trans_input_checking/plotnine/ggplot.py in _draw(self, return_ggplot)
    217             if self.figure is not None:
    218                 plt.close(self.figure)
--> 219             raise err
    220 
    221         if return_ggplot:

~/plotnine_dev/scale__trans_input_checking/plotnine/ggplot.py in _draw(self, return_ggplot)
    206                 self._resize_panels()
    207                 # Drawing
--> 208                 self._draw_layers()
    209                 self._draw_facet_labels()
    210                 self._draw_labels()

~/plotnine_dev/scale__trans_input_checking/plotnine/ggplot.py in _draw_layers(self)
    391         """
    392         # Draw the geoms
--> 393         self.layers.draw(self.layout, self.coordinates)
    394 
    395     def _draw_facet_labels(self):

~/plotnine_dev/scale__trans_input_checking/plotnine/layer.py in draw(self, layout, coord)
     75         for i, l in enumerate(self, start=1):
     76             l.zorder = i
---> 77             l.draw(layout, coord)
     78 
     79     def compute_aesthetics(self, plot):

~/plotnine_dev/scale__trans_input_checking/plotnine/layer.py in draw(self, layout, coord)
    454         # At this point each layer must have the data
    455         # that is created by the plot build process
--> 456         self.geom.draw_layer(self.data, layout, coord, **params)
    457 
    458     def use_defaults(self, data=None):

~/plotnine_dev/scale__trans_input_checking/plotnine/geoms/geom.py in draw_layer(self, data, layout, coord, **params)
    219             panel_params = layout.panel_params[ploc]
    220             ax = layout.axs[ploc]
--> 221             self.draw_panel(pdata, panel_params, coord, ax, **params)
    222 
    223     def draw_panel(self, data, panel_params, coord, ax, **params):

~/plotnine_dev/scale__trans_input_checking/plotnine/geoms/geom_rect.py in draw_panel(self, data, panel_params, coord, ax, **params)
     29         Plot all groups
     30         """
---> 31         self.draw_group(data, panel_params, coord, ax, **params)
     32 
     33     @staticmethod

~/plotnine_dev/scale__trans_input_checking/plotnine/geoms/geom_rect.py in draw_group(data, panel_params, coord, ax, **params)
     33     @staticmethod
     34     def draw_group(data, panel_params, coord, ax, **params):
---> 35         data = coord.transform(data, panel_params, munch=True)
     36         data['size'] *= SIZE_FACTOR
     37         verts = [None] * len(data)

~/plotnine_dev/scale__trans_input_checking/plotnine/coords/coord_trans.py in transform(self, data, panel_params, munch)
     38     def transform(self, data, panel_params, munch=False):
     39         if not self.is_linear and munch:
---> 40             data = self.munch(data, panel_params)
     41 
     42         def trans_x(data):

~/plotnine_dev/scale__trans_input_checking/plotnine/coords/coord.py in munch(self, data, panel_params)
    149         ranges = self.range(panel_params)
    150 
--> 151         data.loc[data['x'] == -np.inf, 'x'] = ranges.x[0]
    152         data.loc[data['x'] == np.inf, 'x'] = ranges.x[1]
    153         data.loc[data['y'] == -np.inf, 'y'] = ranges.y[0]

~/upstream/dev/lib/python3.6/site-packages/pandas/core/frame.py in __getitem__(self, key)
   2686             return self._getitem_multilevel(key)
   2687         else:
-> 2688             return self._getitem_column(key)
   2689 
   2690     def _getitem_column(self, key):

~/upstream/dev/lib/python3.6/site-packages/pandas/core/frame.py in _getitem_column(self, key)
   2693         # get column
   2694         if self.columns.is_unique:
-> 2695             return self._get_item_cache(key)
   2696 
   2697         # duplicate columns & possible reduce dimensionality

~/upstream/dev/lib/python3.6/site-packages/pandas/core/generic.py in _get_item_cache(self, item)
   2487         res = cache.get(item)
   2488         if res is None:
-> 2489             values = self._data.get(item)
   2490             res = self._box_item_values(item, values)
   2491             cache[item] = res

~/upstream/dev/lib/python3.6/site-packages/pandas/core/internals.py in get(self, item, fastpath)
   4113 
   4114             if not isna(item):
-> 4115                 loc = self.items.get_loc(item)
   4116             else:
   4117                 indexer = np.arange(len(self.items))[isna(self.items)]

~/upstream/dev/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   3078                 return self._engine.get_loc(key)
   3079             except KeyError:
-> 3080                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   3081 
   3082         indexer = self.get_indexer([key], method=method, tolerance=tolerance)

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'x'

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions