Ghost plot being generated

Viewed 110

I have the following code:

fig = plt.figure()
ax1 = plt.subplot(211)
ax2 = plt.subplot(212, sharex = ax1)
ax2 = ax1.twinx()
num =  list111.lt(-90).sum(1)
plt.yticks(fontsize = 25)
ax = num.plot(figsize=(45,25), ax=ax2, color = 'Red')
df2.plot(y = 'Close', figsize=(45,25), ax=ax1, color = 'Green')
ax1.grid()
ax.margins(x=0)

I am trying to plot ax1 and ax2 in the same graph. What i am getting is a ghost plot:

enter image description here

How can i get rid of the second ghost plot and move the x axis with label to the top plot?

1 Answers

The statement

ax2 = plt.subplot(212, sharex = ax1)

generates a subplot located beneath the ax1 subplot. But it is in contradiction with the statement

ax2 = ax1.twinx()

which points towards a secondary y-axis on the ax1 axes.

If you want all the data to be plotted only on a single axes, you can delete the first statement and use the .twinx() method:

ax1 = plt.axes()
ax2 = ax1.twinx()
# remaining code

otherwise, use both axes separately with

ax1 = plt.subplot(211)
ax2 = plt.subplot(212, sharex = ax1)
# remaining code
Related