3

I checked similar threads, and my question is going to be a step further from this one: Plotting colored grid based on values

I have a grid size 20 x 10, where the first cell (bottom left) has an ID = 0 and the last one (upper right) has an ID = 99. Lets say that I have two lists. The first one is a list of cells that have a value bigger than 0 and the second list consists of those values, e.g. cell with ID = 11, has a value 77.

Cellid = [2, 4 ,5, 11 ,45 ,48 ,98]
Cellval = [20, 45 ,55, 77,45 ,30 ,15]
  1. I would like to make a 2D plot of this grid where the cell color is based on value. Empty cells are blank, for the rest: the bigger the value, the greener the cell.
  2. Secondly, i'd like to upgrade 2D plot from previus point with 3D-plot, where the cells values are columns.

Can you give me an advice how to approach this?

2 Answers 2

3

There is a contradiction between "grid size 20 x 10" and the upper right ID=99. So I will assume you mean "grid size 10 x 10" here.

You can then create an array which is 0 everywhere, except at the positions given by Cellid. Here, I assume that ID runs along x first. You can mask it to have 0 not being colorized at all; them plot it as imshow.

import numpy as np
import matplotlib.pyplot as plt

nrows = 10
ncols = 10

Cellid = [2, 4 ,5, 11 ,45 ,48 ,98]
Cellval = [20, 45 ,55, 77,45 ,30 ,15]

data = np.zeros(nrows*ncols)
data[Cellid] = Cellval
data = np.ma.array(data.reshape((nrows, ncols)), mask=data==0)

fig, ax = plt.subplots()
ax.imshow(data, cmap="Greens", origin="lower", vmin=0)

# optionally add grid
ax.set_xticks(np.arange(ncols+1)-0.5, minor=True)
ax.set_yticks(np.arange(nrows+1)-0.5, minor=True)
ax.grid(which="minor")
ax.tick_params(which="minor", size=0)

plt.show()

enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

I prefer this solution and yes, you were correct about my "grid mistake". To continue to second question - how would be possible to show this plot in 3D, so instead of colors, there would be a column as big as cell value?
In how far would that not be answered by the 3D bars example?
I don't know how tp correctly define Cellid there in a 2D grid.
You don't need to redefine Cellid. In the example, top is what is data here. You still need to adapt x and y according to rows and cols, and that's it, right?
1

How about this?

x = 20
y = 10

scale_factor = 3

fig, ax = plt.subplots(figsize=(x / scale_factor, y / scale_factor))

ax.axis(xmin=0, xmax=x, ymin=0, ymax=y)
ax.tick_params(left=False, labelleft=False, bottom=False, labelbottom=False)
ax.xaxis.set_major_locator(ticker.MultipleLocator(1.0))
ax.yaxis.set_major_locator(ticker.MultipleLocator(1.0))

ax.grid(color='black')

cell_ids = [2, 4, 5, 11, 45, 48, 98]
cell_values = [20, 45, 55, 77, 45, 30, 15]

cdict = {'red':   [[0.0, 0.0, 0.0],
                   [0.5, 0.0, 0.0],
                   [1.0, 0.5, 0.5]],
         'green': [[0.0, 0.0, 0.0],
                   [0.5, 1.0, 1.0],
                   [1.0, 1.0, 1.0]],
         'blue':  [[0.0, 0.0, 0.0],
                   [0.5, 0.0, 0.0],
                   [1.0, 0.5, 0.5]]}

cmap = colors.LinearSegmentedColormap('greens', cdict)

for cell_id, cell_value in zip(cell_ids, cell_values):
    cell_x = cell_id % x
    cell_y = cell_id // y
    ax.add_artist(patches.Rectangle((cell_x, cell_y), 1.0, 1.0, color=cmap(cell_value * 255 // 100)))

(You might want to ask a separate question for the 3D part - it isn't quite clear)

Result

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.