0

I am trying to plot with quiver (matplotlib). My arrows indicates correlation, so I would like to change its colors according to pvalue. When I create an np.ndarray with strings ("w" or "k") that indicates the colors of each arrow, I get this error:

ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

I think that the problem is in the dtype, because it throws me the same error if I just set the color using a string, for example "w".

I saw examples that use strings to set colors but is not working for me.

Sheldon
  • 4,084
  • 3
  • 20
  • 41
  • 1
    Welcome to SO. Have you tried the solution provided in this SO post? https://stackoverflow.com/questions/40026718/different-colours-for-arrows-in-quiver-plot – Sheldon Nov 05 '22 at 16:48

1 Answers1

0

enter image description here

Don't worry, let Matplotlib do their stuff, it knows how to color the arrows in a quiver plot according to another parameter.

import numpy as np
import matplotlib.pyplot as plt

N=50
# the positions, random points in 0,1; 0,1.
x = np.random.rand(N)
y = np.random.rand(N)

# random directions, 0.025 = 0.5/20
u = np.random.rand(N)/20-0.025
v = np.random.rand(N)/20-0.025

# p values, normalized
p = np.random.rand(N)+np.random.rand(N)1
p = p/sum(p)

# quiver takes a 5th argument, a vector of values that are
# mapped to colors, it is possible to show the mapping using
# plt.colorbar
plt.quiver(x, y, u, v, p)
plt.colorbar()

plt.show()

If you don't like the default colormap, viridis, Matplotlib has a few dozen other colormaps, e.g., to use the Spectral colormap just change to

    plt.quiver(x, y, u, v, p, cmap='Spectral')

enter image description here

gboffi
  • 22,939
  • 8
  • 54
  • 85