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

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

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.

import numpy as np
import matplotlib.pyplot as plt

# Load the .npy file
a = np.load("filename.npy")

# Extract x and y coordinates
x = a[:, :, 0]
y = a[:, :, 1]

# Create a scatter plot
plt.scatter(x, y)
plt.xlabel('X Coordinates')
plt.ylabel('Y Coordinates')
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")

 a

([[[19.3, 117.88],
[20.77, 118.99],
[18.92, 117.66],
[16.23, 115.67]],

[[16.335, 113.789],
 [17.876, 116.8],
 [22.76, 115.34],
 [23.33, 111.45]],

 [[22.56, 113.76],
  [21.6,  118.07],
  [18.3,  116.60],
  [21.739, 117.903]]])

答案1

得分: 1

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

import matplotlib.pyplot as plt

plt.scatter(a[:, :, 0], a[:, :, 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.

import matplotlib.pyplot as plt

plt.scatter(a[:, :, 0], a[:, :, 1])

Hacky alternative:

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:

确定