1

I come across the answer from this forum and have a question. How to show separate plot in one plot. Try to use plt.subplots(1, 4) but not work. Here is the sample codes:

import matplotlib.pyplot as plt
x=[[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]]
y=[[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]]
for i in range(len(x)):
    plt.figure()
    plt.plot(x[i],y[i])
    

#plt.show()
1
  • Could you provide any sample of what do you want to get? Maybe a grid with 4 subplots? Commented Dec 3, 2020 at 20:23

2 Answers 2

1

You can use plt.subplot() as following:

import matplotlib.pyplot as plt
x=[[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]]
y=[[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]]
plt.figure(figsize=[10,10])

for i in range(len(x)):
    plt.subplot(1,len(x)+1,i+1)
    plt.plot(x[i],y[i])
    plt.title('Plot: '+str(i+1))
plt.show()

Here is output figure:

Here is output figure

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

Comments

0

The following may do it. It is looping through the lists (x and y have 4 nested lists) and subplots (in this example 2 by 2) using zip.

x=[[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]]
y=[[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]]

fig = plt.figure(figsize=(10, 6))

for i,j in zip(list(range(0,4)), list(range(1,5)) ):
    ax = fig.add_subplot(2,2,j)
    ax.plot(x[i], y[i])

1 Comment

Thanks. Learn new thing. "zip".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.