Rui Martins
Rui Martins

Reputation: 3858

Draw and refresh two independent windows with pyplot

I want to draw two graphs in two different windows, but I want the graphics that may be dynamically updated.

This code is an example.

Why does it only draws on one of the windows?

How to solve?

Thank you

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
fig_norm = plt.figure()
fig.show()
fig_norm.show()
ax_fig = fig.add_subplot(1, 1, 1)
ax_fig_norm = fig.add_subplot(1, 1, 1)
ax_fig.set_title("Figure 1", fontsize='large')
ax_fig_norm.set_title("Figure Normalized", fontsize='large')

plt.ion()

while True:
    x = np.random.rand(100)
    y = np.random.rand(100)
    ax_fig.plot(x, y)
    ax_fig_norm.plot(x*3, y*3)
    fig.canvas.draw()
    fig_norm.canvas.draw()

enter image description here

Upvotes: 0

Views: 112

Answers (1)

Olivier DUPOUY
Olivier DUPOUY

Reputation: 66

The method add_subplot creates an Axes object on the figure which its called on.

You call it on fig twice, so obtain the same Axes object.

Please, try:

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
fig_norm = plt.figure()
fig.show()
fig_norm.show()
ax_fig = fig.add_subplot(1, 1, 1)
ax_fig_norm = fig_norm.add_subplot(1, 1, 1)
ax_fig.set_title("Figure 1", fontsize='large')
ax_fig_norm.set_title("Figure Normalized", fontsize='large')

plt.ion()

while True:
    x = np.random.rand(100)
    y = np.random.rand(100)
    ax_fig.plot(x, y)
    ax_fig_norm.plot(x*3, y*3)
    fig.canvas.draw()
    fig_norm.canvas.draw()

Upvotes: 1

Related Questions