1

I've got 3 images I'm creating, and I want to draw text on the middle one, or maybe the first one. However by default my code is drawing on the third one (right-most). Here is the relevant code:

fig, axes = plt.subplots(nrows=1, ncols=3)

text = "testing"
px = [x_pt_1, x_pt_2]
py = [y_pt_1, y_pt_2]
plt.plot(px, py, color="white", linewidth=2)
plt.text(100, 50, text, size=16, color='white')

imgs = [img_left, img_middle, img_right]
for img, ax in zip(imgs, axes.ravel()):
    ax.imshow(img)
fig.tight_layout()

# Show images
plt.show()

Edit

I was linked to another question because allegedly this is a duplicate. But I opened up the question and they don't address this particular issue.

So instead of having the text be put on the rightmost image, how do I put it on the middle or left images? And in general I'd like to put some text on one image, some different text on another image, etc.

halfer
  • 19,824
  • 17
  • 99
  • 186
JDS
  • 16,388
  • 47
  • 161
  • 224
  • 2
    Using `plt.` will always target the last axes which was created (in your case, the one on the far right). Nevertheless, you can access any axes easily with `axes[0]` (for the left-most plot) or `axes[1]` for the middle. `axes[2]` will again return the right most plot. Furthermore, an axes object shares much of the same API as the `plt` module. So, in the end, you want something like `axes[1].text(100, 50, text, size=16, color='white')` – Severin Apr 26 '21 at 20:19
  • @Severin thank you, 100% what I needed. Not clear to a matplotlib first time user how to get this stuff going. Feel free to add an answer and I'll accept. – JDS Apr 26 '21 at 20:20
  • 1
    I unfortunately don't see that option :/ . But anyway, glad I could help! – Severin Apr 26 '21 at 20:25
  • 1
    It's not letting us answer, but here's what you need to do. Instead of calling plt.plot() and plt.text(), call axes[i].plot() or axes[i].text() where i is the index of your subplots. In this case, you have three subplots so index at 1 would call the one in the middle. I hope this helps. – Gabe Morris Apr 26 '21 at 20:29
  • Yeah somebody closed it due to it being a duplicate, and then linked to a question that didn't even address the issue... sigh. Anyways thanks for the help and enjoy the comment upvotes – JDS Apr 26 '21 at 20:59

0 Answers0