0

我正在尝试在一组轴上绘制多个图表。

我有一个二维数据数组,想将其分解为 111 个一维数组并绘制它们。到目前为止,这是我的代码示例:

from numpy import *
import matplotlib.pyplot as plt

x = linspace(1, 130, 130) # create a 1D array of 130 integers to set as the x axis
y = Te25117.data # set 2D array of data as y
plt.plot(x, y[1], x, y[2], x, y[3]) 

这段代码工作正常,但我看不到一种编写循环的方法,该循环将在情节本身内循环。如果我每次都明确地写一个数字 1 到 111,我只能使代码工作,这并不理想!(我需要循环的数字范围是 1 到 111。)

4

2 回答 2

3

让我猜猜……长期使用 matlab 用户?如果您不创建新的,Matplotlib 会自动将线图添加到当前图。所以你的代码可以很简单:

from numpy import *
import matplotlib.pyplot as plt

x = linspace(1, 130, 130) # create a 1D array of 130 integers to set as the x axis
y = Te25117.data # set 2D array of data as y
L = len(y) # I assume you can infere the size of the data in this way...
#L = 111 # this is if you don't know any better
for i in range(L)
    plt.plot(x, y[i], color='mycolor',linewidth=1) 
于 2012-12-10T11:20:36.173 回答
0
import numpy as np
import matplotlib.pyplot as plt
x = np.array([1,2])
y = np.array([[1,2],[3,4]])

In [5]: x
Out[5]: array([1, 2])

In [6]: y
Out[6]: 
array([[1, 2],
       [3, 4]])

In [7]: for y_i in y:
  ....:     plt.plot(x, y_i)

将这些绘制在一个图中。

于 2012-12-10T11:20:44.863 回答