Issue
I'm using matplotlib
for work and company policy is to include a watermark on every plot we make. Is there a way to set matplotlib
to do this by default?
I'm currently passing each Axes
object into a helper function which adds the watermark in the bottom left corner.
import matplotlib.pyplot as plt
def add_watermark(ax):
ax.text(ax.get_xlim()[0] + 0.1,
ax.get_ylim()[0] + 0.1,
"<Company Name>",
alpha=0.5)
fig, ax = plt.subplots(1, 1)
x = np.fromiter((np.random.uniform() for _ in range(100)), dtype=np.float32)
y = np.fromiter((np.random.uniform() for _ in range(100)), dtype=np.float32)
ax.scatter(x, y)
add_watermark(ax)
I would like to modify the default behavior of matplotlib
so that I don't have to pass each axes instance into a helper function.
Solution
You may easily subclass and monkey-patch the default axes. So create a file matplotlib_company.py like this
import matplotlib.axes
from matplotlib.offsetbox import AnchoredText
class MyAxes(matplotlib.axes.Axes):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
ab = AnchoredText("<company name>", loc="lower left", frameon=False,
borderpad=0, prop=dict(alpha=0.5))
ab.set_zorder(0)
self.add_artist(ab)
matplotlib.axes.Axes = MyAxes
Then import it everywhere you need it. I.e. the following
import matplotlib_company
import matplotlib.pyplot as plt
plt.plot([1,2,3])
plt.show()
creates
Answered By - ImportanceOfBeingErnest
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.