Using plotnine I am trying to plot line segments and annotate each segment on the right hand side of the plot. I am following examples on plotnine online documentation, however the text on the right are being cutoff. I can display the text if I use ha='right'
but that's not what I want, and it moves text into the plots. I also tried adding + theme(subplots_adjust={'right': 2})
but that only expanded the width of the plot and didn't include the text.
Please note that I do not want to manually specify x
positions because the range of the x
values in my dataframe will vary and I need to make many plots in a script. Being able to get xlimits
using some plotnine function and pass them to x
positions ok though, but I could not figure out how to do that.
Even better would be to have the text placed out of the plot area.
My question is: is there a way to place the geom_text()
text out of the plot's main box, on the margin in the right (and also to the left)? How can I achieve this?
A code example is provided below.
from plotnine import *
from plotnine.data import mtcars
import numpy as np
mtcars_plot_df = mtcars[:20]
mtcars_plot_df.loc[:, 'wt_l'] = mtcars_plot_df['wt'] - np.random.uniform(1)
mtcars_plot_df.loc[:, 'wt_h'] = mtcars_plot_df['wt'] + np.random.uniform(1)
mtcars_plot_df.loc[:, 'wt_str'] = mtcars_plot_df['wt'].round(2).astype(str) + ' (' + mtcars_plot_df['wt_l'].round(2).astype(str) + ' - ' + mtcars_plot_df['wt_h'].round(2).astype(str) + ')'
nrows = mtcars_plot_df.shape[0] * 0.2
(ggplot()
# Range strip
+ geom_segment(
mtcars_plot_df,
aes(x='wt_l', xend='wt_h', y='name', yend='name'),
size=1,
color='red'
)
# Point markers
+ geom_point(
mtcars_plot_df,
aes('wt', 'name'),
size=1.5,
stroke=0.7,
color='blue'
)
# Range label
+ geom_text(
# mtcars_plot_df, => This generates error
aes(x=np.inf, y='name', label="wt_str"),
data = mtcars_plot_df, # => This gets rid of the error
size=8,
ha='left'
)
+ theme(figure_size=(4, nrows*1)) # here you define the plot size
)
When I use ha='right'
in geom_text()
, I get a plot as shown in the attached image. As you can see, the text interferes with the plot. When I use ha='left'
, the text is not visible at all. I would like the text to be outside the plot area, in the white area right of the plot similar to how the car names are printed on the left of the plot.
A side question about something I came across while following plotnine documentation is: if I do not specify the dataframe as data = mtcars_plot_df
after the aes
line, then I get this error: ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()
. See my two comments in the code above.