Reputation: 155
I have a graph in which I have filled out a certain area. I'm looking to add an annotation/label to the filled area but can't figure out how. Please find my code below:
import numpy as np
import matplotlib.pyplot as plt
X = np.linspace(0, 2 * np.pi, 100)
Ya = np.sin(X)
plt.plot(X, Ya)
plt.fill_between(X, Ya, 0,
where = (X >=3.00) & (Ya<= 0),
color = 'b',alpha=.1)
I'd like to add an annotation to the part filled blue. Any help would be greatly appreciated!
Upvotes: 4
Views: 1686
Reputation: 80509
You can grab the output of the fill_between
, get its bounding box and use its center to position the text:
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
X = np.linspace(0, 2 * np.pi, 100)
Ya = np.sin(X)
ax.plot(X, Ya)
filled_poly = ax.fill_between(X, Ya, 0,
where=(X >= 3.00) & (Ya <= 0),
color='b', alpha=.1)
(x0, y0), (x1, y1) = filled_poly.get_paths()[0].get_extents().get_points()
ax.text((x0 + x1) / 2, (y0 + y1) / 2, "Filled\npolygon", ha='center', va='center', fontsize=16, color='crimson')
plt.show()
Upvotes: 7