2017-06-28 65 views
1

我想配置y軸以使值更易於解釋。 I would like to configure the y-axis so the values are more interpretable. Preferably I would have 'mean_Kincaid' on the left y-axis and 'mean_Score' on the right y-axis.將兩個y軸刻度添加到同一個圖表

這是我試過的代碼:

ax = Statements.plot(x='Date', y='mean_Kincaid', legend=True, 
title="Fed Statement Kincaid and Sentiment scores over time") 
Statements.plot(x='Date', y='mean_Score', ax=ax) 
Statements.plot(secondary_y='mean_Kincaid', style='g', ax=ax) 

Resulting in:

當運行:

ts = Statements.set_index('Date') 
ts.mean_Kincaid.plot() 
ts.mean_Score.plot(secondary_y=True) 

我得到:

enter image description here

這與我的原始圖大不相同。這是什麼原因造成的,我該如何解決?

+1

https://pandas.pydata.org/pandas-docs/stable/visualization.html#plotting -on-a-secondary-y-axis? – MaxU

回答

1

您正在尋找secondary_y關鍵字參數。

ts = Statements.set_index('Date') 
ts.mean_Kincaid.plot() 
ts.mean_Score.plot(secondary_y=True) 

如果你想顯示圖例和軸標籤,使用類似

ts = Statements.set_index('Date') 
ax = ts.mean_Kincaid.plot(legend=True) 
ts.mean_Score.plot(legend=True, secondary_y=True) 
ax.set_ylabel('mean_Kincaid') 
ax.right_ax.set_ylabel('mean_Score') 
+0

謝謝,但這似乎扭曲了我的數據。我已經更新了我的問題,你知道發生了什麼嗎?再次感謝 –

+0

我想它是以不同的比例繪製線條。我如何將磅秤標記爲適當的變量?此外,如果你知道如何改變顏色(或許是一條藍色虛線而不是橙色線)並添加一個很棒的傳奇!謝謝! –

+1

@Graham我已經添加了一個示例,顯示如何標記軸並添加圖例。我不確定你的意思是「扭曲我的數據」。你的情節對我來說很好看,這兩個系列只是在不同的尺度上,這是一個輔助軸的整個點。 –