energyMax
energyMax

Reputation: 419

Python - plotting grid based on values

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?

Upvotes: 3

Views: 5618

Answers (2)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339220

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

Upvotes: 3

gmds
gmds

Reputation: 19885

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

Upvotes: 1

Related Questions