簡體   English   中英

如何將文本放在matplotlib中的繪圖框中

[英]How to put text inside a box on a plot in matplotlib

我想在matplotlib圖上的一個方框中放一個文本,但是文檔只提供了如何將它放在右上角的示例(並且選擇不同的角落並不是很簡單)。

以下是示例中的代碼:

# these are matplotlib.patch.Patch properties
props = dict(boxstyle='round', facecolor='wheat', alpha=0.5)

# place a text box in upper left in axes coords
ax.text(0.05, 0.95, textstr, transform=ax.transAxes, fontsize=14,
    verticalalignment='top', bbox=props)

Matplotlib坐標

使用transform=ax.transAxes我們可以使用坐標系將元素放入繪圖中,其中點(0,0)是左下角,(0,1)左上角,(1,1)是右上角,依此類推。

具體來說:如果我們使用位置(0,0)放置一個文本框,則會在左下角放置一個名為anchor的特定點。 要更改錨點,您需要為函數調用添加兩個參數: verticalalignment (可能的值: centertopbottombaseline )和horizontalalignment (可能的值: centerrightleft )。

因此,要將框放在左下角,您需要將框的左下角放在圖的左下角:

# place a text box in lower left in axes coords
ax.text(0.05, 0.05, textstr, transform=ax.transAxes, fontsize=14,
    verticalalignment='bottom', bbox=props)

無論如何,這里是ipython-notebook的鏈接, 其中包含所有展示位置的示例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM