我正在尝试创建一个散点图。我想放两行文字:PP = 0.87和SC = 0.76。现在,只要我在代码中输入一些文字,它就会放在曲线的左下角而不是右上角,请告诉我应该做什么。
我得到了什么: https://i.stack.imgur.com/r2wH3.png
我期待两行: https://i.stack.imgur.com/P5d81.jpg
我的代码:
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()
答案 0 :(得分:2)
plt.text
默认使用数据坐标。你可以像这样使用轴坐标:
plt.text(0.5, 0.25, 'PP=0.87 \n SC=0.76', transform=plt.gca().transAxes)
答案 1 :(得分:2)
带有屏幕截图的可重现示例
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sns.set() # overrides default matplotlib settings with seaborn's
# Data
x = [6,9,4,7,3]
y = [4,9,3,5,3]
a) 数据坐标
plt.text(9.92, 8.71, 'PP = 0.87 \nSC = 0.76',
fontsize=10, fontfamily='Georgia', color='k',
ha='center', va='center');
b) 轴坐标
plt.text(1.01, 0.85, 'PP = 0.87 \nSC = 0.76',
fontsize=10, fontfamily='Georgia', color='k',
ha='left', va='bottom',
transform=plt.gca().transAxes);
transform=plt.gca().transAxes
← 指定 x,y 坐标用于轴
from matplotlib.offsetbox import AnchoredText
x = [6,9,4,7,3]
y = [4,9,3,5,3]
f, ax = plt.subplots(1,1)
plt.scatter(x, y, c = 'k')
anchored_text = AnchoredText('PP = 0.87 \nSC = 0.76',
frameon=False, borderpad=0, pad=0.1,
loc=1, bbox_to_anchor=[1.19,1],
bbox_transform=plt.gca().transAxes,
prop={'color':'k','fontsize':10,
'fontfamily':'Georgia'})
ax.add_artist(anchored_text)
答案 2 :(得分:1)
plt.text()函数的语法是:
plt.text(x, y, s)
这些坐标指的是绘图坐标。您的坐标为负数,因此您可以在左下角找到文本。
尝试:
plt.text(11, 10, "string")