How to add the text to the side of the line using matplotlib?

Question:

I have a code which generates the line using matplotlib. Below is the code

import matplotlib.pyplot as plt
plt.figure(1100)
plt.plot(np.array([1,1]),np.array([0,3]),'k','LineWidth',5)
x = np.array([1,1])
y = np.array([0,3])
plt.text(x[-1], y[-1]+0.5, '(t)', ha='center')
plt.show()

Below is the output of the above code
enter image description here

I need to add the text to the side of the line like below

enter image description here

How to achieve this using matplotlib?

Asked By: merkle

||

Answers:

I did what I can. It needs slight adjustment though

fig, ax = plt.subplots()

ax.set_xlim(0, 2)
ax.set_ylim(0, 4)

line_x = np.array([1.9, 1.9])
line_y = np.array([0, 3])
ax.plot(line_x, line_y, 'k', linewidth=5)

texts = ["RNG #1", "RNG #2", "RNG #3", "RNG #4"]
y_positions = np.linspace(0, 3, len(texts) + 1)[1:]

for i, text in enumerate(texts):
    ax.text(line_x[-1] + 0.05, y_positions[i], text, ha='left', va='center')

plt.show()

Output:

enter image description here

Answered By: Abdulmajeed
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.