0

I want to have two plots (#1, #2). I want to update the plot #1 as data is coming up, and then update plot #2 as well. After this, I might want to go back and update plot #1. However, I am unable to update plot #1. Any help?

Here are my source code.

import matplotlib.pyplot as plt
import numpy as np
a = np.random.random(10)
b = np.random.random(10)

plt.subplot(311)
for i in range (10):
    y = a[i]
    x = b[i]
    plt.scatter(x,y)
    plt.pause(0.05)



plt.subplot(312)
for i in range (10):
    x = a[i]
    y = b[i]
    plt.scatter(x,y)
    plt.pause(0.05)

# update plot #1, but this doesn't work
aa = np.random.random(20)
bb = np.random.random(20)
plt.subplot(311)
for i in range (20):
    y = aa[i]
    x = bb[i]
    plt.scatter(x,y)

plt.show()

1 Answer 1

1

plt.subplot is for creating new subplots. If you want to access different subplots not only directly after creating them, choose

fig, axs = plt.subplots(2)

for creating the figure with all subplots first. Then you can plot with

axs[0].plot(...)
axs[1].plot(...)
axs[0].plot(...)

The same works with plt.subplots(nrows, ncols) for a 2D-array of plots. Then axs is also a 2D-array.

Besides that, plt.subplots has a very convenient way to define shared axes via the kwargs sharex and sharey, which can be one of ['all', 'row', 'col', 'none'].

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.