繁体   English   中英

python-如何仅给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的文档中,除了第一种方法之外,我无法理解其他三种指定颜色的方法。

码:

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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM