Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
335 views
in Technique[技术] by (71.8m points)

python - How can I plot a 3D scatter plot (plt.plot), instead of just show it (plt.show)?

How can I plot a 3D scatter plot (plt.plot), instead of just show it (plt.show)?

Code snippet:

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

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

xp1A = np.ones(1024) 
xp1B = np.ones(1024) 
xp1C = np.ones(1024) 
yp1A = np.ones(1024) 
yp1B = np.ones(1024) 
yp1C = np.ones(1024) 
zp1 = np.empty(1024)
for i in range(1024):
    zp1[i] = 1

xp2A = np.ones(1024) 
xp2B = np.ones(1024) 
xp2C = np.ones(1024) 
yp2A = np.ones(1024) 
yp2B = np.ones(1024) 
yp2C = np.ones(1024) 
zp2 = np.empty(1024)
for i in range(1024):
    zp2[i] = 2

p1A = ax.scatter(xp1A, yp1A, zp1, c='red', label = 'Variance')
p1B = ax.scatter(xp1B, yp1B, zp1, c='blue', label = 'Skewness')
p1C = ax.scatter(xp1C, yp1C, zp1, c='green', label = 'Kurtosis')

p2A = ax.scatter(xp2A, yp2A, zp2, c='red')
p2B = ax.scatter(xp2B, yp2B, zp2, c='blue')
p2C = ax.scatter(xp2C, yp2C, zp2, c='green')

ax.set_zticks([1,2])
ax.set_xlabel('Snapshot')
ax.set_ylabel('Value')
ax.set_zlabel('Dataset')

ax.legend()
plt.plot() #I am trying to do this
plt.plot(p1A,p1B,p1C,p2A,p2B,p2C) #Or this, but none works
plt.show()

I want to plot it, to show it in a proper window, so that I can enlarge the plot, save it, etc.

Thanks in advance!

EDIT: I think I got why I can't view this windows. I am running the code at Google Colab. Any idea how to solve it?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

See the code below for the answer , and how subplot can be used :

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

fig = plt.figure(figsize=(12,10))

ax = fig.add_subplot(111, projection ='3d')   #top left


def randrange(n, vmin, vmax):
    '''
    Helper function to make an array of random numbers having shape (n, )
    with each number distributed Uniform(vmin, vmax).
    '''
    return (vmax - vmin)*np.random.rand(n) + vmin

n =1024

xp1A = np.ones(1024) * randrange(n, 23, 32)
xp1B = np.ones(1024) *randrange(n, 11, 32)
xp1C = np.ones(1024) * randrange(n, 25, 27)
yp1A = np.ones(1024) *randrange(n, 9, 16)
yp1B = np.ones(1024) * randrange(n, 1, 11)
yp1C = np.ones(1024) * randrange(n, 5, 18)
zp1 = np.empty(1024)
for i in range(1024):
    zp1[i] = 1

xp2A = np.ones(1024) * randrange(n, 6, 44)
xp2B = np.ones(1024) * randrange(n, 7, 63)
xp2C = np.ones(1024) * randrange(n, 2, 22)
yp2A = np.ones(1024) * randrange(n, 11, 69)
yp2B = np.ones(1024) * randrange(n, 41, 77)
yp2C = np.ones(1024) * randrange(n, 63, 78)
zp2 = np.empty(1024)
for i in range(1024):
    zp2[i] = 2

#p1A = ax.scatter(xp1A , yp1A, zp1 c='red')

ax.set_xlabel('Snapshot')
ax.set_ylabel('Value')
ax.set_zlabel('Dataset')
ax.legend()

plt.scatter(xp1A , yp1A, zp1, c='red')
plt.scatter(xp1B , yp1B, zp1, c='blue')
plt.scatter(xp1C , yp1C, zp1, c='green')
plt.scatter(xp2A , yp2A, zp2, c='yellow')
plt.scatter(xp2B , yp2B, zp2, c='black')
plt.scatter(xp2C , yp2C, zp2, c='brown')
#p1A.plot()
#plt.plot() #I am trying to do this
#lt.plot(p1A,p1B,p1C,p2A,p2B,p2C) #Or this, but none works
plt.show()

Six data arrays plotted in one figure


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...