Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Rendering colors to arrows based on an array using Matplotlib

I am plotting 6 arrows at specific coordinates. How do I render colors to these arrows based on the array V? The values are to be assigned according to the expected output.

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


V=np.array([[10],[20],[30],[40],[50],[60]])

cmap = plt.cm.jet
cNorm  = colors.Normalize(vmin=np.min(V), vmax=np.max(V))
scalarMap = cm.ScalarMappable(norm=cNorm,cmap=cmap)


fig = plt.figure()
ax  = fig.add_axes([0.1, 0.1, 0.7, 0.855]) # [left, bottom, width, height]
axc = fig.add_axes([0.85, 0.10, 0.05, 0.85])

        
for idx in range(0,len(V)):
        colorVal = scalarMap.to_rgba(V[idx])
        ax.arrow(200, 400, -50, -50,width=5, head_length=20, fc='k',ec='k')
        ax.arrow(200, 400, 50, -50, width=5, head_length=20, fc='k', ec='k')
        ax.arrow(200, 400, 0, -50, width=5, head_length=20, fc='k', ec='k')
        ax.arrow(200, 300, 50, 0, width=5, head_length=20, fc='k', ec='k')
        ax.arrow(200, 300, 50, -50, width=5, head_length=20, fc='k', ec='k')
        ax.arrow(400, 300, -50, -50, width=5, head_length=20, fc='k', ec='k')  
        cb1 = mpl.colorbar.ColorbarBase(axc, cmap=cmap,
                                            norm=cNorm,orientation='vertical')
        
ax.set_xlim(left = 0, right = 500)
ax.set_ylim(bottom = 0, top = 500)
plt.show()

The current output is

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The expected output is

enter image description here

>Solution :

Since the RGBA values are obtained in the loop process, it prepares an empty list and obtains the color of each arrow. From that acquired list, specify the color from the resulting list.

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

V=np.array([[10],[20],[30],[40],[50],[60]])

cmap = plt.cm.jet
cNorm  = colors.Normalize(vmin=np.min(V), vmax=np.max(V))
scalarMap = cm.ScalarMappable(norm=cNorm,cmap=cmap)


fig = plt.figure()
ax  = fig.add_axes([0.1, 0.1, 0.7, 0.855]) # [left, bottom, width, height]
axc = fig.add_axes([0.85, 0.10, 0.05, 0.85])

color_list = []        
for idx in range(0,len(V)):
        colorVal = scalarMap.to_rgba(V[idx])
        r,g,b,a = colorVal[0].tolist()
        color_list.append((r,g,b,a))
        
ax.arrow(200, 400, -50, -50,width=5, head_length=20, fc=color_list[0],ec=color_list[0])
ax.arrow(200, 400, 50, -50, width=5, head_length=20, fc=color_list[1], ec=color_list[1])
ax.arrow(200, 400, 0, -50, width=5, head_length=20, fc=color_list[2], ec=color_list[2])
ax.arrow(200, 300, 50, 0, width=5, head_length=20, fc=color_list[3], ec=color_list[3])
ax.arrow(200, 300, 50, -50, width=5, head_length=20, fc=color_list[4], ec=color_list[4])
ax.arrow(400, 300, -50, -50, width=5, head_length=20, fc=color_list[5], ec=color_list[5])  
cb1 = mpl.colorbar.ColorbarBase(axc, cmap=cmap,
                                    norm=cNorm,orientation='vertical')
        
ax.set_xlim(left = 0, right = 500)
ax.set_ylim(bottom = 0, top = 500)
plt.show()

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading