从每一行中随机选择10个Numpy数组元素,并使用plt.matshow()进行绘图的步骤如下:
import numpy as np
import matplotlib.pyplot as plt
array = np.array([[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
[21, 22, 23, 24, 25, 26, 27, 28, 29, 30]])
rows, cols = array.shape
result = np.empty((rows, 10))
for i in range(rows):
indices = np.random.choice(cols, size=10, replace=False)
result[i] = array[i, indices]
plt.matshow(result)
plt.colorbar()
plt.show()
完整代码示例:
import numpy as np
import matplotlib.pyplot as plt
array = np.array([[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
[21, 22, 23, 24, 25, 26, 27, 28, 29, 30]])
rows, cols = array.shape
result = np.empty((rows, 10))
for i in range(rows):
indices = np.random.choice(cols, size=10, replace=False)
result[i] = array[i, indices]
plt.matshow(result)
plt.colorbar()
plt.show()
这段代码的作用是从给定的Numpy数组中的每一行中随机选择10个元素,并将选择的结果绘制成热图。plt.matshow()函数用于绘制矩阵,颜色的深浅表示元素的大小。plt.colorbar()函数用于添加颜色条。
推荐的腾讯云相关产品:腾讯云服务器(CVM)和腾讯云云数据库MySQL。
领取专属 10元无门槛券
手把手带您无忧上云