4

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 : What I have

and here is what I would like to have (with text on the upper-left corner): The objective

1 Answer 1

3

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()

Resulting in: enter image description here

See also: https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.text.html https://matplotlib.org/stable/tutorials/advanced/transforms_tutorial.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.