0

I would like to add curly braces to a plotnine plot to highlight a feature.

At the moment I an annotating a '}' onto the plot, but in order to get it the right width, the brace is far too fat. This is also not scalable should I want it to expand to more than one group, and ideally it would be possible to control depth/width, and thickness etc.

Is it possible to add curly braces to a plotnine plot?

The below plot shows what I am trying to achieve.

import pandas as pd
from plotnine import *

df = pd.DataFrame({
    'Group':('Group1',)*6 + ('Group2',)*6,
    'AgeRange':(6,5,4,3,2,1)*2,
    'Mean':(1,3,5,7,9,11,2,4,6,8,10,12)
})


p = (ggplot(df,aes(x='AgeRange', y='Mean', fill='Group'))
  + theme_minimal()
  + geom_col(position='dodge', width=0.8)
  + coord_flip()
  + annotate('text', x=5, y=4.65, label='}', size=32)
  + annotate('text', x=5, y=5, label='Something pithy...', size=10, ha='left')
)
p

enter image description here

brb
  • 1,123
  • 17
  • 40
  • I'd have to think what you're doing is the right way to do it. It's a character in a font, so you get whatever the proportional thickness is for that font. You can always try a different font to find a slimmer glyph. – Tim Roberts Oct 31 '21 at 23:40
  • Ok thanks. I will experiment with different fonts. I thought there was a package in R's ggplot (pBrackets), but having another look at the solution I could be wrong and it's done with a custom annotation (which I think is beyond my abilities at the moment)... – brb Nov 01 '21 at 00:15
  • Not sure if this will yield easy solution, but I would look into using LaTeX since it usually gives you full control and nice symbols: https://matplotlib.org/stable/tutorials/text/usetex.html – Julien Nov 01 '21 at 00:26
  • Thanks for the suggestion Julien, appreciate it! I will look into whether I can get this to work. – brb Nov 01 '21 at 01:05

0 Answers0