【问题标题】:How to plot multiple lines in single graph python如何在单个图形python中绘制多条线
【发布时间】:2015-09-28 17:26:48
【问题描述】:

我有一个三维数组。

第一个维度有 4 个元素。 第二个维度有 10 个元素。 第三个维度有 5 个元素。

我想绘制这个数组的内容如下。

第一个维度的每个元素都有自己的图表(页面上有四个图表) 第二维的值对应于图形的 y 值。 (每张图有 10 条线) 第三维的值对应图的x值(10条线每条有5个x值)

我对 python 还很陌生,甚至对图形也很陌生。 我想出了如何正确地用数据加载我的数组......而且我什至没有试图让“一页上的四个图表”方面工作。

现在我只想让一张图正常工作。 这是我到目前为止所拥有的(一旦我的数组设置好了,并且我已经正确加载了我的数组。现在图表显示了,但它是空白的,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()

任何帮助将不胜感激!

编辑。进一步澄清。假设我的数组加载如下:

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

我希望有一条线,y 值为 22、10、15、25、13,x 值为 1、2、3、4、5(因为它的索引为 0,我可以在之前 +1打印标签)

那么,假设我有

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

我希望这是另一行,遵循与第一行相同的规则。

【问题讨论】:

  • 您的数据是否以 (x,y) 对的形式存在,或者 x 或 y 坐标是否与索引相同?您能否提供一些示例数据和一些关于您想要绘制的内容的背景信息?说quadnumcounts[0][3] 是列表[7.1, 6.5, 4.7, 2.3, 9.8]。是否要通过点 (0, 7.1)(1, 6.5)、...、(4, 9.8) 绘制一条线?
  • 添加了一些说明。这有帮助吗?

标签: python multidimensional-array graph


【解决方案1】:

以下是如何制作 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()

【讨论】:

    【解决方案2】:

    这是创建数据子图的示例。您没有提供数据集,所以我使用x 作为从0360 度的角度,y 作为x 的三角函数(正弦和余弦)。

    代码示例:

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

    输出:

    更新: 使用您在更新中提供的示例日期,您可以进行如下操作:

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

    请注意,尺寸与您的略有不同。在这里,它们是mydata[0][0] == 10mydata[1][3] == 25 等。输出如下所示:

    【讨论】:

    • 这正是我想要的!现在我只需要知道如何在每张图上放置 10 条线。
    猜你喜欢
    • 2023-03-25
    • 2013-08-01
    • 2020-09-15
    • 2019-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-06
    相关资源
    最近更新 更多