Home > Mobile >  Rendering colors to arrows based on an array using Matplotlib
Rendering colors to arrows based on an array using Matplotlib

Time:07-18

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

The expected output is

enter image description here

CodePudding user response:

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

  • Related