0

I'd like to have two plots stacked up. What I did so far is I'm plotting live data this way:

    def animate(i):
    
        plt.tick_params(axis='y', which='both', labelleft=False, labelright=True)
        plt.cla()
        plt.plot(df.time, df.close)
        plt.plot(df.time, df.sma)   
        plt.plot(df.time, df.price_line, 'g-')    
        plt.title('ETHUSDT')
    
ani = FuncAnimation(plt.gcf(), animate, interval = 200)
        
plt.tight_layout()
    
plt.pause(0.001)

Result:
enter image description here

Now I add a sublot this way:

vola_ax = plt.subplot(2,1,1)
vola_ax.plot(df.time, df.volatility)

But the when I run the code I get first this: enter image description here

Then it turns to this:

enter image description here

But my desired output is (roughly)this:

enter image description here

How can I achieve this?

1
  • Please attach a complete example Commented Apr 6, 2021 at 11:33

2 Answers 2

1

Try creating the figure and axes in the beginning:

def animate(i):
    
    ax0, ax1 = axs
    
    plt.cla()
    
    ax0.plot(df.time, df.close)
    ax0.plot(df.time, df.sma)   
    # ax0.plot(df.time, df.price_line, 'g-')    
    ax0.set_title('ETHUSDT')
    ax0.tick_params(axis='y', which='both', labelleft=False, labelright=True)

    ax1.plot(df.time, df.volatility)

fig, axs = plt.subplots(2, 1)

ani = FuncAnimation(fig, animate, interval = 200)
        
plt.tight_layout()
    
plt.pause(0.001)
Sign up to request clarification or add additional context in comments.

3 Comments

ax0 and ax1 are visible, but also an error is showing up. 'AttributeError: 'DataFrame' object has no attribute 'price_line'' ´ . the y axis values are on the left side from the ax0, should be on hte right side. ax1 has no data, only the pane
I edited so that the tick params are applied to the interested axes. The error you are getting is not related to the plotting. it's just your dataframe not having a 'price_line' column. Check your data or provide a complete example. If the solution for the plotting problem works, please consider accepting the answer, thanks.
something's still wrong. With each update (interval) there's a new window popping up
0

I would recommend creating the two subplots separately when instantiating the figure and plotting the data on each ax object separately, as follows:

import matplotlib.pyplot as plt
nrow = 2; ncol = 1;
fig, axs = plt.subplots(figsize=(16, 16), nrows=nrow, ncols=ncol)
ax = axs[0] # ie. the top subplot
df1.plox(ax=ax) # Apply FuncAnimation here as necessary

ax = axs[1] # ie. the bottom subplot
df2.plot(ax=ax) # Apply FuncAnimation here as necessary

# You can then apply common formatting through an iterator, eg common x limits:
for ax in axs:
   ax.set_xlim([0, 200])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.