How to plot an image from a connection matrix?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-10 15:24:45

问题


I want to write a script to create an image from a connection matrix. Basically, wherever there is a '1' in the matrix, I want that area to be shaded in the image. For eg -

I created this image using Photoshop. But I have a large dataset so I will have to automate the process. It would be really helpful if anyone could point me in the right direction.

EDIT

The image that I am getting after using the script is this. This is due to the fact that the matrix is large (19 x 19). Is there any way I can increase the visibility of this image so the black and white boxes appear more clear?


回答1:


I would suggest usage of opencv combined with numpy in this case. Create two-dimensional numpy.array of dtype='uint8' with 0 for black and 255 for white. For example, to get 2x2 array with white left upper, white right lower, black left lower and black right upper, you could use code:

myarray = numpy.array([[255,0],[0,255]],dtype='uint8')

Then you could save that array as image with opencv2 in this way:

cv2.imwrite('image.bmp',myarray)

In which every cell of array is represented by single pixel, however if you want to upscale (so for example every cell is represented by 5x5 square) then you might use numpy.kron function, with following one line:

myarray = numpy.kron(myarray, numpy.ones((5,5)))

before writing image




回答2:


May be you can try this!

import matplotlib.cm as cm 
# Display matrix
plt.imshow(np.random.choice([0, 1], size=100).reshape((10, 10)),cmap=cm.binary)




回答3:


With a Seaborn heatmap:

import seaborn as sns
np.random.seed(3)
sns.set()
data = np.random.choice([0, 1], size=(16,16), p=[3./4, 1./4])
ax = sns.heatmap(data, square=True, xticklabels=False, yticklabels=False, cbar=False, linewidths=.8, linecolor='lightgray', cmap='gray_r')

Note the reverse colormap gray_r to have black for 1's and white for 0's.



来源:https://stackoverflow.com/questions/53845752/how-to-plot-an-image-from-a-connection-matrix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!