Facets and Strips¶
Extended Facets¶
The extended facets add selected ggh4x-style behaviours on top of plotnine’s facet system. Exact layout parity is limited by the extension points available in plotnine.
facet_grid2¶
- class plotnine_extra.facet_grid2(rows=None, cols=None, *, margins=False, scales='fixed', space='fixed', shrink=True, labeller='label_value', as_table=True, drop=True, independent='none', axes='margins', remove_labels='none')[source]¶
Bases:
plotnine.facet_gridExtended grid facets with independent scales.
Extends
plotnine.facet_gridwith support for truly independent position scales, configurable inner axes, and selective label removal.- Parameters:
rows (str or list of str, optional) – Variables to facet by in rows.
cols (str or list of str, optional) – Variables to facet by in columns.
margins (bool or list of str) – Whether to display marginal facets.
scales (str) – Whether scales are fixed (“fixed”), free (“free”, “free_x”, “free_y”).
space (str or dict) – Panel spacing: “fixed”, “free”, “free_x”, “free_y” or a dict of ratios.
shrink (bool) – Whether to shrink scales to fit output of statistics.
labeller (str) – Labelling function for strip text.
as_table (bool) – If True, facets are laid out like a table with the highest values at the bottom-right.
drop (bool) – Whether to drop unused factor levels.
independent (str) – Which axes have truly independent scales per panel. One of “none”, “x”, “y”, or “all”.
axes (str) – Which inner axes to draw. One of “all”, “x”, “y”, or “margins”.
remove_labels (str) – Which inner axis labels to remove. One of “none”, “x”, “y”, or “all”.
- compute_layout(data)[source]¶
Compute layout
- Parameters:
data (list[pd.DataFrame]) – Dataframe for a each layer
- Return type:
pd.DataFrame
facet_wrap2¶
- class plotnine_extra.facet_wrap2(facets=None, *, nrow=None, ncol=None, scales='fixed', shrink=True, labeller='label_value', as_table=True, drop=True, dir='h', axes='margins', remove_labels='none', trim_blank=True)[source]¶
Bases:
plotnine.facet_wrapExtended wrapped facets.
Extends
plotnine.facet_wrapwith configurable inner axes, selective label removal, and blank-panel trimming.- Parameters:
facets (str or list of str, optional) – Variables to facet by.
nrow (int, optional) – Number of rows.
ncol (int, optional) – Number of columns.
scales (str) – Whether scales are fixed (“fixed”), free (“free”, “free_x”, “free_y”).
shrink (bool) – Whether to shrink scales to fit output of statistics.
labeller (str) – Labelling function for strip text.
as_table (bool) – If True, facets are laid out like a table.
drop (bool) – Whether to drop unused factor levels.
dir (str) – Direction: “h” for horizontal, “v” for vertical.
axes (str) – Which inner axes to draw. One of “all”, “x”, “y”, or “margins”.
remove_labels (str) – Which inner axis labels to remove. One of “none”, “x”, “y”, or “all”.
trim_blank (bool) – If True, trim blank panels from the layout.
- compute_layout(data)[source]¶
Compute layout
- Parameters:
data (list[pd.DataFrame]) – Dataframe for a each layer
- Return type:
pd.DataFrame
facet_nested¶
- class plotnine_extra.facet_nested(rows=None, cols=None, *, margins=False, scales='fixed', space='fixed', shrink=True, labeller='label_value', as_table=True, drop=True, nest_line=False, solo_line=False, resect=0, bleed=False)[source]¶
Bases:
plotnine.facet_gridGrid facets with nested strips.
Extends
plotnine.facet_gridso that hierarchical strip labels are merged when adjacent panels share the same parent category.- Parameters:
rows (str or list of str, optional) – Variables to facet by in rows.
cols (str or list of str, optional) – Variables to facet by in columns.
margins (bool or list of str) – Whether to display marginal facets.
scales (str) – Whether scales are fixed or free.
shrink (bool) – Whether to shrink scales to fit output of statistics.
labeller (str) – Labelling function for strip text.
as_table (bool) – If True, facets are laid out like a table.
drop (bool) – Whether to drop unused factor levels.
nest_line (bool) – If True, draw a nesting indicator line between parent and child strip levels.
solo_line (bool) – If True, draw a line even when a parent category has only one child.
resect (float) – Amount (in [0, 1]) to shorten the nesting line at each end.
bleed (bool) – If True, allow nesting indicators to extend into neighbouring strips.
- make_strips(layout_info, ax)[source]¶
Create strips with merged parent labels.
When adjacent panels share the same value for a parent (outer) faceting variable, the duplicate label is blanked out so that the parent strip appears to span multiple panels.
- Parameters:
layout_info (layout_details)
ax (Axes)
- Return type:
Strips
facet_nested_wrap¶
- class plotnine_extra.facet_nested_wrap(facets=None, *, nrow=None, ncol=None, scales='fixed', shrink=True, labeller='label_value', as_table=True, drop=True, dir='h', nest_line=False, solo_line=False, resect=0, bleed=False)[source]¶
Bases:
plotnine.facet_wrapWrapped facets with nested strips.
Extends
plotnine.facet_wrapso that hierarchical strip labels are merged when adjacent panels share the same parent category.- Parameters:
facets (str or list of str, optional) – Variables to facet by.
nrow (int, optional) – Number of rows.
ncol (int, optional) – Number of columns.
scales (str) – Whether scales are fixed or free.
shrink (bool) – Whether to shrink scales to fit output of statistics.
labeller (str) – Labelling function for strip text.
as_table (bool) – If True, facets are laid out like a table.
drop (bool) – Whether to drop unused factor levels.
dir (str) – Direction: “h” for horizontal, “v” for vertical.
nest_line (bool) – If True, draw a nesting indicator line between parent and child strip levels.
solo_line (bool) – If True, draw a line even when a parent category has only one child.
resect (float) – Amount (in [0, 1]) to shorten the nesting line at each end.
bleed (bool) – If True, allow nesting indicators to extend into neighbouring strips.
- make_strips(layout_info, ax)[source]¶
Create strips with merged parent labels.
When adjacent panels (in the same row) share the same value for a parent faceting variable, the duplicate label is blanked out so the parent strip visually spans those panels.
- Parameters:
layout_info (layout_details)
ax (Axes)
- Return type:
Strips
facet_manual¶
- class plotnine_extra.facet_manual(facets=None, *, design=None, scales='fixed', shrink=True, labeller='label_value', as_table=True, drop=True, respect=False, widths=None, heights=None, axes='margins', remove_labels='none', trim_blank=False)[source]¶
Bases:
facetManually specify panel layout via a design matrix.
Allows arbitrary placement of facet panels using a text design string or a 2-D array. Use
#to denote empty cells in the layout.- Parameters:
facets (str or list of str, optional) – Variables to facet by. Each unique combination is assigned a panel label from the design.
design (str or array-like, optional) – Layout specification. A multi-line string like
"AB\n#C"or a 2-D list / numpy array.scales (str) – Whether scales are fixed (“fixed”), free (“free”, “free_x”, “free_y”).
shrink (bool) – Whether to shrink scales to fit output of statistics.
labeller (str) – Labelling function for strip text.
as_table (bool) – If True, facets are laid out like a table.
drop (bool) – Whether to drop unused factor levels.
respect (bool) – If True, panel sizes respect the aspect ratio.
widths (Optional[Sequence[float]])
heights (Optional[Sequence[float]])
axes (Literal['all', 'x', 'y', 'margins'])
remove_labels (Literal['none', 'x', 'y', 'all'])
trim_blank (bool)
- make_strips(layout_info, ax)[source]¶
Create strips for the facet
- Parameters:
layout_info (layout_details) – Layout information. Row from the layout table
ax (Axes) – Axes to label
- Return type:
Strips
Per-panel Scales¶
Per-panel position scales are attached with + facetted_pos_scales(...) or
through scale_x_facet and scale_y_facet. Use free or independent facet
scales so plotnine can apply the per-panel replacements.
facetted_pos_scales¶
- plotnine_extra.facetted_pos_scales(x=None, y=None)[source]¶
Set individual position scales for facet panels.
Returns an object that, when added to a
ggplot, attaches per-panel position scales. Compatible facets (e.g.facet_grid2,facet_wrap2) will pick up these scales and apply them during plotting.- Parameters:
- Returns:
Object that can be added to a ggplot to apply per-panel position scales.
- Return type:
FacettedPosScales
Examples
from plotnine import * from plotnine_extra.facets import ( facet_wrap2, facetted_pos_scales, ) ( ggplot(df, aes("x", "y")) + geom_point() + facet_wrap2("group", scales="free_x") + facetted_pos_scales( x=[ scale_x_continuous(limits=(0, 10)), scale_x_continuous(limits=(0, 50)), ] ) )
scale_x_facet¶
- plotnine_extra.scale_x_facet(expr, *args, type='continuous', **kwargs)[source]¶
Apply x-axis scale to specific facet panels.
- Parameters:
expr (str or callable) – Expression identifying which panels to target.
*args (Any) – Forwarded to the underlying scale constructor.
**kwargs (Any) – Forwarded to the underlying scale constructor.
type (Literal['continuous', 'discrete'])
**kwargs
- Returns:
Object that can be added to a ggplot.
- Return type:
ScaleFacet
scale_y_facet¶
- plotnine_extra.scale_y_facet(expr, *args, type='continuous', **kwargs)[source]¶
Apply y-axis scale to specific facet panels.
- Parameters:
expr (str or callable) – Expression identifying which panels to target.
*args (Any) – Forwarded to the underlying scale constructor.
**kwargs (Any) – Forwarded to the underlying scale constructor.
type (Literal['continuous', 'discrete'])
**kwargs
- Returns:
Object that can be added to a ggplot.
- Return type:
ScaleFacet
Strips¶
Strip helpers are accepted by the extended facets where plotnine permits custom strip rendering. Unsupported strip and layout combinations raise explicit errors rather than being ignored.
strip_nested¶
- class plotnine_extra.strip_nested(*, nest_line=False, solo_line=False, resect=0, bleed=False)[source]¶
Bases:
StripNested strip labels.
Merges adjacent strip labels that share the same parent value, producing a hierarchical strip display.
- Parameters:
nest_line (bool) – If True, draw a line between parent and child strip levels.
solo_line (bool) – If True, draw a line even when there is only one child category.
resect (float) – Amount (in [0, 1]) to shorten the nesting line at each end.
bleed (bool) – If True, nesting indicators may extend into neighbouring strips.
strip_themed¶
- class plotnine_extra.strip_themed(*, background_x=None, background_y=None, text_x=None, text_y=None)[source]¶
Bases:
StripIndividually themed strip labels.
Allows each strip level or individual strips to have distinct visual styling (background colour, text style, etc.) via per-strip theme elements.
- Parameters:
background_x (list of theme elements, optional) – Background themes for horizontal strip levels.
background_y (list of theme elements, optional) – Background themes for vertical strip levels.
text_x (list of theme elements, optional) – Text themes for horizontal strip levels.
text_y (list of theme elements, optional) – Text themes for vertical strip levels.
strip_split¶
- class plotnine_extra.strip_split(position=('top', 'right'))[source]¶
Bases:
StripSplit strip labels across panel sides.
Places different faceting variable levels on different sides of the panel (e.g. one variable on top, another on the right).
strip_tag¶
- class plotnine_extra.strip_tag(tag_pool=None, *, position='topleft', prefix='', suffix='')[source]¶
Bases:
StripTag-style strip labels.
Instead of a strip bar above or beside the panel, labels are rendered as a small text tag within the panel area (e.g. “(a)”, “(b)”, …).
- Parameters:
tag_pool (list of str, optional) – Labels to use. Defaults to uppercase A-Z.
position (str) – Where within the panel to place the tag. One of “topleft”, “top”, “topright”, “left”, “right”, “bottomleft”, “bottom”, “bottomright”.
prefix (str) – Text prepended to each tag (e.g. “(“).
suffix (str) – Text appended to each tag (e.g. “)”).