6

I would specifically like to create two different plots using one single loop. One plot should have four straight lines from x-y, and another plot should have four angled lines from x-y2. My code only shows everything in a single plot. I don't quite understand how plt works, how can I create two distinct plt objects?

import matplotlib.pyplot as plt
import matplotlib.pyplot as plt2

x=[[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]]
y=[[1,2,3,4],[2,3,4,5],[3,4,5,6],[7,8,9,10]]
y2=[[11,12,13,24],[42,33,34,65],[23,54,65,86],[77,90,39,54]]
colours=['r','g','b','k']

for i in range(len(x)):
   plt.plot(x[i],y2[i],colours[i])
   plt2.plot(x[i],y[i],colours[i])

plt.show()
plt2.show()
1
  • create two separate figure objects e.g. by fig1, ax1 = plt.subplots(), fig2, ax2 = plt.subplots(), then plot with ax1.plot and ax2.plot Commented Oct 23, 2015 at 18:40

1 Answer 1

10

Is that what you want to do?

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],[2,3,4,5],[3,4,5,6],[7,8,9,10]]
y2=[[11,12,13,24],[42,33,34,65],[23,54,65,86],[77,90,39,54]]
colours=['r','g','b','k']

fig1, ax1 = plt.subplots()
fig2, ax2 = plt.subplots()
for i in range(len(x)):
    ax1.plot(x[i],y2[i],colours[i])
    ax2.plot(x[i],y[i],colours[i])

fig1.show()
fig2.show()
Sign up to request clarification or add additional context in comments.

1 Comment

Can you explain how to best add title and legends in this loop?

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.