将一维数组与多维数组作图?

0 投票
1 回答
657 浏览
提问于 2025-04-18 18:00

考虑以下代码:

   import matplotlib.pyplot as plt
   x = [[512],[256],[1024],[2048],[4096],[8192],[32768],
       [65536],[131072]]
   y =[[900,1800],[ 600, 900, 1800, 2700,3600],
      [1800],[900,1800,3600,6000,7200,10800,12000,14400,28800],
      [900, 1800, 3600, 5400, 6000, 7200, 36000],
      [600, 900, 1200, 1800, 2700, 3600, 5400, 6000, 7200, 9600, 18000, 36000],
      [1800, 2700, 3600, 4800, 5400, 6000, 7200, 10800, 18000],
      [1800, 2700, 3600, 5400, 7200, 10800, 21600],[900, 1800, 3600, 7200]]

   fig = plt.figure()

   graph = fig.add_subplot(111)
   for i in range(len(x)):
     plt.plot(x[i], y[i], color='blue', lw=1, marker='s')
   plt.title('userid_22')
   plt.xlabel('Processors')
   plt.ylabel('Est_runtime')

   plt.savefig('processor-esttimeanalysis4.png')
   plt.show()

我遇到了以下错误:

    ValueError: x and y must have same first dimension

如果有人能建议我怎么正确绘制它,那就太好了!

1 个回答

1

你没有具体说明你想要它做什么,但也许你是想要这个。

   import matplotlib.pyplot as plt
   x = [[512],[256],[1024],[2048],[4096],[8192],[32768],
       [65536],[131072]]
   y =[[900,1800],[ 600, 900, 1800, 2700,3600],
      [1800],[900,1800,3600,6000,7200,10800,12000,14400,28800],
      [900, 1800, 3600, 5400, 6000, 7200, 36000],
      [600, 900, 1200, 1800, 2700, 3600, 5400, 6000, 7200, 9600, 18000, 36000],
      [1800, 2700, 3600, 4800, 5400, 6000, 7200, 10800, 18000],
      [1800, 2700, 3600, 5400, 7200, 10800, 21600],[900, 1800, 3600, 7200]]

   fig = plt.figure()

   graph = fig.add_subplot(111)
   for x_value, y_value in zip(x, y):
       plt.plot(x_value * len(y_value), y_value, color='blue', lw=1, marker='s')
   plt.title('userid_22')
   plt.xlabel('Processors')
   plt.ylabel('Est_runtime')

   plt.savefig('processor-esttimeanalysis4.png')
   plt.show()

撰写回答