compose.block_plot(fig, alt='', width=520, align='center', float='none')
Create a block containing an embedded plotnine plot.
This function saves a plotnine plot to a temporary PNG file and wraps it as a Block with an embedded image.
Parameters
fig :
-
A plotnine plot object (ggplot).
alt : str = ''
-
Alt text for accessibility. Default is empty string.
width : Union[int, str] = 520
-
Image width. Can be an integer (interpreted as pixels, e.g., 520 → “520px”) or a CSS string (e.g., “600px”, “50%”). Default is 520 (pixels).
align : Literal['center', 'left', 'right', 'inline'] = 'center'
-
Block-level alignment: “center”, “left”, “right”, or “inline”. Default is “center”.
float : Literal['none', 'left', 'right'] = 'none'
-
CSS float value for text wrapping: “none”, “left”, or “right”. Default is “none”.
Returns
: Block
-
A block containing the plot image.
Raises
: ImportError
-
If the plotnine package is not installed.
Examples
from nbmail.compose import block_plot, create_blocks, block_text, compose_email
from plotnine import ggplot, aes, geom_point
from great_tables.data import gtcars
plot = (
ggplot(gtcars, aes("trq", "hp"))
+ geom_point()
)
compose_email(
body=create_blocks(
block_text("Here's my plot:"),
block_plot(plot, alt="Scatter plot"),
block_text("What do you think?")
)
)