我正在尝试创建一个散点图。我想放置两行文本: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()
plt.text默认使用数据坐标。您可以像这样使用轴坐标:
plt.text(0.5, 0.25, 'PP=0.87 \n SC=0.76', transform=plt.gca().transAxes)
带屏幕截图的可重现示例
\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nsns.set()  # overrides default matplotlib settings with seaborn\'s\n\n# Data\nx = [6,9,4,7,3]\ny = [4,9,3,5,3]\na) 数据坐标
\nplt.text(9.92, 8.71, \'PP = 0.87 \\nSC = 0.76\', \n         fontsize=10, fontfamily=\'Georgia\', color=\'k\',\n         ha=\'center\', va=\'center\');\nb) 轴坐标
\nplt.text(1.01, 0.85, \'PP = 0.87 \\nSC = 0.76\', \n         fontsize=10, fontfamily=\'Georgia\', color=\'k\',\n         ha=\'left\', va=\'bottom\',\n         transform=plt.gca().transAxes);\ntransform=plt.gca().transAxes  \xc2\xa0\xe2\x86\x90\xc2\xa0 指定 x,y 坐标为轴
from matplotlib.offsetbox import AnchoredText\n\nx = [6,9,4,7,3]\ny = [4,9,3,5,3]\nf, ax = plt.subplots(1,1)\nplt.scatter(x, y, c = \'k\')\nanchored_text = AnchoredText(\'PP = 0.87 \\nSC = 0.76\', \n                             frameon=False, borderpad=0, pad=0.1, \n                             loc=1, bbox_to_anchor=[1.19,1], \n                             bbox_transform=plt.gca().transAxes,\n                             prop={\'color\':\'k\',\'fontsize\':10,\n                                   \'fontfamily\':\'Georgia\'})\nax.add_artist(anchored_text)\n小智 2
plt.text() 函数的语法是:
plt.text(x, y, s)
这些坐标指的是绘图坐标。您的坐标为负数,因此您会在左下角看到文本。
尝试:
plt.text(11, 10, "string")
| 归档时间: | 
 | 
| 查看次数: | 5765 次 | 
| 最近记录: |