Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib: Plot numpy arrays with None as values

I have an array that looks like:

k = numpy.array([(1.,0.001), (1.1, 0.002), (None, None), 
                 (1.2, 0.003), (0.99, 0.004)])

I want to plot the values that are not (None, None) and keep the index of the array value. That is, I want a gap wherever there is a (None, None) value.

When that is done I'd like to plot

y = k[:,0] + k[:,1]

but I can't even add the arrays together. I tried masking the array, but I lost the index values of the original k array.

A minimal example:

import matplotlib.pyplot as pyplot
import numpy

x = range(5)
k = numpy.array([(1.,0.001), (1.1, 0.002), (None, None), 
                 (1.2, 0.003), (0.99, 0.004)])

Fig, ax = pyplot.subplots()

# This plots a gap---as desired
ax.plot(x, k[:,0], 'k-')

# I'd like to plot
#     k[:,0] + k[:,1]
# but I can't add None

# Here I get rid of the (None, None) values so I can add
# But I lose the original indexing
mask = k != (None, None)
y = k[mask].reshape((-1,2))

ax.plot(range(len(y)), y[:,0]+y[:,1], 'k--')
like image 897
jlconlin Avatar asked Oct 29 '13 14:10

jlconlin


People also ask

What is none in Numpy array?

None is an alias for NP. newaxis. It creates an axis with length 1. This can be useful for matrix multiplcation etc.

Is PLT show () blocking?

show() and plt. draw() are unnecessary and / or blocking in one way or the other.

Is PLT show () necessary?

Using plt. show() in Matplotlib mode is not required.


2 Answers

You can use numpy.nan instead of None.

import matplotlib.pyplot as pyplot
import numpy

x = range(5)
k = numpy.array([(1.,0.001), (1.1, 0.002), (numpy.nan, numpy.nan), 
                 (1.2, 0.003), (0.99, 0.004)])

Fig, ax = pyplot.subplots()

# This plots a gap---as desired
ax.plot(x, k[:,0], 'k-')

ax.plot(range(len(y)), y[:,0]+y[:,1], 'k--')

Or you could mask the x value as well, so the indices were consistent between x and y

import matplotlib.pyplot as pyplot
import numpy

x = range(5)
y = numpy.array([(1.,0.001), (1.1, 0.002), (numpy.nan, numpy.nan), 
                 (1.2, 0.003), (0.99, 0.004)])

Fig, ax = pyplot.subplots()


ax.plot(range(len(y)), y[:,0]+y[:,1], 'k--')
import matplotlib.pyplot as pyplot
import numpy

x = range(5)
k = numpy.array([(1.,0.001), (1.1, 0.002), (None, None), 
                 (1.2, 0.003), (0.99, 0.004)])

Fig, ax = pyplot.subplots()

# This plots a gap---as desired
ax.plot(x, k[:,0], 'k-')

# I'd like to plot
#     k[:,0] + k[:,1]
# but I can't add None

arr_none = np.array([None])
mask = (k[:,0] == arr_none) | (k[:,1] == arr_none)

ax.plot(numpy.arange(len(y))[mask], k[mask,0]+k[mask,1], 'k--')
like image 136
Greg Whittier Avatar answered Sep 25 '22 03:09

Greg Whittier


You can filter you array doing:

test = np.array([None])
k = k[k!=test].reshape(-1, 2).astype(float)

And then sum up the columns and make the plot. The problem of your approach is that you did not convert the None type to a numpy array, which did not allow the proper creation of the mask.

like image 44
Saullo G. P. Castro Avatar answered Sep 22 '22 03:09

Saullo G. P. Castro