matplotlib 中来自同一循环的两个不同图? [英] Two different plots from same loop in matplotlib?

查看:68
本文介绍了matplotlib 中来自同一循环的两个不同图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我特别想使用一个循环来创建两个不同的图.一个图应该有来自 x-y 的四条直线,另一个图应该有来自 x-y2 的四条斜线.我的代码只在一个图中显示所有内容.我不太了解plt的工作原理,如何创建两个不同的plt对象?

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

推荐答案

这就是您想要做的吗?

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

这篇关于matplotlib 中来自同一循环的两个不同图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆