0

I am trying to represent the array Pe using lines as shown in the current output but the colors of the lines do not reflect the actual array. For example, line marked 0 has the value 394.20560747663563 and should be blue instead it is yellow.

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
from  numpy import nan

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


N=2*n*(n-1)

J = np.array([[]])
Pe=np.array([[394.20560747663563, 408.7929050665396 , 419.132709901089  ,
       398.95097406721044, 403.81198021076113, 430.00914784982064,
       424.50127213826016, 453.54817733128607, 441.4651085668709 ,
       447.42507960635163, 413.8982415602072 , 390.3025816600353 ]])
             
             
             

C1 = nan

for i in J[0]:
    Pe = np.insert(Pe, i, [C1], axis=1)
print("Pe =", [Pe])



for i in range(0,len(Pe)):
    Max=max(max(Pe[i]), max(Pe[i]))
    Min=min(min(Pe[i]), min(Pe[i]))
a=Min
b=Max



Amax= math.ceil(Max)
Amin= math.floor(Min)
print(Amax, Amin)



color = cm.get_cmap('Dark2')
norm = Normalize(vmin=Amin, vmax=Amax)
color_list = []



for i in range(len(Pe[0])):
    color_list.append(color(Pe[0,i]/Amax))

id = 0
for j in range(0, n):
    for k in range(n-1):
        aPe.hlines(200+200*(n-j-1)+5*n, 200*(k+1)+5*n, 200*(k+2)+5*n, zorder=0, colors=color_list[id])
        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')
        aPe.add_patch(rect)
        if j < n-1:
            aPe.vlines(200+200*i+5*n, 200*(n-1-j)+5*n, 200*(n-j)+5*n, zorder=0, colors=color_list[id])
            id += 1

cb = fig.colorbar(cm.ScalarMappable(cmap=color, norm=norm))
cb.set_label("Entry pressure (N/m$^{2}$)")
aPe.set_xlim(left = 0, right = 220*n)
aPe.set_ylim(bottom = 0, top = 220*n)


plt.axis('off')

plt.show()

The current output is

enter image description here

1 Answer 1

1

The problem is how you are trying to get your colors from color(). You need to first set the scale so that the value 390 Amin is equivalent to 0 and 454 Amax is equivalent to 1. To do this, subtract the Amin from the values then divide that by the difference of Amax and Amin. So the color_list will be created by:

for i in range(len(Pe[0])):
    color_list.append(color(((Pe[0,i])-Amin)/(Amax-Amin)))

The values are:

[394.20560747663563,
 408.7929050665396,
 419.132709901089,
 398.95097406721044,
 403.81198021076113,
 430.00914784982064,
 424.50127213826016,
 453.54817733128607,
 441.4651085668709,
 447.42507960635163,
 413.8982415602072,
 390.3025816600353]

And the values used for grabbing the colors from color() are:

[0.06571261682243179,
 0.2936391416646815,
 0.45519859220451586,
 0.1398589698001631,
 0.21581219079314273,
 0.6251429351534474,
 0.539082377160315,
 0.9929402708013448,
 0.8041423213573582,
 0.8972668688492442,
 0.3734100243782379,
 0.004727838438051357]

Which, when put all together, gives you the graph:

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
from  numpy import nan

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

N=2*n*(n-1)

J = np.array([[]])
Pe=np.array([[394.20560747663563, 408.7929050665396 , 419.132709901089  ,
       398.95097406721044, 403.81198021076113, 430.00914784982064,
       424.50127213826016, 453.54817733128607, 441.4651085668709 ,
       447.42507960635163, 413.8982415602072 , 390.3025816600353 ]])         
             
C1 = nan
for i in J[0]:
    Pe = np.insert(Pe, i, [C1], axis=1)
print("Pe =", [Pe])

for i in range(0,len(Pe)):
    Max=max(max(Pe[i]), max(Pe[i]))
    Min=min(min(Pe[i]), min(Pe[i]))
a=Min
b=Max
Amax= math.ceil(Max)
Amin= math.floor(Min)
print(Amax, Amin)

color = cm.get_cmap('Dark2')
norm = Normalize(vmin=Amin, vmax=Amax)
color_list = []
for i in range(len(Pe[0])):   
    color_list.append(color(((Pe[0,i])-Amin)/(Amax-Amin)))
    
id = 0
for j in range(0, n):
    for k in range(n-1):
        aPe.hlines(200+200*(n-j-1)+5*n, 200*(k+1)+5*n, 200*(k+2)+5*n, zorder=0, colors=color_list[id])
        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')
        aPe.add_patch(rect)
        if j < n-1:
            aPe.vlines(200+200*i+5*n, 200*(n-1-j)+5*n, 200*(n-j)+5*n, zorder=0, colors=color_list[id])
            id += 1

cb = fig.colorbar(cm.ScalarMappable(cmap=color, norm=norm), ticks=np.arange(Amin, Amax+len(color.colors), len(color.colors)))
cb.set_label("Entry pressure (N/m$^{2}$)")
aPe.set_xlim(left = 0, right = 220*n)
aPe.set_ylim(bottom = 0, top = 220*n)
plt.axis('off')
plt.show()

enter image description here

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

4 Comments

This works but the colorbar is a bit odd. For example, 400 is somewhere in the orange band, 410 is at the center of purple band and so on. How can I fix so that the values are at the starting of each color band?
That has to do with your fig.colorbar's tick scaling. You can set the ticks with cb = fig.colorbar(cm.ScalarMappable(cmap=color, norm=norm), ticks=np.arange(Amin, Amax, len(color.colors)))
I was wondering why Amax=454 is not written along the color bar but Amin=390 is.
cb = fig.colorbar(cm.ScalarMappable(cmap=color, norm=norm), ticks=np.arange(Amin, Amax+len(color.colors), len(color.colors)))

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.