0

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

1 Answer 1

1

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

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

Comments

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.