Draw / create Scatterplots datasets with NaN

I want to draw a scatter plot using pylab, however some of my data NaN, for example:

a = [1, 2, 3]
b = [1, 2, None]

pylab.scatter(a,b) does not work.

Is there a way that I could draw dots of real value without displaying these values NaN?

+5
source share
2 answers

Everything will work fine if you use NaNs. None- this is not the same thing. A NaNis a float.

As an example:

import numpy as np
import matplotlib.pyplot as plt

plt.scatter([1, 2, 3], [1, 2, np.nan])
plt.show()

enter image description here

pandas ​​numpy ( numpy.genfromtxt, ), . numpy, pandas - .

:

import matplotlib.pyplot as plt
import pandas

x = pandas.Series([1, 2, 3])
y = pandas.Series([1, 2, None])
plt.scatter(x, y)
plt.show()

pandas NaN , . , , "" "". , , NaN .

, NaN s, :

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 6 * np.pi, 300)
y = np.cos(x)

y1 = np.ma.masked_where(y > 0.7, y)

y2 = y.copy()
y2[y > 0.7] = np.nan

fig, axes = plt.subplots(nrows=3, sharex=True, sharey=True)
for ax, ydata in zip(axes, [y, y1, y2]):
    ax.plot(x, ydata)
    ax.axhline(0.7, color='red')

axes[0].set_title('Original')
axes[1].set_title('Masked Arrays')
axes[2].set_title("Using NaN's")

fig.tight_layout()

plt.show()

enter image description here

+12

2D-, X, Y. "", 2D-, , "", .

. :

a = [1, 2, 3]
b = [1, None, 2]

i = 0
while i < len(a):
    if a[i] == None or b[i] == None:
        a = a[:i] + a[i+1:]
        b = b[:i] + b[i+1:]
    else:
        i += 1

"""Now a = [1, 3] and b = [1, 2]"""

pylab.scatter(a,b)
+1

All Articles