from matplotlib import pyplot as plt
import numpy as np
# generate six random 1d arrays of different sizes
coeffs = [np.random.uniform(0, 1, np.random.randint(30, 100)) for i in range(6)]
# cA5, cD5, cD4, cD3, cD2, cD1 = coeffs
for i, ci in enumerate(coeffs):
plt.imshow(ci.reshape(1, -1), extent=[0, 1000, i + 0.5, i + 1.5], cmap='inferno', aspect='auto', interpolation='nearest')
plt.ylim(0.5, len(coeffs) + 0.5) # set the y-lim to include the six horizontal images
# optionally relabel the y-axis (the given labeling is 1,2,3,...)
plt.yticks(range(1, len(coeffs) + 1), ['cA5', 'cD5', 'cD4', 'cD3', 'cD2', 'cD1'])
plt.show()
一种可能的方法是将每个阵列绘制为一维图像,每个阵列位于不同的y位置
plt.imshow
需要一个2D数组,因此将数组的形状改为1作为第一维,将原始大小作为第二维,可以得到一个水平图像。(如果它还不是numpy数组,则需要通过np.array(ci).reshape(1, -1)
进行转换)。通过extent
参数,可以设置边界x和y值interpolation='nearest'
显示每个像素之间的硬边界aspect='auto'
需要防止imshow设置固定的纵横比相关问题 更多 >
编程相关推荐