imshow() 没有正确显示图像
imshow() not showing image correctly
我要绘制的数据是一个形状为 5x64 的矩阵,名为 'field_matrix'
field_matrix = [
[0 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 2 2 2 2 2 2 2 2 2 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1]
[0 0 0 0 0 0 0 1 0 0 1 2 2 2 2 2 2 2 2 2 0 0 2 2 2 2 2 2 0 0 0 0 0 0 0 0 0
0 0 0 0 1 0 2 2 2 2 2 2 2 2 2 0 2 2 2 2 2 2 2 0 0 0 0]
[0 0 0 0 0 0 0 1 0 1 0 2 2 2 2 2 2 2 2 2 0 2 2 2 2 2 2 2 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 2 2 2 2 2 2 2 1 1 1 1 1]
[0 0 0 0 0 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 0
2 2 2 2 2 2 2 0 0 0 0 1 0 1 1 0 0 0 0 1 1 1 1 1 1 1 1]
[0 0 0 0 1 1 1 1 1 1 1 2 2 2 2 2 2 2 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0 0 1 2 2 2 2 0 1 1 1 0 0 1 1 1 1 1 1]]
密码是:
norm = mpl.colors.Normalize(vmin=0, vmax=3)
cmap = cm.Blues
mapper = cm.ScalarMappable(norm=norm, cmap=cmap)
#field_value_dict = {0: 'Sensor', 1: 'Multi-Value', 2: 'Constant', 3: 'Counter'}
field_value_dict = {2: 'Sensor', 1: 'Multi-Value', 0: 'Constant', 3: 'Counter'}
patch_list = []
for k,v in field_value_dict.items():
patch = mpatches.Patch(color=mapper.to_rgba(k), label=v)
patch_list.append(patch)
#print(patch_list)
plt.figure(figsize=(20, 12.5))
plt.imshow(field_matrix, interpolation='nearest', cmap=cmap, )
ax = plt.gca()
# Put a legend to the right of the current axis
# Shrink current axis by 20%
#box = ax.get_position()
#ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])
#ax.legend(loc='center left', bbox_to_anchor=(1, 0.5), handles=patch_list)
ax.legend(loc='center', bbox_to_anchor=(0.5, -0.5), handles=patch_list)
# Set the ticks and labels
plt.yticks(np.arange(5), [utilities.num2pid[k] for k in range(5)]) #num2pids will output '0000', 'F002', 'F003', 'F004', 'F005 -- but note that '0000' is missing from the image
plt.xticks(np.arange(-0.5, 60, 8.), np.arange(0, 64, 8))
# Set offset ticks to make the grid boxes surround the squares representing bits
ax.set_xticks(np.arange(-0.5, 64.5, 8.), minor=True)
ax.set_yticks(np.arange(0.5, 20.5, 1.), minor=True)
plt.grid(which='minor')
plt.xlabel('bit')
plt.ylabel('ID')
fig_save_dir = os.path.expanduser('../figures/')
#filename = os.path.join(fig_save_dir, 'field_map.pgf')
filename = os.path.join(fig_save_dir, 'field_map_J1939-PGNs.png')
plt.savefig(filename)#, bbox_inches=2)
plt.show()
问题:
数据矩阵 (field_matrix) 中的每个向量(总共 5 个)的值为 0-3,该值产生适当的颜色 - 请参见下图和图例。
但是,输出图像只产生 4 行(F002、F003、F004、F005),而它还应该包含 0000,因为该绘图图形中应该有 5 行数据。
我曾尝试在 Stack 上使用类似的 imshow() 问题进行故障排除,但无法解决问题。有什么想法吗?
旁白问题 - 作为 matplotlib 的新手,您知道如何消除图中的白色 space(注意图像如何偏向图表顶部。
谢谢!
Output from plt.show()
基本上,你在做:
plt.yticks(np.arange(5), [utilities.num2pid[k] for k in range(5)]) #num2pids will output '0000', 'F002', 'F003', 'F004', 'F005 -- but note that '0000' is missing from the image
然后:
ax.set_yticks(np.arange(0.5, 20.5, 1.), minor=True)
所以,第二行是在同一轴上覆盖之前修改的yticks
。所以...我删除了第二行并这样做了:
plt.yticks(np.arange(0, 5), ['0000', 'F002', 'F003', 'F004', 'F005'])
请注意,我使用了一个固定列表,其中包含您期望从 utilities.num2pid[k]
获得的值。因为我不知道那个函数。
我得到了这个结果:
希望对您有所帮助。
我要绘制的数据是一个形状为 5x64 的矩阵,名为 'field_matrix'
field_matrix = [
[0 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 2 2 2 2 2 2 2 2 2 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1]
[0 0 0 0 0 0 0 1 0 0 1 2 2 2 2 2 2 2 2 2 0 0 2 2 2 2 2 2 0 0 0 0 0 0 0 0 0
0 0 0 0 1 0 2 2 2 2 2 2 2 2 2 0 2 2 2 2 2 2 2 0 0 0 0]
[0 0 0 0 0 0 0 1 0 1 0 2 2 2 2 2 2 2 2 2 0 2 2 2 2 2 2 2 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 2 2 2 2 2 2 2 1 1 1 1 1]
[0 0 0 0 0 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 0
2 2 2 2 2 2 2 0 0 0 0 1 0 1 1 0 0 0 0 1 1 1 1 1 1 1 1]
[0 0 0 0 1 1 1 1 1 1 1 2 2 2 2 2 2 2 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0 0 1 2 2 2 2 0 1 1 1 0 0 1 1 1 1 1 1]]
密码是:
norm = mpl.colors.Normalize(vmin=0, vmax=3)
cmap = cm.Blues
mapper = cm.ScalarMappable(norm=norm, cmap=cmap)
#field_value_dict = {0: 'Sensor', 1: 'Multi-Value', 2: 'Constant', 3: 'Counter'}
field_value_dict = {2: 'Sensor', 1: 'Multi-Value', 0: 'Constant', 3: 'Counter'}
patch_list = []
for k,v in field_value_dict.items():
patch = mpatches.Patch(color=mapper.to_rgba(k), label=v)
patch_list.append(patch)
#print(patch_list)
plt.figure(figsize=(20, 12.5))
plt.imshow(field_matrix, interpolation='nearest', cmap=cmap, )
ax = plt.gca()
# Put a legend to the right of the current axis
# Shrink current axis by 20%
#box = ax.get_position()
#ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])
#ax.legend(loc='center left', bbox_to_anchor=(1, 0.5), handles=patch_list)
ax.legend(loc='center', bbox_to_anchor=(0.5, -0.5), handles=patch_list)
# Set the ticks and labels
plt.yticks(np.arange(5), [utilities.num2pid[k] for k in range(5)]) #num2pids will output '0000', 'F002', 'F003', 'F004', 'F005 -- but note that '0000' is missing from the image
plt.xticks(np.arange(-0.5, 60, 8.), np.arange(0, 64, 8))
# Set offset ticks to make the grid boxes surround the squares representing bits
ax.set_xticks(np.arange(-0.5, 64.5, 8.), minor=True)
ax.set_yticks(np.arange(0.5, 20.5, 1.), minor=True)
plt.grid(which='minor')
plt.xlabel('bit')
plt.ylabel('ID')
fig_save_dir = os.path.expanduser('../figures/')
#filename = os.path.join(fig_save_dir, 'field_map.pgf')
filename = os.path.join(fig_save_dir, 'field_map_J1939-PGNs.png')
plt.savefig(filename)#, bbox_inches=2)
plt.show()
问题: 数据矩阵 (field_matrix) 中的每个向量(总共 5 个)的值为 0-3,该值产生适当的颜色 - 请参见下图和图例。
但是,输出图像只产生 4 行(F002、F003、F004、F005),而它还应该包含 0000,因为该绘图图形中应该有 5 行数据。
我曾尝试在 Stack 上使用类似的 imshow() 问题进行故障排除,但无法解决问题。有什么想法吗?
旁白问题 - 作为 matplotlib 的新手,您知道如何消除图中的白色 space(注意图像如何偏向图表顶部。
谢谢!
Output from plt.show()
基本上,你在做:
plt.yticks(np.arange(5), [utilities.num2pid[k] for k in range(5)]) #num2pids will output '0000', 'F002', 'F003', 'F004', 'F005 -- but note that '0000' is missing from the image
然后:
ax.set_yticks(np.arange(0.5, 20.5, 1.), minor=True)
所以,第二行是在同一轴上覆盖之前修改的yticks
。所以...我删除了第二行并这样做了:
plt.yticks(np.arange(0, 5), ['0000', 'F002', 'F003', 'F004', 'F005'])
请注意,我使用了一个固定列表,其中包含您期望从 utilities.num2pid[k]
获得的值。因为我不知道那个函数。
我得到了这个结果:
希望对您有所帮助。