zedd
zedd

Reputation: 21

Randomly generated points in 3D scatter plot are plotted in different figures

I would like to generate a lot of scatter points in a 3D graph but my code plots the points in different figures.

So im trying to generate a lot of scatter points on the same plot, but however the code is forming a new one in each cycle of for code.

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

def random_position(total_number, blue_number):
    my_list = list(range(1, total_number))
    random.shuffle(my_list)
    results = my_list
    index = 1
    nn = 1

    fig1 = plt.figure()


    random_list = list(itertools.product(range(1, 51), range(1, 51), range(1,11)))
    for colour in results:
        if colour < blue_number:
            a = (index, 'blue', colour)
            b = random.sample(random_list, nn)
            c = list(a)
            d = list(b)
            e = c + d
            index += 1
            xx, yy, zz = zip(*b)
            ax = fig1.add_subplot(projection='3d')
            xs = xx
            ys = yy
            zs = zz
            ax.scatter(xs, ys, zs, marker='o')

            print(xs, ys, zs)
        else:
            a = (index, 'red', colour)
            b = random.sample(random_list, nn)
            c = list(a)
            d = list(b)
            e = c + d
            index += 1
            xx, yy, zz = zip(*b)
            ax = fig1.add_subplot(projection='3d')
            xs = xx
            ys = yy
            zs = zz
            ax.scatter(xs, ys, zs, marker='o')
            print(xs, ys, zs)

        plt.show()
    return

random_position(250,25)

Upvotes: 2

Views: 351

Answers (1)

pacdev
pacdev

Reputation: 581

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

def ramdom_position(total_number, blue_number):
    my_list = list(range(1, total_number))
    random.shuffle(my_list)
    results = my_list
    index = 1
    nn = 1

    fig1 = plt.figure()
    ax = fig1.add_subplot(projection='3d')

    random_list = list(itertools.product(range(1, 51), range(1, 51), range(1,11)))
    for colour in results:
        if colour < blue_number:
            a = (index, 'blue', colour)
            b = random.sample(random_list, nn)
            c = list(a)
            d = list(b)
            e = c + d
            index += 1
            xx, yy, zz = zip(*b)
            
            xs = xx
            ys = yy
            zs = zz
            ax.scatter(xs, ys, zs, marker='o')

            print(xs, ys, zs)
        else:
            a = (index, 'red', colour)
            b = random.sample(random_list, nn)
            c = list(a)
            d = list(b)
            e = c + d
            index += 1
            xx, yy, zz = zip(*b)
        
            xs = xx
            ys = yy
            zs = zz
            ax.scatter(xs, ys, zs, marker='o')
            print(xs, ys, zs)

    plt.show()
    return

ramdom_position(250,25)

random point plot 3D

Upvotes: 1

Related Questions