Python: draw multiple figures in separate windows (same data but with different X range)

2.2k views Asked by At

I wonder how to plot data in separate figures (not multiple figures in one window). The issue is that I want to visualize the data along different X range. For example:

import numpy as np
import pandas as pd
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
X=np.linspace(0,100,num=1000)
Y=X**2-X
fig=plt.figure()
plt.plot(X,Y)
matplotlib.pyplot.xlim([0, 50])
plt.show()
fig=plt.figure()
plt.plot(X,Y)
matplotlib.pyplot.xlim([50, 100])
plt.show()

Here I get two separate figures. But I did the plt.plot() for the same data repeatedly. It can take time if the data is big.

1

There are 1 answers

0
ImportanceOfBeingErnest On BEST ANSWER

You can limit the range of data to plot, not only the viewing interval. To this end you may introduce a condition and filter the arrays to plot by that condition.

import numpy as np
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt

X=np.linspace(0,100,num=1000)
Y=X**2-X

ranges = [[0, 50], [50, 100]]

for r in ranges:
    cond = (X >= r[0]) & (X <= r[1])
    fig=plt.figure()
    plt.plot(X[cond],Y[cond])
    plt.xlim(r)

plt.show()

enter image description here