如何在使用辅助y轴时标记y轴?

时间:2015-10-07 18:18:23

标签: python pandas

我试图标记两个y轴,一个标记" WLL"另一个标记" S& P 500"。现在,我只能标记辅助y轴(S& P 500)。

import pandas as pd
from pandas import DataFrame
from matplotlib import pyplot as plt
import pandas.io.data as web
import datetime as dt

start = '2013-01-01'
end = dt.datetime.today()

df = web.DataReader('WLL', 'yahoo', start, end)
sp = web.DataReader('^GSPC', 'yahoo', start, end)

fig, ax1 = plt.subplots()
df['Close'].plot(ax=ax1,color='g',linewidth=1.0)
sp['Close'].plot(secondary_y=True, ax=ax1,color='b',linewidth=1.0)
ax = df['Close'].plot(); sp['Close'].plot(ax=ax, secondary_y=True)

plt.xlabel('xlabel', fontsize=10)
plt.ylabel('ylabel', fontsize=10)

plt.show()

2 个答案:

答案 0 :(得分:4)

这可以通过在绘制辅助y-axis之前设置标签来实现。

fig, ax1 = plt.subplots()
df['Close'].plot(ax=ax1, color='g', linewidth=1.0)
sp['Close'].plot(secondary_y=True, ax=ax1, color='b', linewidth=1.0)
ax = df['Close'].plot(); 
ax.set_ylabel('WLL', fontsize=10);
sp['Close'].plot(ax=ax, secondary_y=True);

plt.xlabel('xlabel', fontsize=10)
plt.ylabel('S&P 500', fontsize=10, rotation=-90)

enter image description here

答案 1 :(得分:1)

只需在set_ylabel()之前添加right_ax ax.right_ax.set_ylabel()