簡體   English   中英

將文本放在matplotlib圖的左上角

[英]Putting text in top left corner of matplotlib plot

如何將文本放在 matplotlib 圖形的左上角(或右上角),例如左上角圖例所在的位置,或者在圖的頂部但在左上角? 例如,如果它是plt.scatter() ,那么將在散點圖的正方形內的東西放在最左上角。

例如,我想在理想情況下不知道所繪制散點圖的比例的情況下執行此操作,因為它會從數據集更改為數據集。 我只是希望它的文本大致在左上角,或者大致在右上角。 使用圖例類型定位時,無論如何它都不應與任何散點圖點重疊。

您可以使用text

text(x, y, s, fontsize=12)

text坐標可以相對於軸給出,因此文本的位置將與繪圖的大小無關:

默認轉換指定文本在數據坐標中,或者,您可以在軸坐標中指定文本(0,0 是左下角,1,1 是右上角)。 下面的示例將文本放置在軸的中心:

text(0.5, 0.5,'matplotlib',
     horizontalalignment='center',
     verticalalignment='center',
     transform = ax.transAxes)

為了防止文本干擾你分散的任何點,afaik 更加困難。 更簡單的方法是將 y_axis (ymax in ylim((ymin,ymax)) ) 設置為比您的點的最大 y 坐標高一點的值。 這樣,您將始終擁有用於文本的空閑空間。

編輯:這里有一個例子:

In [17]: from pylab import figure, text, scatter, show
In [18]: f = figure()
In [19]: ax = f.add_subplot(111)
In [20]: scatter([3,5,2,6,8],[5,3,2,1,5])
Out[20]: <matplotlib.collections.CircleCollection object at 0x0000000007439A90>
In [21]: text(0.1, 0.9,'matplotlib', ha='center', va='center', transform=ax.transAxes)
Out[21]: <matplotlib.text.Text object at 0x0000000007415B38>
In [22]:

在此處輸入圖像描述

ha 和 va 參數設置文本相對於插入點的對齊方式。 IE。 ha='left' 是一個很好的設置,可以防止在手動縮小(變窄)框架時長文本超出左軸。

import matplotlib.pyplot as plt

plt.figure(figsize=(6, 6))
plt.text(0.1, 0.9, 'text', size=15, color='purple')

# or 

fig, axe = plt.subplots(figsize=(6, 6))
axe.text(0.1, 0.9, 'text', size=15, color='purple')

兩者的輸出

在此處輸入圖像描述

import matplotlib.pyplot as plt

# Build a rectangle in axes coords
left, width = .25, .5
bottom, height = .25, .5
right = left + width
top = bottom + height
ax = plt.gca()
p = plt.Rectangle((left, bottom), width, height, fill=False)
p.set_transform(ax.transAxes)
p.set_clip_on(False)
ax.add_patch(p)


ax.text(left, bottom, 'left top',
        horizontalalignment='left',
        verticalalignment='top',
        transform=ax.transAxes)

ax.text(left, bottom, 'left bottom',
        horizontalalignment='left',
        verticalalignment='bottom',
        transform=ax.transAxes)

ax.text(right, top, 'right bottom',
        horizontalalignment='right',
        verticalalignment='bottom',
        transform=ax.transAxes)

ax.text(right, top, 'right top',
        horizontalalignment='right',
        verticalalignment='top',
        transform=ax.transAxes)

ax.text(right, bottom, 'center top',
        horizontalalignment='center',
        verticalalignment='top',
        transform=ax.transAxes)

ax.text(left, 0.5 * (bottom + top), 'right center',
        horizontalalignment='right',
        verticalalignment='center',
        rotation='vertical',
        transform=ax.transAxes)

ax.text(left, 0.5 * (bottom + top), 'left center',
        horizontalalignment='left',
        verticalalignment='center',
        rotation='vertical',
        transform=ax.transAxes)

ax.text(0.5 * (left + right), 0.5 * (bottom + top), 'middle',
        horizontalalignment='center',
        verticalalignment='center',
        transform=ax.transAxes)

ax.text(right, 0.5 * (bottom + top), 'centered',
        horizontalalignment='center',
        verticalalignment='center',
        rotation='vertical',
        transform=ax.transAxes)

ax.text(left, top, 'rotated\nwith newlines',
        horizontalalignment='center',
        verticalalignment='center',
        rotation=45,
        transform=ax.transAxes)

plt.axis('off')

plt.show()

在此處輸入圖像描述

seaborn軸水平圖

import seaborn as sns

# sample dataframe
flights = sns.load_dataset("flights")

fig, axe = plt.subplots(figsize=(6, 6))
g = sns.lineplot(data=flights, x="year", y="passengers", ax=axe)
g.text(1950, 500, 'flights with CI', size=15, color='purple')

在此處輸入圖像描述

seaborn 人物級情節

tips = sns.load_dataset('tips')

g = sns.relplot(data=tips, x="total_bill", y="tip", hue="day", col="time")

# iterate through each axes
for ax in g.axes.flat:
    
    ax.text(10, 9, "Who's Hungy?", size=15, color='purple')

在此處輸入圖像描述

一種解決方案是使用plt.legend函數,即使您不需要實際的圖例。 您可以使用loc關鍵字指定圖例框的位置。 可以在此網站上找到更多信息,但我還提供了一個示例,展示了如何放置圖例:

ax.scatter(xa,ya, marker='o', s=20, c="lightgreen", alpha=0.9)
ax.scatter(xb,yb, marker='o', s=20, c="dodgerblue", alpha=0.9)
ax.scatter(xc,yc marker='o', s=20, c="firebrick", alpha=1.0)
ax.scatter(xd,xd,xd, marker='o', s=20, c="goldenrod", alpha=0.9)
line1 = Line2D(range(10), range(10), marker='o', color="goldenrod")
line2 = Line2D(range(10), range(10), marker='o',color="firebrick")
line3 = Line2D(range(10), range(10), marker='o',color="lightgreen")
line4 = Line2D(range(10), range(10), marker='o',color="dodgerblue")
plt.legend((line1,line2,line3, line4),('line1','line2', 'line3', 'line4'),numpoints=1, loc=2) 

請注意,因為loc=2 ,所以圖例位於圖的左上角。 如果文本與繪圖重疊,您可以使用legend.fontsize使其更小,這將使圖例更小。

暫無
暫無

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

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