Home > Enterprise >  Specifying a color bar of multiple arrows using Matplotlib
Specifying a color bar of multiple arrows using Matplotlib

Time:07-04

I am plotting multiple arrows using the list I6. I want these arrows to represent different values. For instance, in I6[0], the arrow represents 10, in I6[1], it represents 20 and so on along with a color bar highlighting these values. The current and expected outputs are presented.

import matplotlib.pyplot as plt

I6 = [
    [(0.5, -0.5), (1.0, 0.0),10], 
    [(0.5, -0.5), (0.0, -1.0),20], 
    [(1.5, -0.5), (0.0, -1.0),30], 
    [(0.5, -1.5), (1.0, 0.0),40]
]

for element in I6:
    (x, y), (dx, dy), (u) = element
    plt.quiver(x, y, dx, dy,u,scale=1, units="xy", scale_units="xy")

plt.show()

The current output is

enter image description here

The expected output is

enter image description here

CodePudding user response:

One solution is to provide lists to the quiver function:

import matplotlib.pyplot as plt

I6 = [
    [(0.5, -0.5), (1.0, 0.0), 10], 
    [(0.5, -0.5), (0.0, -1.0), 20], 
    [(1.5, -0.5), (0.0, -1.0), 30], 
    [(0.5, -1.5), (1.0, 0.0), 40]
]

x = [elem[0][0] for elem in I6]
y = [elem[0][1] for elem in I6]
dx = [elem[1][0] for elem in I6]
dy = [elem[1][1] for elem in I6]
u = [elem[2] for elem in I6]

plt.quiver(x, y, dx, dy, u, scale=1, units="xy", scale_units="xy")
plt.show()

enter image description here

EDIT

With discrete colorbar:

import matplotlib
import matplotlib.pyplot as plt

I6 = [
    [(0.5, -0.5), (1.0, 0.0), 10], 
    [(0.5, -0.5), (0.0, -1.0), 20], 
    [(1.5, -0.5), (0.0, -1.0), 30], 
    [(0.5, -1.5), (1.0, 0.0), 40]
]

x = [elem[0][0] for elem in I6]
y = [elem[0][1] for elem in I6]
dx = [elem[1][0] for elem in I6]
dy = [elem[1][1] for elem in I6]
u = [elem[2] for elem in I6]


cmap = matplotlib.cm.get_cmap("plasma", len(u))
plt.quiver(x, y, dx, dy, u, scale=1, units="xy", scale_units="xy", cmap=cmap)
plt.colorbar()
plt.show()

enter image description here

With standard colorbar:

import matplotlib.pyplot as plt

I6 = [
    [(0.5, -0.5), (1.0, 0.0), 10], 
    [(0.5, -0.5), (0.0, -1.0), 20], 
    [(1.5, -0.5), (0.0, -1.0), 30], 
    [(0.5, -1.5), (1.0, 0.0), 40]
]

x = [elem[0][0] for elem in I6]
y = [elem[0][1] for elem in I6]
dx = [elem[1][0] for elem in I6]
dy = [elem[1][1] for elem in I6]
u = [elem[2] for elem in I6]


plt.quiver(x, y, dx, dy, u, scale=1, units="xy", scale_units="xy")
plt.colorbar()
plt.show()

enter image description here

  • Related