问题
The data I am trying to map out is a matrix of shape 5x64 called '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]]
The code is:
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()
Issue: Each vector (5 in total) within the data matrix (field_matrix) has a value of 0-3 and the value yields the appropriate colour - see image below and the legend.
However, the output image only yields 4 rows (F002, F003, F004, F005) when it should also include 0000 as there should be 5 rows of data in that plot graphic.
I have tried to troubleshoot using similar imshow() issues on Stack but cannot fix the issue. Any ideas?
Aside question - as being fairly new to matplotlib, do you know how to eliminate white space in the graph (notice how the image is skewed to the top of the graph.
Thanks!
Output from plt.show()
回答1:
Basically, you are doing:
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
And then:
ax.set_yticks(np.arange(0.5, 20.5, 1.), minor=True)
So, the second line is overwriting the yticks
modified before on the same axis. So...I removed the second line and did this:
plt.yticks(np.arange(0, 5), ['0000', 'F002', 'F003', 'F004', 'F005'])
Notice I used a fixed list with values you expect from utilities.num2pid[k]
. because I do not know that function.
I got this result:
Hope this helps.
来源:https://stackoverflow.com/questions/60872500/imshow-not-showing-image-correctly