import highlight_text as ht
from plotnine import aes, geom_point, ggplot, labs, theme_matplotlib, theme_set
from plotnine.data import mpg
theme_set(theme_matplotlib())
= (
p ="displ", y="hwy"))
ggplot(mpg, aes(x+ geom_point()
+ labs(x="displacement", y="horsepower")
)
= p.draw()
fig = fig.axes[0] # or via fig.get_axes()[0]
ax = 'Color highlighted by <HighlightText::{"color": "#E58606"}>'
ht_ax_text 4, 40, ht_ax_text, vsep=3, fontsize=9, va="top", ax=ax)
ht.ax_text( fig
This is a short post documenting my experience trying to retrieve the Axes
from plotnine.
The idea was inspired by this code and this video I came across, but the original solution didn’t quite work for me. Fortunately, I found that we can access the list of Axes
directly using either fig.axes
or fig.get_axes()
.
In the example below, I adapt a snippet from the plotnine documentation to demonstrate how to highlight text in color using HighlightText.
Disclaimer
This post was drafted by me, with AI assistance to refine the content.