Variation in color representation using Matplotlib

Question:

I am trying to represent the array p with different values on the grid as shown. I identify the max,min of p and set the colorbar according to Amax,Amin. However, I do not see color variation even though the values are quite different. I don’t know if there is problem with color_list.append(color(P[i]/Amax)).

import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
import numpy as np
from matplotlib.colors import Normalize
from matplotlib import cm
import math


fig,ax = plt.subplots(1)
n=3


N=2*n*(n-1)
#p = np.random.uniform(a,b,N)
p=np.array([[0.000053               , 0.00005017809219905991 ,
        0.00005034247517775545 , 0.000048835631206379674,
        0.000051109595745001285, 0.0000519589078015949  ,
        0.00004938357446869813 , 0.000047575361703047204,
        0.0000502876808515236  , 0.00004858905673833636 ,
        0.00004724659574565612 , 0.00005242465957456561 ]])


p=p.reshape(12,1)           
P = np.array(p)
P=P.reshape(len(P),1)

Max=max(P)
#print("Max =",Max)
Min=min(P)
#print("Min =",Min)
a=Min
b=Max



Amax= b*math.ceil(Max/b)
Amin= a*math.floor(Min/a)
#print(Amax, Amin)



color = cm.get_cmap('Greys')
norm = Normalize(vmin=Amin, vmax=Amax)
color_list = []
for i in range(len(P)):
    color_list.append(color(P[i]/Amax))
#print(color_list)
id = 0
for j in range(0, n):
    for k in range(n-1):
        ax.hlines(200+200*(n-j-1)+5*n, 200*(k+1)+5*n, 200*(k+2)+5*n, zorder=0, colors=color_list[id],linewidth=5.0)
        id += 1

    for i in range(0, n):
        rect = mpl.patches.Rectangle((200+200*i, 200+200*j), 10*n, 10*n, linewidth=1, edgecolor='black', facecolor='black')
        ax.add_patch(rect)
        if j < n-1:
            ax.vlines(200+200*i+5*n, 200*(n-1-j)+5*n, 200*(n-j)+5*n, zorder=0, colors=color_list[id],linewidth=5.0)
            id += 1

cb = fig.colorbar(cm.ScalarMappable(cmap=color, norm=norm))
cb.set_label("Radius (m)")
ax.set_xlim(left = 0, right = 220*n)
ax.set_ylim(bottom = 0, top = 220*n)

# ax.set_yticklabels([])
# ax.set_xticklabels([])
plt.axis('off')

plt.show()

enter image description here

Asked By: Klimt865

||

Answers:

You are scaling your color_list from [0, Amax] in stead of [Amin, Amax]
You could use the norm to scale it.

for i in range(len(P)):
    color_list.append(color(norm(P[i])))

enter image description here

Answered By: filiphl
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.