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?

9
  • do you want to plot each line of the matrix a as points ?
    – seralouk
    Nov 16, 2019 at 12:42
  • What do you mean as points? Isn't that scatter about to plot them as points not connect them? This is just an example. I have around 10000 points and around 30 groups of random in my data. Nov 16, 2019 at 12:44
  • or something like this, but again with matplot lib and in 3d. - stackoverflow.com/questions/44595288/… Nov 16, 2019 at 12:59
  • Can I use something like pandas for adding labels? Nov 16, 2019 at 14:15
  • you want to plot each line or all possible combinations? from the loop you use I believe you need all combinations but then the labels do not make sense. Line 1 has label Random1 but if you mix the points what are the labels?
    – seralouk
    Nov 16, 2019 at 14:45

1 Answer 1

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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.