【问题标题】:Two different plots from same loop in matplotlib?matplotlib中同一循环的两个不同的图?
【发布时间】:2016-01-23 09:33:59
【问题描述】:

我特别想使用一个循环创建两个不同的图。一个图应该有四条来自 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()

【问题讨论】:

  • 创建两个单独的图形对象,例如通过fig1, ax1 = plt.subplots()fig2, ax2 = plt.subplots(),然后使用ax1.plotax2.plot 进行绘图

标签: python python-2.7 matplotlib


【解决方案1】:

这是你想做的吗?

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

【讨论】:

  • 你能解释一下如何在这个循环中最好地添加标题和图例吗?
猜你喜欢
  • 2014-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-13
  • 2020-07-20
  • 2023-03-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多