Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot numbers from an array as annotation using matplotlib?

I am trying to produce the map on basemap using vales extracted from meteorological data. Sample code is:-

y=[2.56422, 3.77284,3.52623,3.51468,3.02199]
z=[0.15, 0.3, 0.45, 0.6, 0.75]
n=[58,651,393,203,123]

fig, ax = plt.subplots()
ax.scatter(z, y)

for i, txt in enumerate(n):
    ax.annotate(txt, (z[i],y[i]))

enter image description here

The data I am using is a numpy array. I dont know how to loop through each array to plot the kind of map similar to above. I would like to plot only values (ie. no countour or contourf).

Initially I was trying to plot float values using pylab.plot function. However, it retured with error

ValueError: third arg must be a format string

Then I tried to convert this numpy array to string and then plot with this command:-

temperature = np.array2string(data, precision=2)

and the print statement looks like a modified string:-

print temperature
[[ 19.69  21.09  21.57  21.45  20.59  20.53  20.93  20.63  20.64  21.26
   21.29  20.63  20.98  21.01  20.84  20.81  20.55  20.33  20.52  20.23
   19.84]
 [ 20.77  21.35  20.81  20.64  20.9   20.78  20.79  23.57  20.11  21.07
   21.06  21.33  21.48  21.18  21.4   21.09  20.5   20.31  20.12  19.8
   19.97]
 [ 21.51  21.23  20.55  20.08  20.05  20.78  21.17  24.77  21.17  20.95
   21.43  21.47  21.46  21.77  21.69  21.13  20.47  20.04  20.08  20.37
   20.14]
 [ 21.29  21.1   20.63  20.32  20.22  20.37  24.4   23.82  22.23  21.03
   22.11  22.62  22.71  22.37  21.73  21.35  21.03  20.67  20.58  20.89
   20.93]
 [ 21.24  21.04  20.68  20.56  20.76  20.91  24.26  23.75  23.28  21.26
   21.48  22.    21.94  21.78  21.36  21.14  20.96  20.92  21.1   21.19
   21.31]
 [ 20.83  20.88  20.6   20.87  21.01  21.91  22.33  22.21  21.74  20.66
   20.76  20.73  21.04  21.09  20.83  20.7   20.72  20.71  21.23  21.04
   20.73]
 [ 20.32  20.41  20.19  20.05  20.68  22.17  21.82  20.67  19.85  19.02
   18.91  19.6   20.15  20.64  20.64  20.09  19.81  19.76  19.9   19.94
   19.46]
 [ 19.68  20.37  20.56  20.68  20.93  21.28  21.24  20.33  20.7   20.
   18.72  18.94  19.56  19.57  19.83  19.74  19.17  18.53  18.1   18.72
   19.12]
 [ 18.88  19.71  20.77  20.81  20.32  21.58  20.96  21.33  21.2   20.17
   19.95  22.05  19.72  19.85  19.3   18.75  18.69  18.44  17.57  17.2
   18.22]
 [ 19.11  19.19  20.13  20.78  21.25  21.98  21.15  20.96  20.66  20.14
   20.51  21.92  20.36  20.27  19.    18.22  17.81  17.58  17.16  16.67
   17.46]
 [ 18.5   19.28  19.57  20.01  21.16  21.01  21.06  20.93  20.62  19.89
   20.3   20.7   19.7   19.76  18.24  17.    16.36  16.63  17.62  17.32
   17.38]
 [ 17.6   18.33  20.27  19.97  20.63  20.51  21.09  21.39  20.81  19.55
   20.    18.3   17.32  18.24  17.57  17.15  16.42  15.76  16.14  16.45
   21.95]
 [ 17.04  17.55  18.16  18.32  21.23  20.5   20.41  19.82  20.7   20.55
   20.41  18.47  18.05  17.63  17.11  15.6   16.02  15.46  14.29  13.88
   23.04]]

Finally, I get this error when I tried to plot the above value on a map with this line

pylab.plot(x, y, temperature)

    'Unrecognized character %c in format string' % c)
ValueError: Unrecognized character [ in format string

Problem seems to be with nparray to string conversion.

Any help to solve this issue is appreciated.

like image 777
sundar_ima Avatar asked Oct 20 '25 18:10

sundar_ima


1 Answers

Your original solution with ax.annotate is perfectly fine for your more general solution. The only thing to change is that in case of 2d arrays, you need to flatten them before looping over them using np.ravel() (which is also a method of the ndarray class).

However, in your specific case you can spare explicit indexing and the use of ravel() by broadcasting the three arrays you need to plot:

import numpy as np
import matplotlib.pyplot as plt

# generate some dummy data
rng = np.random.default_rng()
z, y = np.mgrid[:3, :3]
n = rng.integers(low=50, high=500, size=z.shape)

fig, ax = plt.subplots()
ax.scatter(z, y)

for zz, yy, txt in np.broadcast(z, y, n):
    ax.annotate(txt, (zz, yy))

Note that the result of np.broadcast is the same as if we'd used zip(z.ravel(), y.ravel(), n.ravel()).

like image 142