Python中的非重复颜色和特定线宽

时间:2019-01-13 22:57:44

标签: python plot

这是绘制一系列轨迹的代码的一小部分。我想知道如何不获得重复的颜色,因为有时使用random.choice()会得到一些相同颜色的轨道。我想知道是否有可能为“机构”中包含的每个机构选择特定的机体。预先谢谢你。

#Output of the code
def plot_output(bodies, outfile = None):
    fig = plot.figure()
    colours = ['r', 'b','g','y','m','c', 'black','aqua', 'salmon', 'orangered', 'lime', 'mediumseagreen', 'yellow', 'gold', 'darkcyan', 'lime', 'magenta', 'grey', 'mediumslateblue', 'dimgray', 'deeppink', 'firebrick', 'pink', 'deepskyblue', 'olive', 'greenyellow', 'thistle', 'springgreen']
    ax = fig.add_subplot(1,1,1, projection='3d')
    max_range = 0
    for current_body in bodies:
        max_dim = max(max(current_body["x"]),max(current_body["y"]),max(current_body["z"]))
        if max_dim > max_range:
            max_range = max_dim
        ax.plot(current_body["x"], current_body["y"], current_body["z"], c = random.choice(colours), label = current_body["name"])       
    ax.set_xlim([-max_range,max_range])    
    ax.set_ylim([-max_range,max_range])
    ax.set_zlim([-max_range,max_range])
    ax.legend()       

    if outfile:
        plot.savefig(outfile)
    else:
        plot.show()

1 个答案:

答案 0 :(得分:0)

如果希望采样的颜色彼此唯一,则可以使用random.sample(population, k)。 它从总体中采样了k个“独特”元素。

以下是显示如何使用它的脚本:

bodies = ['a', 'b', 'c']
colours = ['r', 'b','g','y','m','c', 'black','aqua', 'salmon', 'orangered', 'lime', 'mediumseagreen', 'yellow', 'gold', 'darkcyan', 'lime', 'magenta', 'grey', 'mediumslateblue', 'dimgray', 'deeppink', 'firebrick', 'pink', 'deepskyblue', 'olive', 'greenyellow', 'thistle', 'springgreen']
sampled_colours = random.sample(colours, len(bodies))

for current_body, colour in zip(bodies, sampled_colours):
    print(current_body, colour)