I'm plotting a figure with matplotlib
, it is a 2x2 figure. I want to add a watermark to every subplot, how can I do that? I know how to add a watermark to the whole figure, but I didn't manage to do that on the single subplots.
Here is my code:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# Create dummy DataFrame
df = pd.DataFrame(np.array([[1, 2], [3, 4], [5, 6]]),
columns=['a', 'b'])
fig, axes2 = plt.subplots(nrows=2, ncols=2, figsize=(9, 9))
# unpack all the axes subplots
axes = axes2.ravel()
df.iloc[:, [0, 1]].plot(ax=axes[0], marker='o')
axes[0].set_title('Subplot 1')
df.iloc[:, [0, 1]].plot(ax=axes[1], marker='o')
axes[1].set_title('Subplot 2')
df.iloc[:, [0, 1]].plot(ax=axes[2], marker='o')
axes[2].set_title('Subplot 3')
df.iloc[:, [0, 1]].plot(ax=axes[3], marker='o')
axes[3].set_title('Subplot 4')
# Text Watermark
fig.text(0.7,0.3, 'Test watermark',
fontsize=50, color='gray',
ha='right', va='bottom', alpha=0.4,rotation=45)
plt.tight_layout()
plt.savefig('../test.png', dpi=300)
plt.show()
CodePudding user response:
You can assign .text
to each axes
like below.
Try this:
# Text Watermark
axes[0].text(1.0, 1.0,'Subplot 1 Test watermark', alpha=0.4,rotation=45)
axes[1].text(1.0, 1.0,'Subplot 2 Test watermark', alpha=0.4,rotation=45)
axes[2].text(1.0, 1.0,'Subplot 3 Test watermark', alpha=0.4,rotation=45)
axes[3].text(1.0, 1.0,'Subplot 4 Test watermark', alpha=0.4,rotation=45)
Output: