matplotlib中来自同一循环的两个不同的图?

时间:2015-10-23 18:37:30

标签: python python-2.7 matplotlib

我特别想使用一个循环创建两个不同的图。一个图应该有来自x-y的四条直线,而另一个图应该有来自x-y2的四条成角度的线。我的代码只显示一个图中的所有内容。我不太明白plt是如何工作的,我怎样才能创建两个不同的plt对象?

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 个答案:

答案 0 :(得分:7)

这就是你想要做的吗?

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()