1

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

1 Answer 1

1

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

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

1 Comment

Is it possible to have a color bar to show the range of values?

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.