How to designate the y axis when using the secondary y axis?

I am trying to designate both the Y axis and the "WLL", and the other the "S & P 500". Right now I can only designate the secondary Y axis (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()
+6
source share
2 answers

This can be achieved by setting a label before building the secondary 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

+4
source

just add right_ax before set_ylabel () ax.right_ax.set_ylabel ()

+1
source

Source: https://habr.com/ru/post/1610632/


All Articles