解压缩NumPy文件中的矩阵并绘制它们。

huangapple go评论104阅读模式
英文:

Unpack matrices in numpy file and plot them

问题

I need a code that reads a .npy file containing a set of arrays and creates a scatter plot using numpy and matplotlib. The first column need to be the x cordinates and the second column are the y cordinates.

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. # Load the .npy file
  4. a = np.load("filename.npy")
  5. # Extract x and y coordinates
  6. x = a[:, :, 0]
  7. y = a[:, :, 1]
  8. # Create a scatter plot
  9. plt.scatter(x, y)
  10. plt.xlabel('X Coordinates')
  11. plt.ylabel('Y Coordinates')
  12. plt.show()
英文:

I need a code that reads a .npy file containing a set of arrays and creates a scatter plot using numpy and matplotlib. The first column need to be the x cordinates and the second column are the y cordinates

ex:
a = np.load("filename.npy")

  1. a
  2. ([[[19.3, 117.88],
  3. [20.77, 118.99],
  4. [18.92, 117.66],
  5. [16.23, 115.67]],
  6. [[16.335, 113.789],
  7. [17.876, 116.8],
  8. [22.76, 115.34],
  9. [23.33, 111.45]],
  10. [[22.56, 113.76],
  11. [21.6, 118.07],
  12. [18.3, 116.60],
  13. [21.739, 117.903]]])

答案1

得分: 1

只需切片“columns”(实际上是第三维度),然后将其传递给plt.scatter。Matplotlib会在内部展平输入。

  1. import matplotlib.pyplot as plt
  2. plt.scatter(a[:, :, 0], a[:, :, 1])

巧妙的替代方法:

  1. plt.scatter(*a.reshape(-1, 2).T)

输出:

解压缩NumPy文件中的矩阵并绘制它们。

英文:

Simply slice the "columns" (actually the third dimension) and pass it to plt.scatter. Matplotlib will flatten the inputs internally.

  1. import matplotlib.pyplot as plt
  2. plt.scatter(a[:, :, 0], a[:, :, 1])

Hacky alternative:

  1. plt.scatter(*a.reshape(-1, 2).T)

Output:

解压缩NumPy文件中的矩阵并绘制它们。

huangapple
  • 本文由 发表于 2023年6月27日 20:53:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76565066.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定