Strip Text - Dev Testing Notebook

Development notebook for testing strip text options, including strip_text_x, strip_text_y, angle, size, hjust, vjust, margins, and facet variants.

In [1]:
import pandas as pd
from lets_plot import *
LetsPlot.setup_html()
In [2]:
# Base dataset: two factors with three levels each.
data = {
    'x':    ['A', 'B', 'C', 'A', 'B', 'C', 'A', 'B', 'C',
              'A', 'B', 'C', 'A', 'B', 'C', 'A', 'B', 'C'],
    'row':  ['North', 'North', 'North', 'South', 'South', 'South', 'East', 'East', 'East',
              'North', 'North', 'North', 'South', 'South', 'South', 'East', 'East', 'East'],
    'col':  ['Alpha', 'Alpha', 'Alpha', 'Alpha', 'Alpha', 'Alpha', 'Alpha', 'Alpha', 'Alpha',
              'Beta',  'Beta',  'Beta',  'Beta',  'Beta',  'Beta',  'Beta',  'Beta',  'Beta'],
    'val':  [12, 15, 9, 11, 7, 14, 6, 18, 10, 13, 8, 16, 5, 19, 11, 9, 14, 12]
}

# Long-label dataset for testing layout under stress.
data_long = {
    'x':   list(range(6)) * 4,
    'y':   [i * 1.2 + (j % 2) for j in range(4) for i in range(6)],
    'row': ['Very Long Row Label One'] * 12 + ['Very Long Row Label Two'] * 12,
    'col': (['Short Col A'] * 6 + ['Short Col B'] * 6) * 2,
}

base = ggplot(data, aes('x', 'val')) + geom_bar(stat='identity', fill='steelblue') + theme_bw()

1. strip_text_x: angle sweep (facet_grid columns)

In [3]:
angles = [0, 30, 45, 90, -45]
plots = []
for a in angles:
    p = base + \
        facet_grid(x='col') + \
        theme(strip_text_x=element_text(angle=a, size=12)) + \
        ggtitle(f'strip_text_x angle={a}') + \
        ggsize(420, 220)
    plots.append(p)
bunch = GGBunch()
for i, p in enumerate(plots):
    bunch.add_plot(p, (i % 3) * 440, (i // 3) * 240)
bunch.show()
(!) WARN: class GGBunch is deprecated and will be removed in future releases.

  Please, use function ggbunch() to combine several plots into
  a single figure with custom layout.

2. strip_text_y: angle sweep (facet_grid rows)

In [4]:
plots = []
for a in angles:
    p = base + \
        facet_grid(y='row') + \
        theme(strip_text_y=element_text(angle=a, size=12)) + \
        ggtitle(f'strip_text_y angle={a}') + \
        ggsize(420, 280)
    plots.append(p)
bunch = GGBunch()
for i, p in enumerate(plots):
    bunch.add_plot(p, (i % 3) * 440, (i // 3) * 300)
bunch.show()
(!) WARN: class GGBunch is deprecated and will be removed in future releases.

  Please, use function ggbunch() to combine several plots into
  a single figure with custom layout.

3. X and Y strips: independent angles (facet_grid)

In [5]:
combos = [(0, 90), (45, 0), (45, 45), (90, 90)]
plots = []
for ax, ay in combos:
    p = base + \
        facet_grid(x='col', y='row') + \
        theme(
            strip_text_x=element_text(angle=ax, size=11),
            strip_text_y=element_text(angle=ay, size=11),
        ) + \
        ggtitle(f'x_angle={ax}, y_angle={ay}') + \
        ggsize(460, 340)
    plots.append(p)
bunch = GGBunch()
for i, p in enumerate(plots):
    bunch.add_plot(p, (i % 2) * 480, (i // 2) * 360)
bunch.show()
(!) WARN: class GGBunch is deprecated and will be removed in future releases.

  Please, use function ggbunch() to combine several plots into
  a single figure with custom layout.

4. Font size effect at a fixed 45-degree angle

In [6]:
sizes = [8, 12, 16, 22]
plots = []
for s in sizes:
    p = base + \
        facet_grid(x='col', y='row') + \
        theme(
            strip_text_x=element_text(angle=45, size=s),
            strip_text_y=element_text(angle=45, size=s),
        ) + \
        ggtitle(f'size={s}, angle=45') + \
        ggsize(420, 320)
    plots.append(p)
bunch = GGBunch()
for i, p in enumerate(plots):
    bunch.add_plot(p, (i % 2) * 440, (i // 2) * 340)
bunch.show()
(!) WARN: class GGBunch is deprecated and will be removed in future releases.

  Please, use function ggbunch() to combine several plots into
  a single figure with custom layout.

5. hjust and vjust with angle=45

In [7]:
combos_jv = [(0, 0), (0.5, 0.5), (1, 1), (0, 1), (1, 0)]
plots = []
for h, v in combos_jv:
    p = base + \
        facet_grid(x='col') + \
        theme(
            strip_text_x=element_text(angle=45, size=13, hjust=h, vjust=v),
            strip_background_x=element_rect(fill='aliceblue', color='steelblue'),
        ) + \
        ggtitle(f'hjust={h}, vjust={v}') + \
        ggsize(420, 240)
    plots.append(p)
bunch = GGBunch()
for i, p in enumerate(plots):
    bunch.add_plot(p, (i % 3) * 440, (i // 3) * 260)
bunch.show()
(!) WARN: class GGBunch is deprecated and will be removed in future releases.

  Please, use function ggbunch() to combine several plots into
  a single figure with custom layout.

6. facet_wrap: strip_text_x angle sweep

In [8]:
data_wrap = {
    'x':   list(range(5)) * 6,
    'y':   [i + j * 0.7 for j in range(6) for i in range(5)],
    'grp': (['Group One'] * 5 + ['Group Two'] * 5 + ['Group Three'] * 5) * 2,
}

plots = []
for a in [0, 30, 45, 90]:
    p = ggplot(data_wrap, aes('x', 'y')) + \
        geom_point(color='steelblue') + \
        facet_wrap('grp', ncol=3) + \
        theme_bw() + \
        theme(strip_text_x=element_text(angle=a, size=12)) + \
        ggtitle(f'facet_wrap angle={a}') + \
        ggsize(480, 260)
    plots.append(p)
bunch = GGBunch()
for i, p in enumerate(plots):
    bunch.add_plot(p, (i % 2) * 500, (i // 2) * 280)
bunch.show()
(!) WARN: class GGBunch is deprecated and will be removed in future releases.

  Please, use function ggbunch() to combine several plots into
  a single figure with custom layout.

7. Long strip labels: layout stress test

In [9]:
base_long = ggplot(data_long, aes('x', 'y')) + geom_point(color='tomato') + theme_bw()

for a in [0, 45, 90]:
    p = base_long + \
        facet_grid(x='col', y='row') + \
        theme(
            strip_text_x=element_text(angle=a, size=11),
            strip_text_y=element_text(angle=a, size=11),
        ) + \
        ggtitle(f'Long labels, angle={a}') + \
        ggsize(620, 380)
    p.show()

8. Styled strips: color, face, and background

In [10]:
(base +
 facet_grid(x='col', y='row') +
 theme(
     strip_text_x=element_text(angle=35, size=13, face='bold', color='darkslategray'),
     strip_text_y=element_text(angle=-35, size=13, face='italic', color='darkred'),
     strip_background_x=element_rect(fill='aliceblue', color='cadetblue'),
     strip_background_y=element_rect(fill='lemonchiffon', color='tan'),
 ) +
 ggtitle('Styled strips: x angle=35, y angle=-35') +
 ggsize(560, 420)
)
Out[10]:

9. Default angle=0 baseline for comparison

In [11]:
(base +
 facet_grid(x='col', y='row') +
 ggtitle('Default - no angle override') +
 ggsize(560, 400)
)
Out[11]: