%matplotlib inline
In this post, I will introduce mpl-speech-bubble package.
You can install it by
pip install mpl-speech-bubble
And the source code can be found at https://github.com/leejjoon/mpl-speech-bubble and documentation at https://mpl-speech-bubble.readthedocs.io/ (documentation is far from complete)
With mpl-speech-bubble
, you can annotate you Matplotlib plot with speech bubbles, like this
Let’s start with a Matplotlib’s annotate example. Note that we use “wedge” arrow style.
import matplotlib.pyplot as plt
= plt.subplots(num=1, clear=True)
fig, ax
= (0.2, 0.5)
xy 0]], [xy[1]], "o")
ax.plot([xy[
= dict(
annotate_kwargs ="center", va="bottom",
ha=20,
size=dict(boxstyle="round, pad=0.2",
bbox="w", ec="k"),
fc=dict(
arrowprops="wedge, tail_width=0.5",
arrowstyle="y",
fc=None, # by default, annotate set patchA to the bbox.
patchA
)
)
= ax.annotate(
t ="Default",
text=xy, xycoords='data',
xy=(-0., .9), textcoords="offset fontsize",
xytext**annotate_kwargs
)
0, 1) ax.set_xlim(
(0.0, 1.0)
mpl-speech-bubble
has a function annotate_merged
. This fucntion is mostly identical to MPL’s annotate, and at the drawing time, it will merge the bbox patch and the arrow patch. Behind the scence, it uses skia-pathops
to merge bezier paths. The properties of merged patch will inherid from the bbox patch.
from mpl_speech_bubble import annotate_merged
= (0.5, 0.5)
xy 0]], [xy[1]], "o")
ax.plot([xy[
= annotate_merged(
t
ax,="Merged",
text=xy, xycoords='data',
xy=(-0., .9), textcoords="offset fontsize",
xytext**annotate_kwargs
)
In addtion, it provides annotate-bubble
function. It has slghtly different call signature from annotate
, but has an advantage of better handling of things like rotation.
Instead of xytext
and textcoords
, you should use loc
and dist
. The unit of dist
is fontsize.
from mpl_speech_bubble import annotate_bubble
= (0.8, 0.5)
xy 0]], [xy[1]], "o")
ax.plot([xy[
= annotate_bubble(
t
ax,="Bubble",
text=xy, xycoords='data',
xy="up", dist=1.,
loc=20,
size
)
= annotate_bubble(
t
ax,="Bubble 2",
text=xy, xycoords='data',
xy="down", dist=1.,
loc=20, rotation=30,
size )
annotate_bubble
is a simple wrapper around AnnotationBubble
class. Please take a look at the example here