Issue
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):
Solution
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
Answered By - fdireito
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.