2015-06-05 64 views
2

我在數據透視表上聚合了一些時間序列信息,因此每個數據透視表都包含標記爲2015,2014等的列。我想比較每個數據透視表,因此我將它們繪製在相同的軸線:當在熊貓中繪製不同的數據框時更改繪圖標籤

print pv_test_A.columns 
Int64Index([2010, 2011, 2012, 2013, 2014, 2015], dtype='int64') 

print pv_test_B.columns 
Int64Index([2010, 2011, 2012, 2013, 2014, 2015], dtype='int64') 

fig, axes = plt.subplots() 
pv_test_A.loc[:,[2015]].plot(ax=axes) 
pv_test_B.loc[:,[2015]].plot(ax=axes) 
在這種情況下

,每條線將有圖上的相同的標籤,2015年我試圖傳遞label參數情節沒有影響。有沒有辦法重新命名標籤,而不必訴諸使用plt.legend或註釋?

回答

5

你可以使用.rename()

pv_test_A.loc[:,[2015]].rename(columns={2015: "New Label A"}).plot(ax=axes) 
pv_test_B.loc[:,[2015]].rename(columns={2015: "New Label B"}).plot(ax=axes) 

底層數據將被保留,但要繪製的列賦予不同的標籤用於繪圖的目的。