For a personal project, I'd like to add a text in the upper right corner of the plot but as I'm displaying horizontal bars, I don't have y-coordinates and thus, I can't use either plt.text()
or ax.annotate()
as they require a y-coordinates.
Here is a picture of the plot I'm having :
and here is what I would like to have (with text on the upper-left corner):
CodePudding user response:
You can use text
. And the coordinates "still exist". See the following example:
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.subplots()
names = ["a", "b", "c"]
val = [23, 17, 5]
ax.barh(names, val)
ax.text(0.8, 0.95, "text 1", transform = ax.transAxes)
ax.text(20, 0, "text 2", transform = ax.transData)
ax.text(20, 2, "text 3", transform = ax.transData)
plt.show()
See also: https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.text.html https://matplotlib.org/stable/tutorials/advanced/transforms_tutorial.html