如何随机给'n'(比如说3)颜色到python中的散点图点?
How to give only 'n'(say 3) colors randomly to scatterplot points in python?
生成随机数 x、y、z,然后将它们放入 3d 散点图中,但无法绘制点,随机使用 3 种特定颜色(比如红色、黑色、黄色)。
在 matplotlib.pyplot.scatter 的文档中,我无法理解除第一种以外的其他 3 种指定颜色的方法。
代码:
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d, Axes3D
x, y, z = np.random.rand(3,50)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x,y,z,marker='.',color='b')
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.show()
如果您只想随机分配颜色给一组 n 种颜色中的每个点,您可以这样做:
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d, Axes3D
x, y, z = np.random.rand(3,50)
n=3
colors = np.random.randint(n, size=x.shape[0])
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x,y,z,marker='.',c=colors)
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.show()
生成随机数 x、y、z,然后将它们放入 3d 散点图中,但无法绘制点,随机使用 3 种特定颜色(比如红色、黑色、黄色)。
在 matplotlib.pyplot.scatter 的文档中,我无法理解除第一种以外的其他 3 种指定颜色的方法。
代码:
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d, Axes3D
x, y, z = np.random.rand(3,50)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x,y,z,marker='.',color='b')
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.show()
如果您只想随机分配颜色给一组 n 种颜色中的每个点,您可以这样做:
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d, Axes3D
x, y, z = np.random.rand(3,50)
n=3
colors = np.random.randint(n, size=x.shape[0])
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x,y,z,marker='.',c=colors)
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.show()