1

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()
1
  • 1
    I suppose this depends on how you want to deal with 2D points in your 3D plot, but the simple solution to exclude the None point is to replace None values with np.nan. Alternatively, you may want to preserve all 3 points, in which case you can replace with 0 Commented May 6, 2020 at 0:12

3 Answers 3

1

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)
Sign up to request clarification or add additional context in comments.

Comments

0

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')

Comments

0

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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.