-1

enter image description here

This image is plot using:

subplot(1,2,1)
hist(...)
subplot(1,2,2)
text(...)

But I don't want the border of second plot and I want the text on the upper left of the plot. How can I make it?

Howard
  • 217
  • 2
  • 12

1 Answers1

2

Try this:

subplot(1,2,1)
hist(...)
ax2 = subplot(1,2,2)
ax2.set_xticks([])
ax2.set_yticks([])
for spine in ax2.spines.values():
    spine.set_visible(False)
ax2.invert_yaxis()
ax2.text(..., verticalalignment="top")

Update:

As pointed out in the comments, you can also just do:

subplot(1,2,1)
hist(...)
ax2 = subplot(1,2,2)
ax2.axis("off")
ax2.invert_yaxis()
ax2.text(..., verticalalignment="top")

Although this will remove the default axis background too (depending on your settings and Matplotlib version, this may leave you with a gray background color).

jdehesa
  • 58,456
  • 7
  • 77
  • 121
  • Thanks, this is perfect. – Howard Jul 28 '17 at 08:47
  • 2
    For this, you can just use `plt.axis('off')` on the second plot. – umutto Jul 28 '17 at 08:48
  • Yes, I checked the Axes API, and replace it with `ax2.set_axis_off()` – Howard Jul 28 '17 at 08:58
  • why do you `invert_yaxis()`? – tmdavison Jul 28 '17 at 09:17
  • @tom So the text will appear on the upper part of the plot, as requested by the OP (seems that the origin, or a point close to it, is being used as coordinates for the text). – jdehesa Jul 28 '17 at 09:28
  • wouldn't it be simpler to just use the `x, y` coordinates in `ax.text()` to set the position of the text? and if needed set `transform=ax2.transAxes`? – tmdavison Jul 28 '17 at 09:29
  • @tom You could do that, but I wouldn't say it's simpler... You'd need to retrieve the maximum value of the Y axis and use it as coordinate, and if the axis ever changes scale for whatever reason it may cause the text to move. I think it's easier to think of it in terms of a canvas having the origin in the top-left corner. But I guess you can find pros and cons depending on the specific case and personal preference. – jdehesa Jul 28 '17 at 09:33
  • That's why you would set the transform to axes coordinates rather than data coordinate. But each to his own, I guess :) – tmdavison Jul 28 '17 at 09:34
  • @tom Ahh alright I see what you mean, yes well there are many ways to go about it I guess. – jdehesa Jul 28 '17 at 10:06