簡體   English   中英

Python 3.2 Matplotlib中為圖添加圖例的錯誤

[英]error of adding a legend for a plot in Python 3.2 Matplotlib

我需要在Python 3.2 Matplotlib中為圖添加圖例。

但是,添加“ black_dash”后無法顯示圖例

我的代碼:

   blue_line, = plt.plot([0, 1], [-3, -3], color='b', linestyle='-',    linewidth=1)

   black_dash, = plt.plot([0, 1], [-7, -7], color='k', linestyle='--', linewidth=1)

   plt.legend([blue_line, black_dash] , ["boundary of reg_zip", "lat/lon line"] , loc='upper center', bbox_to_anchor=(0.5, -0.5), \
           fancybox=True, shadow=True, ncol=5, fontsize=11)

圖例應包含兩行及其說明。

更新:

我需要在圖例中插入一個填充的黑色圓圈,但出現錯誤:

 File "C:\Python32\lib\site-packages\matplotlib\backends\backend_agg.py", line 146, in draw_path

self._renderer.draw_path(gc, path, transform, rgbFace)

**TypeError: float() argument must be a string or a number**

我的代碼:

plt.plot([0, 1], [-3, -3], "ro", ms=10, mfc="k", mew=2, mec="k", label="boundary of reg_zip")

謝謝

這是應該工作的

plt.plot([0, 1], [-3, -3], color='b', linestyle='-', linewidth=1, label="blue line")
plt.plot([0, 1], [-7, -7], color='k', linestyle='--', linewidth=1, label="black dash")
plt.legend(loc='upper center', fancybox=True, shadow=True, ncol=5, fontsize=11)
plt.show()

因此,基本上,在行中添加標簽,而不是圖例,圖例需要通過名稱識別對象,如果您不對它們進行標簽,則圖例就不能(這也會自動更改圖例中的行以適合當前外觀)。

還請務必檢查您的y軸范圍。 它經常嘗試自動調整它們的大小,並且使用水平線經常會失敗並將它們放置在圖形的最邊緣。 它們在那里,您看不到它們!

編輯1:

既然我能看到你對此感到困惑。 我做了幾個情節。 第一個是文本(通常是其他任何框)。 其次是傳說,其位置由loc關鍵字確定。 第三是傳說的立場由bbox_to_anchor確定。 請注意,文本框與圖例框不對應。 主要原因是bbox_to_anchor錨定了圖例的右上角,而文本錨定了框的左下角。

還要注意loc關鍵字如何不像bbox_to_anchor那樣取決於圖形縮放。 要擺脫這種討厭的習慣,您必須通過執行以下操作為bbox_to_anchor聲明一個轉換:

plt.legend(bbox_to_anchor=(1, 1),
       bbox_transform=plt.gcf().transFigure)

圖例手冊中所述

此外,如果您的圖例甚至不適合交互式繪圖屏幕中的灰色圖區域,則必須選擇“配置子圖”圖標,然后更改值,直到可以再次找到圖例。

文本框的位置

通過<code> loc </ code>關鍵字的圖例位置

在此處輸入圖片說明

同樣重要的是要意識到,通過使用bbox_to_anchor向圖例添加loc關鍵字bbox_to_anchor沒有區別。 bbox_to_anchor將踐踏您提供圖例的所有其他位置。

現在,我要說的是,如果您不願意閱讀手冊並更深入地研究matplotlib實現,那么您不應該真正介入bbox_to_anchor選項,但我確實建議在所有情況下都避免使用bbox_to_anchor ,您的圖表太擁擠了,您必須將其放置在外面。 (在哪種情況下是考慮圖形設計的好時機?)

最后,這是從上方繪制圖形的代碼。

import matplotlib.pyplot as plt

plt.plot((0,0), (1,1), label="legend")

legends = []
for i in range(0, 11):
    legends.append(plt.legend([str(i)], loc=i))

for legend in legends:
    plt.gca().add_artist(legend)
#legends with loc=5 and 7 overlap
plt.show()


plt.plot((0,1), (0,1), label="legend")

legend1 = plt.legend(["0,0"], bbox_to_anchor=(0, 0))
legend3 = plt.legend(["1,1"], bbox_to_anchor=(1, 1))
legend2 = plt.legend(["0.5,0.5"], bbox_to_anchor=(0.5, 0.5))
legend4 = plt.legend(["0.5,0"], bbox_to_anchor=(0.5, 0))
legend6 = plt.legend(["0,0.5"], bbox_to_anchor=(0, 0.5))
legend5 = plt.legend(["1,0.5"], bbox_to_anchor=(1, 0.5))
legend7 = plt.legend(["0.5,1"], bbox_to_anchor=(0.5, 1))
legend8 = plt.legend(["1,0"], bbox_to_anchor=(1, 0))
legend9 = plt.legend(["0,1"], bbox_to_anchor=(0, 1))

plt.gca().add_artist(legend1)
plt.gca().add_artist(legend2)
plt.gca().add_artist(legend3)
plt.gca().add_artist(legend4)
plt.gca().add_artist(legend5)
plt.gca().add_artist(legend6)
plt.gca().add_artist(legend7)
plt.gca().add_artist(legend8)
plt.gca().add_artist(legend9)

plt.show()

暫無
暫無

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

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