2016-03-22 28 views
4

我在plot命令中使用了label=None,在這種情況下matplotlib選擇了數據的關鍵字作爲標籤。我發現這種行爲不直觀,並且在我明確設置標籤時,預計會完全控制標籤。 如何禁用涉及pandas數據幀的數據的圖的標籤?繪製熊貓數據時如何禁用標籤?

這裏是一個小例子,這說明行爲:

import matplotlib.pyplot as plt 
import pandas as pd 

df = pd.DataFrame({'x': [0, 1], 'y': [0, 1]}) 
plt.plot(df['x'], df['y'], label=None) 
plt.legend(loc='best') 

這將導致以下情節:

enter image description here

在它的事項的情況下,我目前使用matplotlib版本1.5.1和pandas版本0.18.0在從macports安裝的以下python: Python 2.7.11(默認,2016年3月1日,18:40:10) [GCC 4.2.1 Compatible Apple LLVM 7.0.2(clang- 700.1.81)]在達爾文

+0

你可以傳遞一個空字符串:'plt.plot(df ['x'],df ['y'],label ='')' – EdChum

+0

你可以發表這個答案,我可以接受它嗎? –

回答

3

如果你傳遞一個空字符串,然後它會導致該地塊:

import matplotlib.pyplot as plt 
import pandas as pd 

df = pd.DataFrame({'x': [0, 1], 'y': [0, 1]}) 
plt.plot(df['x'], df['y'], label='') 
plt.legend(loc='best') 

enter image description here

1

只需傳遞一個空標籤。 Matplotlib在這種情況下不會打擾圖例輸入。參數label=None是繪圖函數檢測到的默認值,告訴它構成一個標籤。

(當然,在這種情況下,你可能只是不能使用plt.legend ...)