Issue
I'm trying to use an AnchoredText to print some information on an axis in Matplotlib, but I would like to have its background semitransparent (say alpha=0.5).
My initial guess was:
info_box = AnchoredText('test', loc='upper left', prop=dict(size=8, alpha=0.5), frameon=True,
bbox_to_anchor=(0., 1.), bbox_transform=axis.transAxes)
axis.add_artist(info_box)
But in this way the text was set 50% transparent, not the background.
Looking around I found that this is the expected behaviour and I have to make the bbox partially transparent, but I could not find how to do this on an AnchoredText object.
Do you have any suggestions?
Solution
You can set the alpha on the object's patch. I agree this is not obvious from the documentation.
import matplotlib.pyplot as plt
from matplotlib.offsetbox import AnchoredText
fig, ax = plt.subplots()
ax.plot([1, 0])
info_box = AnchoredText('test', loc='upper left', prop=dict(size=8), frameon=True,
bbox_to_anchor=(0., 1.), bbox_transform=ax.transAxes)
info_box.patch.set_alpha(0.5)
ax.add_artist(info_box)
plt.show()
Answered By - RuthC
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.