0

I have a number of data I want to plot with every combination in 3D. So Data looks like that.

enter image description here

I created a matrix:

a = array([[1,2,2],[3,4,3],[4,2,2],[2,7,4],[5,8,1],[7,1,7]])

Im gonna skip the loop for this example.

k = 1 ; j = 0; z = 2
fig = figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(a[:,j], a[:,z], a[:,k])

Next step is that I wanna color plots by the column A (if they belong to Random1 or Random2) and make a legend based on this condition. And it doesnt work.

My attempt was to make a list of column A

a = [Random1, Random2]

and put it in my figure ploting.

k = 1 ; j = 0; z = 2
fig = figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(a[:,j], a[:,z], a[:,k], label = a)
ax.legend()

basically I want to have something like this scatter plot with legend colored by group without multiple calls to plt.scatter

but with matplotlib (its a necessity) and in 3d. Hope its clear what I want to do.

Edit: with this addition, it does color them as I want, but Still have no idea who to make a legend based on the color.

ax.scatter(a[:,j], a[:,z], a[:,k], c = len, cmap = 'brg'))

where len is length of my data.

Can I use something like pandas, to add labels to corresponding data, but read data from matrix?

Noob Programmer
  • 698
  • 2
  • 6
  • 22

1 Answers1

0

I am updating my answer. So based on what you said you want to plot all possible combinations of the points.

This should work fine

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from numpy import array
import matplotlib.patches as mpatches

a = array([[1,2,2],[3,4,3],[4,2,2],[2,7,4],[5,8,1],[7,1,7]])

colors = ['r', 'r','r','g','g','g']

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

for i in range(3):
    for j in range(3):
        for k in range(3):
            ax.scatter(a[:,i], a[:,j], a[:,k], c=colors)

ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')

R1 = mpatches.Patch(color='red', label='Random 1')
R2 = mpatches.Patch(color='green', label='Random 2')
plt.legend(handles=[R1,R2])
plt.show()

enter image description here

seralouk
  • 30,938
  • 9
  • 118
  • 133