Home > Net >  Unable to show legend on the graph for each subplot
Unable to show legend on the graph for each subplot

Time:11-29

I am trying to show the legend for each subplot but it doesn't seem like working. There is a box on the upper right of the subplot but it's empty. This is my code which I have tried. Also is there anyway I can enlarge the first subplot?

XWD_TO = data.iloc[:,0:1]
VAS_AX = data.iloc[:,1:2]
BTC_AUD = data.iloc[:,2:]

# Daily data chart of XWD.TO and VAS.AX (need to fix legend)
#fig = plt.figure()
fig, (ax1, ax2) = plt.subplots(2, sharex=True, sharey=True)
fig.suptitle('Daily Volatility')
ax1 = plt.subplot(211)
ax1.set_title("XWD.TO and VAS.AX")
ax1.plot(XWD_TO,c='orange')
ax1.plot(VAS_AX,c='green')
ax1.legend(loc = "upper right")
ax2 = plt.subplot(212)
ax2.set_title("BTC-AUD")
ax2.plot(BTC_AUD,c='blue')
plt.show()

CodePudding user response:

You need to label each subplot. I have edited your code a bit. Hope it helps.

XWD_TO = data.iloc[:,0:1]
VAS_AX = data.iloc[:,1:2]
BTC_AUD = data.iloc[:,2:]

# Daily data chart of XWD.TO and VAS.AX (need to fix legend)
#fig = plt.figure()
fig, (ax1, ax2) = plt.subplots(2, sharex=True, sharey=True)
fig.suptitle('Daily Volatility')
ax1 = plt.subplot(211)
ax1.set_title("XWD.TO and VAS.AX")
ax1.plot(XWD_TO,c='orange',label='XWD_TO')
ax1.plot(VAS_AX,c='green',label='VAS_AX'))
ax1.legend(loc = "upper right")
ax2 = plt.subplot(212)
ax2.set_title("BTC-AUD")
ax2.plot(BTC_AUD,c='blue',label='BTC_AUD')
ax2.legend(loc = "upper right")
plt.show()
  • Related