简体   繁体   English

如何在单个图形python中绘制多条线

[英]How to plot multiple lines in single graph python

I have a three-dimensional array. 我有一个三维数组。

The first dimension has 4 elements. 第一维包含4个元素。 The second dimension has 10 elements. 第二维包含10个元素。 The third dimension has 5 elements. 第三维具有5个元素。

I want to plot the contents of this array as follows. 我想按以下方式绘制此数组的内容。

Each element of the first dimension gets its own graph (four graphs on the page) The values of the second dimension correspond to the y values of the graphs. 第一个维度的每个元素都有自己的图形(页面上有四个图形)。第二个维度的值对应于图形的y值。 (there are 10 lines on each graph) The values of the third dimension correspond to the x values of the graphs (each of the 10 lines has 5 x values) (每个图形上有10条线)三维尺寸的值对应于图形的x值(10条线中的每条都有5个x值)

I'm pretty new to python, and even newer to graphing. 我是python的新手,甚至在图形学方面还是新手。 I figured out how to correctly load my array with the data...and I'm not even trying to get the 'four graphs on one page' aspect working. 我想出了如何正确地用数据加载数组的方法……我什至没有试图使“一页上的四个图”方面正常工作。

For now I just want one graph to work correctly. 现在,我只希望一张图能够正常工作。 Here's what I have so far (once my array is set up, and I've correctly loaded my arrays. Right now the graph shows up, but it's blank, and the x-axis includes negative values. None of my data is negative) 到目前为止,这是我的所有信息(一旦设置好数组,并正确加载了数组。现在图形显示出来,但是它是空白的,并且x轴包含负值。我的数据都不为负)

for n in range(1):
  for m in range(10):
      for o in range(5):
        plt.plot(quadnumcounts[n][m][o])
        plt.xlabel("Trials")
        plt.ylabel("Frequency")
  plt.show()

Any help would be really appreciated! 任何帮助将非常感激!

Edit. 编辑。 Further clarification. 进一步澄清。 Let's say my array is loaded as follows: 假设我的数组加载如下:

myarray[0][1][0] = 22
myarray[0][1][1] = 10
myarray[0][1][2] = 15
myarray[0][1][3] = 25
myarray[0][1][4] = 13

I want there to be a line, with the y values 22, 10, 15, 25, 13, and the x values 1, 2, 3, 4, 5 (since it's 0 indexed, I can just +1 before printing the label) 我希望有一行,y值分别为22、10、15、25、13,x值为1、2、3、4、5(由于索引为0,因此我可以在打印标签之前+1 )

Then, let's say I have 然后,假设我有

myarray[0][2][0] = 10
myarray[0][2][1] = 17
myarray[0][2][2] = 9
myarray[0][2][3] = 12
myarray[0][2][4] = 3

I want that to be another line, following the same rules as the first. 我希望这是另一条线,遵循与第一条相同的规则。

Here is the example of creating subplots of your data. 这是创建数据子图的示例。 You have not provided the dataset so I used x to be an angle from 0 to 360 degrees and the y to be the trigonemetric functions of x (sine and cosine). 您尚未提供数据集,因此我使用x表示从0360度的角度,使用y表示x的三角函数(正弦和余弦)。

Code example: 代码示例:

import numpy as np
import pylab as plt

x = np.arange(0, 361)  # 0 to 360 degrees
y = []
y.append(1*np.sin(x*np.pi/180.0))
y.append(2*np.sin(x*np.pi/180.0))
y.append(1*np.cos(x*np.pi/180.0))
y.append(2*np.cos(x*np.pi/180.0))

z = [[x, y[0]], [x, y[1]], [x, y[2]], [x, y[3]]]  # 3-dimensional array


# plot graphs
for count, (x_data, y_data) in enumerate(z):
    plt.subplot(2, 2, count + 1)
    plt.plot(x_data, y_data)
    plt.xlabel('Angle')
    plt.ylabel('Amplitude')
    plt.grid(True)

plt.show()

Output: 输出:

在此处输入图片说明

UPDATE: Using the sample date you provided in your update, you could proceed as follows: 更新:使用您在更新中提供的示例日期,可以执行以下操作:

import numpy as np
import pylab as plt

y1 = (10, 17, 9, 12, 3)
y2 = (22, 10, 15, 25, 13)
y3 = tuple(reversed(y1))  # generated for explanation
y4 = tuple(reversed(y2))  # generated for explanation
mydata = [y1, y2, y3, y4]

# plot graphs
for count, y_data in enumerate(mydata):
    x_data = range(1, len(y_data) + 1)
    print x_data
    print y_data
    plt.subplot(2, 2, count + 1)
    plt.plot(x_data, y_data, '-*')
    plt.xlabel('Trials')
    plt.ylabel('Frequency')
    plt.grid(True)

plt.show()

Note that the dimensions are slightly different from yours. 请注意,尺寸与您的尺寸略有不同。 Here they are such that mydata[0][0] == 10 , mydata[1][3] == 25 etc. The output is show below: 它们是mydata[0][0] == 10mydata[1][3] == 25等。输出如下所示: 在此处输入图片说明

Here's how to make the 4 plots with 10 lines in each. 这是制作4个绘图的方法,每个绘图有10条线。

import matplotlib.pyplot as plt
for i, fig_data in enumerate(quadnumcounts):
    # Set current figure to the i'th subplot in the 2x2 grid
    plt.subplot(2, 2, i + 1)
    # Set axis labels for current figure
    plt.xlabel('Trials')
    plt.ylabel('Frequency')
    for line_data in fig_data:
        # Plot a single line
        xs = [i + 1 for i in range(len(line_data))]
        ys = line_data
        plt.plot(xs, ys)
# Now that we have created all plots, show the result
plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM