Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Position matplotlib text top right

I am trying to create a scatter plot. I want to place two lines of text: PP=0.87 and SC=0.76. Now whenever I'm entering some text in the code it is placed in the bottom left corner of the curve instead of the top right please tell me what I should do.

What I'm getting: https://i.stack.imgur.com/r2wH3.png

I'm expecting in two lines: https://i.stack.imgur.com/P5d81.jpg

My code:

import numpy as np
import matplotlib.pyplot as plt
x = [6,9,4,7,3]
y = [4,9,3,5,3]
plt.text(-0.5, -0.25, 'PP=0.87 \n SC=0.76')
plt.scatter(x, y, c = 'k')
plt.show()
like image 280
Subhankar Ghosh Avatar asked Dec 23 '22 22:12

Subhankar Ghosh


1 Answers

plt.text uses data coordinates by default. You can use axis coordinates like this:

plt.text(0.5, 0.25, 'PP=0.87 \n SC=0.76', transform=plt.gca().transAxes)
like image 172
jmd_dk Avatar answered Jan 06 '23 02:01

jmd_dk