Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating a 3D scatter plot from a CSV file

I wanted to create a 3d scatter from a csv file and I wasn't able to print the 3d scatter. My code was like this

import matplotlib.pyplot as plt
import numpy as np 

fig = plt.figure()
ax1 = fig.add_subplot(111,projection='3d')

x, y, z = np.loadtxt('3d_sample.txt', delimiter=',', unpack=True)

ax1.scatter(x,y,z)
plt.show()
like image 341
R0b0t0 Avatar asked May 10 '26 08:05

R0b0t0


1 Answers

You need to add:

from mpl_toolkits.mplot3d import Axes3D

For example,

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax1 = fig.add_subplot(111,projection='3d')

x= np.array([1,2,3,4])
y=np.array([5,6,7,8])
z=np.array([9,10,11,12])

ax1.scatter(x,y,z)
plt.show()

gives

enter image description here

like image 143
Miriam Farber Avatar answered May 11 '26 20:05

Miriam Farber



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!