133

I am currently have a nx3 matrix array. I want plot the three columns as three axis's. How can I do that?

I have googled and people suggested using Matlab, but I am really having a hard time with understanding it. I also need it be a scatter plot.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
user211037
  • 1,725
  • 5
  • 16
  • 11
  • 1
    Are you looking for an application to do the plot, or do you want to write code (in whatever language) that draws the plot? – Nils Pipenbrinck Dec 31 '09 at 15:38
  • Use asymptote - it is free, powerful, and the quality is awesome! – Hamish Grubijan Dec 31 '09 at 15:40
  • 1
    If you have an array, I'd assume you have a program and this array is part of it. Please give us a chance to help you by indicating what kind of program (language) it is! – Carl Smotricz Dec 31 '09 at 15:46
  • I would still use asymptote (as a lib), or just generate the input file for it. Check out it's amazing output quality! – Hamish Grubijan Dec 31 '09 at 15:54
  • 2
    If it's an `n` by 3 array, you will get a plot of a curve in 3 dimensions. For a "3-d plot", you would need an `n x m x l` array. Do you want a curve, with any one tuple `(x,y,z)` representing a point on the curve? – Alok Singhal Dec 31 '09 at 15:55

4 Answers4

221

You can use matplotlib for this. matplotlib has a mplot3d module that will do exactly what you want.

import matplotlib.pyplot as plt
import random

fig = plt.figure(figsize=(12, 12))
ax = fig.add_subplot(projection='3d')

sequence_containing_x_vals = list(range(0, 100))
sequence_containing_y_vals = list(range(0, 100))
sequence_containing_z_vals = list(range(0, 100))

random.shuffle(sequence_containing_x_vals)
random.shuffle(sequence_containing_y_vals)
random.shuffle(sequence_containing_z_vals)

ax.scatter(sequence_containing_x_vals, sequence_containing_y_vals, sequence_containing_z_vals)
plt.show()

The code above generates a figure like:

enter image description here

rayryeng
  • 102,964
  • 22
  • 184
  • 193
Chinmay Kanchi
  • 62,729
  • 22
  • 87
  • 114
  • 15
    Nice solution. Note that `plt.zlabel('zlabel')` wont work. To label the z axis you need to use: `ax.set_zlabel('Z')`. – Yonatan Simson May 12 '16 at 04:18
  • 2
    instead of from matplotlib.pyplot it should be import matplotlib.pyplot – roktim Dec 01 '21 at 02:18
  • 1
    @roktim Good spot. The previous editor made that mistake in formatting the post which I've now corrected. – rayryeng Jan 06 '22 at 20:32
  • 1
    This was exactly what I was looking for, thanks! Just to add onto this, I also wanted my 3D scatterplot to rotate (I needed a gif of this), and found this [blog post](http://blog.mahler83.net/2019/10/rotating-3d-t-sne-animated-gif-scatterplot-with-matplotlib/) that described how to do it with the solution you listed – S.Chauhan Aug 22 '22 at 01:07
15

Use the following code it worked for me:

# Create the figure
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# Generate the values
x_vals = X_iso[:, 0:1]
y_vals = X_iso[:, 1:2]
z_vals = X_iso[:, 2:3]

# Plot the values
ax.scatter(x_vals, y_vals, z_vals, c = 'b', marker='o')
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.set_zlabel('Z-axis')

plt.show()

while X_iso is my 3-D array and for X_vals, Y_vals, Z_vals I copied/used 1 column/axis from that array and assigned to those variables/arrays respectively.

IEatBagels
  • 823
  • 2
  • 8
  • 23
Saeed Ullah
  • 302
  • 3
  • 11
6
from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = plt.axes(projection='3d')

scatter plot

zdata = 15 * np.random.random(100)
xdata = np.sin(zdata) + 0.1 * np.random.randn(100)
ydata = np.cos(zdata) + 0.1 * np.random.randn(100)
ax.scatter3D(xdata, ydata, zdata);

enter image description here

Colab notebook

Sajjad Aemmi
  • 2,120
  • 3
  • 13
  • 25
Aman Bagrecha
  • 406
  • 4
  • 9
1

Using plotly - Easiest and most functional and nice plots

import plotly.express as px
df = px.data.iris()
fig = px.scatter_3d(df, x='sepal_length', y='sepal_width', z='petal_width',
              color='species')
fig.show()

https://plotly.com/python/3d-scatter-plots/

DataYoda
  • 771
  • 5
  • 18