-1

I am trying to create a grid with gray, white and black colors and specify these colors for each box in the grid. I could create a grid with 2 rows and three columns, but not sure to specify the colors in the grid (not randomly color the boxes in the grid)

Code

import matplotlib.pyplot as plt
import numpy as np

# Make a 2x3 grid...
nrows, ncols = 2,3
image = np.zeros(nrows*ncols)
image = image.reshape((nrows, ncols))
row_labels = ["Attractor1", "Attractor2"]
col_labels = ['BCL6', 'GRIN2A', 'PAFAH1B1']
plt.matshow(image)
plt.xticks(range(ncols), col_labels)
plt.yticks(range(nrows), row_labels)
plt.show() 

This is how I want to create the grid figure

image

I also tried this code below. But, I don't how to specify the color for gray and change the lables

import matplotlib.pyplot as plt
a = [[0,1],[1,0]]
plt.imshow(a,cmap='gray')
plt.show()

Thanks

vestland
  • 55,229
  • 37
  • 187
  • 305
Atom
  • 25
  • 6
  • What is not working with what you have done? – Jody Klymak Aug 03 '22 at 15:25
  • 1
    Does this answer your question? [Display image as grayscale using matplotlib](https://stackoverflow.com/questions/3823752/display-image-as-grayscale-using-matplotlib) – Jody Klymak Aug 03 '22 at 15:25
  • How to specify colors in the grid? For example as shown in the figure. Some blocks are in black, some boxes are in white and gray. How to give it in a code, the color for each box in the grid? – Atom Aug 03 '22 at 15:34
  • @JodyKlymak No, in this link, a image is imported and the colors are displayed on grayscale. Thanks – Atom Aug 03 '22 at 15:36
  • Fill your matrix with 0, 0.5, and 1? – Jody Klymak Aug 03 '22 at 15:47
  • When I give as 0 and 1, I can get the colors black and white. How to do it for gray color? I have pasted the code at the end of the question. Please find it – Atom Aug 03 '22 at 15:53
  • If black is 0, white is 1, then grey is ??? – Jody Klymak Aug 03 '22 at 16:07
  • Yes, Thanks I can get the grey color if i give 0.5. I will edit the code above for answer – Atom Aug 03 '22 at 16:14

1 Answers1

1

Please find the below code to plot a grid, specify the colors for each box in the grid as well specify the row and column names

import matplotlib.pyplot as plt
import numpy as np

a = [[0,1,0.5],[1,0,0.5]]
nrows, ncols = 2,3
image = np.zeros(nrows*ncols)
image = image.reshape((nrows, ncols))
row_labels = ["Attractor1", "Attractor2"]
col_labels = ['BCL6', 'GRIN2A', 'PAFAH1B1']
plt.matshow(image)
plt.xticks(range(ncols), col_labels)
plt.yticks(range(nrows), row_labels)
plt.imshow(a,cmap='gray')
plt.show()
Atom
  • 25
  • 6