cowpatch.layout_elements

Module Contents

Classes

layout

layout class to store information about arangement of patches found

area

object that stores information about what area a patch will fill

class cowpatch.layout_elements.layout(ncol=None, nrow=None, byrow=None, rel_widths=None, rel_heights=None, design=None)[source]

layout class to store information about arangement of patches found in cow.patch.

Parameters
  • ncol (integer) – Integer for the number of columns to arrange the the patches in. The default is None (which avoids conflicts if a value for design is provided). If ncol is None but nrow is not, then ncol will default to the minimum number of columns to make sure that all patches can be visualized.

  • nrow (integer) – Integer for the number of rows to arrange the the patches in. The default is None (which avoids conflicts if a value for design is provided). If nrow is None but ncol is not, then nrow will default to the minimum number of rows to make sure that all patches can be visualized.

  • byrow (boolean) – If ncol and/or nrow is included, then this boolean indicates if the patches should be ordered by row (default if byrow is None or when parameter is True) or by column (if byrow was False).

  • design (np.array (float based) or str) – Specification of the location of each patch in the arrangement. Can either be a float numpy array with integers between 0 and the number of patches to arrange, or a text string that captures similar ideas to the array approach but uses capital alphabetical characters (A-Z) to indicate each figure. More information is in Notes.

  • rel_widths (list, np vector or tuple) – Numerical vector of relative columns widths. This not required, the default would be np.ones(ncol) or np.ones(design.shape[0]). Note that this is a relative sizing and the values are only required to be non-negative, non-zero values, for example [1,2] would would make the first column twice as wide as the second column.

  • rel_heights (list or tuple) – Numerical vector of relative row heights. This not required, the default would be np.ones(nrow) or np.ones(design.shape[1]). Note that this is a relative sizing and the values are only required to be non-negative, non-zero values, for example [1,2] would would make the first row twice as tall as the second row.

Notes

Design

The design parameter expects specific input.

1. If the design is input as a numpy array, we expect it to have integers only (0 to # patches-1). It is allowed to have np.nan values if certain “squares” of the layout are not covered by others (the covering is defined by the value ordering). Note that we won’t check for overlap and np.nan is not enforced if another patches’ relative (min-x,min-y) and (max-x, max-y) define a box over that np.nan’s area.

An example of a design of the numpy array form could look like

>>> my_np_design = np.array([[1,1,2],
...                          [3,3,2],
...                          [3,3,np.nan]])

2. if the design parameter takes in a string, we expect it to have a structure such that each line (pre \\n) contains the same number of characters, and these characters must come from the first (number of patches) capital alphabetical characters or the \# or . sign to indicate an empty square. Similar arguments w.r.t. overlap and the lack of real enforcement for empty squares applies (as in 1.).

An example of a design of the string form could look like

>>> my_str_design = """
... AAB
... CCB
... CC\#
... """

or

>>> my_str_design = """
... AAB
... CCB
... CC.
... """

See the Layout guide for more detailed examples of functionality.

Similarities to our `R` cousins:

This layout function is similar to patchwork::plot_layout (with a special node to design parameter) and helps perform similar ideas to gridExtra::arrangeGrob’s layout_matrix parameter, and cowplot::plot_grid’s rel_widths and rel_heights parameters.

Examples

>>> # Necessary libraries for example
>>> import numpy as np
>>> import cowpatch as cow
>>> import plotnine as p9
>>> import plotnine.data as p9_data
>>> g0 = p9.ggplot(p9_data.mpg) +\
...     p9.geom_bar(p9.aes(x="hwy")) +\
...     p9.labs(title = 'Plot 0')
>>> g1 = p9.ggplot(p9_data.mpg) +\
...     p9.geom_point(p9.aes(x="hwy", y = "displ")) +\
...     p9.labs(title = 'Plot 1')
>>> g2 = p9.ggplot(p9_data.mpg) +\
...     p9.geom_point(p9.aes(x="hwy", y = "displ", color="class")) +\
...     p9.labs(title = 'Plot 2')
>>> g3 = p9.ggplot(p9_data.mpg[p9_data.mpg["class"].isin(["compact",
...                                                      "suv",
...                                                      "pickup"])]) +\
...     p9.geom_histogram(p9.aes(x="hwy"),bins=10) +\
...     p9.facet_wrap("class")
>>> # design matrix
>>> vis_obj = cow.patch(g1,g2,g3)
>>> vis_obj += cow.layout(design = np.array([[0,1],
...                                          [2,2]]))
>>> vis_obj.show()
>>> # design string
>>> vis_obj2 = cow.patch(g1,g2,g3)
>>> vis_obj2 += cow.layout(design = """
...                                 AB
...                                 CC
...                                 """)
>>> vis_obj2.show()
>>> # nrow, ncol, byrow
>>> vis_obj3 = cow.patch(g0,g1,g2,g3)
>>> vis_obj3 += cow.layout(nrow=2, byrow=False)
>>> vis_obj3.show()
>>> # rel_widths/heights
>>> vis_obj = cow.patch(g1,g2,g3)
>>> vis_obj += cow.layout(design = np.array([[0,1],
...                                          [2,2]]),
...                       rel_widths = np.array([1,2]))
>>> vis_obj.show()

See also

area

object class that helps layout define where plots will go in the arangement

patch

fundamental object class which is combined with layout to defin the overall arangement of plots

design

defines underlying design attribute (potentially defined relative to a cow.patch object if certain structure are not extremely specific.

class cowpatch.layout_elements.area(x_left, y_top, width, height, _type)[source]

object that stores information about what area a patch will fill

Parameters
  • x_left (float) – scalar of where the left-most point of the patch is located (impacted by the _type parameter)

  • y_top (float) – scalar of where the top-most point of the patch is located (impacted by the _type parameter)

  • width (float) – scalar of the width of the patch (impacted by the _type parameter)

  • height (float) – scalar of the height of the patch (impacted by the _type parameter)

  • _type (str {"design", "relative", "pt"}) – describes how the parameters are stored. See Notes for more information between the options.

Notes

These objects provide structural information about where in the overall arangement individual plots / sub arangments lie.

The _type parameter informs how to understand the other parameters:

1. “design” means that the values are w.r.t. to a design matrix relative to the layout class, and values are relative to the rows and columns units.

2. “relative” means the values are defined relative to the full size of the canvas and taking values between 0-1 (inclusive).

  1. “pt” means that values are defined relative to point values

See also

layout

object that incorporates multiple area definitions to define layouts.

pt(width_pt=None, height_pt=None, rel_widths=None, rel_heights=None)[source]

Translates area object to _type = “pt”

Parameters
  • width_pt (float) – width in points (required if _type is not “pt”)

  • height_pt (float) – height in points (required if _type is not “pt”)

  • rel_widths (np.array (vector)) – list of relative widths of each column of the layout matrix (required if _type is “design”)

  • rel_heights (np.array (vector)) – list of relative heights of each row of the layout matrix (required if _type is “design”)

Returns

area object of _type = “pt”

Return type

area object