简体   繁体   English

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

[英]How to label y-axis when using a secondary y-axis?

I am trying to label both y-axis, one to say "WLL", the other to say "S&P 500".我试图标记两个 y 轴,一个表示“WLL”,另一个表示“S&P 500”。 Right now, I can only label the secondary y-axis (S&P 500).现在,我只能标记辅助 y 轴(标准普尔 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()

只需在set_ylabel()之前添加 right_ax , set_ylabel()所示:

ax.right_ax.set_ylabel()

This can be achieved be setting the label before plotting the secondary y-axis .这可以通过在绘制辅助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)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM