Back to Article
Text Outside the Panel(s)
Download Notebook

Text Outside the Panel(s)

In [1]:
from plotnine import ggplot, aes, labs, geom_point, theme, theme_538, theme_set
from plotnine.data import mtcars

theme_set(theme_538())
In [2]:
# Gallery, labels

caption = """\
This graphic illustrates the inverse relationship between vehicle weight
and fuel efficiency (MPG). It aims to highlight how heavier vehicles generally
consume more fuel, and how the complexity of an engine's cylinder count and
transmission system can affect its fuel economy.
"""

from plotnine import *
(
    ggplot(mtcars, aes("mpg", "wt", colour="factor(cyl)", size="gear"))
    + geom_point()
    + labs(
        x="Miles per Gallon",
        y="Weight",
        colour="Cylinders",
        size="Gears",
        title="Fuel Efficiency vs. Vehicle Weight",
        subtitle ="Exploring Factors that Affect the Fuel Effeciency of a Car",
        caption=caption,
    )

    + theme(
        # left justify the caption and have one line of space between it and
        # the x-axis label
        plot_caption=element_text(ha="left", margin={"t": 1, "units": "lines"})
    )
    
)