Reputation: 43
Say I create three lists:
x=[1,2,3]
y=[4,5,6]
z=[1,None,4]
How can I scatter this and simply only include the points with numbers (i.e. exclude the "none" point). My code won't produce a scatter plot when I include these lists (however when I include a number instead of "None" it works):
from mpl_toolkits import mplot3d
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
%matplotlib notebook
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x, y, z, c='r', marker='o')
plt.show()
Upvotes: 1
Views: 1393
Reputation: 1653
You should use NaNs
instead of None
which is not the same thing. A NaN
is a float.
Minimal example
import numpy as np
import matplotlib.pyplot as plt
x=[1,2,3]
y=[4,5,6]
z=[1,np.nan,4]
plt.scatter(x,y,z)
plt.show()
Upvotes: 0
Reputation: 1442
you can use numpy.nan
instead of None
import numpy as np
z=[1,None,4]
z_numpy = np.asarray(z, dtype=np.float32)
....
ax.scatter(x, y, z_numpy, c='r', marker='o')
Upvotes: 0
Reputation: 2495
You can do
import numpy as np
and replace your None
with a np.nan
. The points containing np.nan
will not be plotted in your scatter plot. See this matplotlib doc for more information.
If you have long lists containing None
, you can perform the conversion via
array_containing_nans = np.array(list_containing_nones, dtype=float)
Upvotes: 1